Skip to content

Commit

Permalink
Merge pull request #919 from demeritcowboy/major-donor
Browse files Browse the repository at this point in the history
Address display of tags
  • Loading branch information
jitendrapurohit authored Oct 29, 2023
2 parents d8a4e67 + 386f9b0 commit b4dd3d9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
4 changes: 1 addition & 3 deletions src/Plugin/WebformElement/CivicrmContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Drupal\webform_civicrm\Plugin\WebformElement;

use CRM_Core_BAO_Tag;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Form\FormStateInterface;
Expand Down Expand Up @@ -400,12 +399,11 @@ public function form(array $form, FormStateInterface $form_state) {
'#default_value' => $element_properties['group'],
'#description' => $this->t('Listed contacts must be members of at least one of the selected groups (leave blank to not filter by group).'),
];
$tags = [];
$form['filters']['tag'] = [
'#type' => 'select',
'#multiple' => TRUE,
'#title' => $this->t('Tags'),
'#options' => ['' => '- ' . $this->t('None') . ' -'] + CRM_Core_BAO_Tag::getTags('civicrm_contact', $tags, NULL, '- '),
'#options' => ['' => '- ' . $this->t('None') . ' -'] + $utils->wf_crm_get_tags('contact'),
'#default_value' => $element_properties['tag'],
'#description' => $this->t('Listed contacts must be have at least one of the selected tags (leave blank to not filter by tag).'),
];
Expand Down
17 changes: 14 additions & 3 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,15 @@ function wf_crm_get_tags($used_for, $parent_id = NULL) {
'parent_id' => $parent_id ?: ['IS NULL' => 1],
'options' => ['sort' => 'name'],
];
$tags = $this->wf_crm_apivalues('Tag', 'get', $params, 'name');
$tag_display_field = $this->tag_display_field();
$tags = $this->wf_crm_apivalues('Tag', 'get', $params, $tag_display_field);
// Tagsets cannot be nested so no need to fetch children
if ($parent_id || !$tags) {
return $tags;
}
// Fetch child tags
unset($params['parent_id']);
$params += ['return' => ['name', 'parent_id'], 'parent_id.is_tagset' => 0, 'parent_id.is_selectable' => 1, 'parent_id.used_for' => $params['used_for']];
$params += ['return' => [$tag_display_field, 'parent_id'], 'parent_id.is_tagset' => 0, 'parent_id.is_selectable' => 1, 'parent_id.used_for' => $params['used_for']];
$unsorted = $this->wf_crm_apivalues('Tag', 'get', $params);
$parents = array_fill_keys(array_keys($tags), ['depth' => 1]);
// Place children under their parents.
Expand All @@ -244,7 +245,7 @@ function wf_crm_get_tags($used_for, $parent_id = NULL) {
foreach ($unsorted as $id => $tag) {
$parent = $tag['parent_id'];
if (isset($parents[$parent])) {
$name = str_repeat('- ', $parents[$parent]['depth']) . $tag['name'];
$name = str_repeat('- ', $parents[$parent]['depth']) . $tag[$tag_display_field];
$pos = array_search($parents[$parent]['child'] ?? $parent, array_keys($tags)) + 1;
$tags = array_slice($tags, 0, $pos, TRUE) + [$id => $name] + array_slice($tags, $pos, NULL, TRUE);
$parents[$id] = ['depth' => $parents[$parent]['depth'] + 1];
Expand Down Expand Up @@ -1021,4 +1022,14 @@ public function hasMultipleValues($element) {
return FALSE;
}

/**
* @return string Which field is the tag display field in this version of civi?
*/
private function tag_display_field(): string {
if (version_compare(\CRM_Core_BAO_Domain::version(), '5.68.alpha1', '>=')) {
return 'label';
}
return 'name';
}

}
6 changes: 3 additions & 3 deletions tests/src/FunctionalJavascript/GroupsTagsSubmissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function testSubmitWebform() {
$this->editCivicrmOptionElement('edit-webform-ui-elements-civicrm-1-contact-1-other-group-operations', FALSE, FALSE, NULL, 'checkboxes');

$majorDonorTagID = $this->utils->wf_civicrm_api('Tag', 'get', [
'name' => "Major Donor",
'name' => (version_compare(\CRM_Core_BAO_Domain::version(), '5.68.alpha1', '<') ? "Major Donor" : "Major_Donor"),
])['id'];
// Make Major Donor as the default option.
$this->editCivicrmOptionElement('edit-webform-ui-elements-civicrm-1-contact-1-other-tag-operations', TRUE, FALSE, $majorDonorTagID);
Expand Down Expand Up @@ -146,7 +146,7 @@ public function testSubmitWebform() {

$this->assertTrue(in_array($this->groups['GroupB'], $contactGroups));
$this->assertTrue(in_array($this->groups['GroupC'], $contactGroups));
$this->assertTrue(in_array('Major Donor', $contactTags));
$this->assertTrue(in_array(version_compare(\CRM_Core_BAO_Domain::version(), '5.68.alpha1', '<') ? "Major Donor" : "Major_Donor", $contactTags));
$this->assertTrue(in_array('Volunteer', $contactTags));

// Ensure option labels are present on result page.
Expand Down Expand Up @@ -197,7 +197,7 @@ public function testSubmitWebform() {
])['values'][0];
$contactTags = explode(',', $contact['tags']);
$contactGroups = explode(',', $contact['groups']);
$this->assertTrue(in_array('Major Donor', $contactTags));
$this->assertTrue(in_array(version_compare(\CRM_Core_BAO_Domain::version(), '5.68.alpha1', '<') ? "Major Donor" : "Major_Donor", $contactTags));
$this->assertFalse(in_array('Volunteer', $contactTags));

$this->assertTrue(in_array($this->groups['GroupA'], $contactGroups));
Expand Down

0 comments on commit b4dd3d9

Please sign in to comment.