Skip to content

Commit

Permalink
afform_tags - use Tag entity rather than option group
Browse files Browse the repository at this point in the history
  • Loading branch information
ufundo committed Jan 27, 2025
1 parent 92c2188 commit 6cc97d4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 26 deletions.
7 changes: 1 addition & 6 deletions ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ public static function getAdminSettings() {
->addWhere('option_group_id:name', '=', 'afform_placement')
->addOrderBy('weight')
->execute(), 'label', 'value');
$afformTags = \CRM_Utils_Array::formatForSelect2((array) \Civi\Api4\OptionValue::get(FALSE)
->addSelect('value', 'label', 'icon', 'description')
->addWhere('is_active', '=', TRUE)
->addWhere('option_group_id:name', '=', 'afform_tags')
->addOrderBy('weight')
->execute(), 'label', 'value');
$afformTags = \CRM_Utils_Array::formatForSelect2((array) \Civi\Api4\Utils\AfformTags::getTagOptions());
$afformTypes = (array) \Civi\Api4\OptionValue::get(FALSE)
->addSelect('name', 'label', 'icon')
->addWhere('is_active', '=', TRUE)
Expand Down
4 changes: 3 additions & 1 deletion ext/afform/core/Civi/Api4/Afform.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ public static function getFields($checkPermissions = TRUE) {
[
'name' => 'tags',
'title' => E::ts('Tags'),
'pseudoconstant' => ['optionGroupName' => 'afform_tags'],
'pseudoconstant' => [
'callback' => [Utils\AfformTags::class, 'getTagOptions'],
],
'data_type' => 'Array',
'input_type' => 'Select',
],
Expand Down
33 changes: 33 additions & 0 deletions ext/afform/core/Civi/Api4/Utils/AfformTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Civi\Api4\Utils;

/**
* Class AfformTags
* @package Civi\Api4\Utils
*
* Utils for managing the tags field on Afforms
*
*/
class AfformTags {

/**
* @return array
*/
public static function getTagOptions(): array {
$tagRecords = (array) \Civi\Api4\Tag::get(FALSE)
->addSelect('name', 'label', 'description', 'color')
->addWhere('used_for', 'CONTAINS', 'Afform')
->addWhere('is_selectable', '=', TRUE)
->execute();

$tagOptions = [];

// prefer tag names to keys for portability
foreach ($tagRecords as $record) {
$record['id'] = $record['name'];
$tagOptions[] = $record;
}

return $tagOptions;
}
}
41 changes: 22 additions & 19 deletions ext/afform/core/managed/AfformTags.mgd.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,52 @@

use CRM_Afform_ExtensionUtil as E;

// Option group for Afform.tags field
// This file:
// - adds option value to `tag_used_for`, allowing Afforms to be tagged
// NOTE: this is unusual in that afform is not a db entity
// (normally the value normally corresponds to a physical table name in the
// database - here it is just the entity name)
//
// - creates an example tag that can be used for afforms. This is more to demonstrate
// how it can be used, as Donor Journey might be useful for some orgs but not for others
// @todo think of better example tag that is more universally applicable?
return [
[
'name' => 'AfformTags',
'entity' => 'OptionGroup',
'update' => 'always',
'name' => 'AfformTags:tag_used_for',
'entity' => 'OptionValue',
'cleanup' => 'always',
'update' => 'always',
'params' => [
'version' => 4,
'values' => [
'name' => 'afform_tags',
'title' => E::ts('Afform Tags'),
'is_reserved' => TRUE,
'option_group_id.name' => 'tag_used_for',
'value' => 'Afform',
'name' => 'Afform',
'label' => E::ts('Afforms'),
'is_reserved' => FALSE,
'is_active' => TRUE,
'option_value_fields' => [
'name',
'label',
'icon',
'description',
'color',
],
'domain_id' => NULL,
],
'match' => ['name'],
'match' => ['option_group_id', 'name'],
],
],
[
'name' => 'AfformTags:donor_journey',
'entity' => 'OptionValue',
'entity' => 'Tag',
'cleanup' => 'unused',
'update' => 'unmodified',
'params' => [
'version' => 4,
'values' => [
'option_group_id.name' => 'afform_tags',
'name' => 'donor_journey',
'value' => 'donor_journey',
'tag_used_for' => ['afform'],
'label' => E::ts('Donor Journey'),
'is_reserved' => FALSE,
'is_active' => TRUE,
'icon' => 'fa-coins',
'description' => E::ts('Forms relating to donor journey.'),
],
'match' => ['option_group_id', 'name'],
'match' => ['name'],
],
],
];

0 comments on commit 6cc97d4

Please sign in to comment.