From 4070e857de4ffcc399539f586bdd0aa711535a48 Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Thu, 19 Oct 2023 16:45:41 -0400 Subject: [PATCH] tags --- src/Plugin/WebformElement/CivicrmContact.php | 4 +--- src/Utils.php | 17 ++++++++++++++--- .../GroupsTagsSubmissionTest.php | 6 +++--- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Plugin/WebformElement/CivicrmContact.php b/src/Plugin/WebformElement/CivicrmContact.php index e3d8a240b..310756f7b 100644 --- a/src/Plugin/WebformElement/CivicrmContact.php +++ b/src/Plugin/WebformElement/CivicrmContact.php @@ -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; @@ -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).'), ]; diff --git a/src/Utils.php b/src/Utils.php index 0fb062dba..ff0b15d64 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -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. @@ -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]; @@ -1021,4 +1022,14 @@ public function hasMultipleValues($element) { return FALSE; } + /** + * @return 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'; + } + } diff --git a/tests/src/FunctionalJavascript/GroupsTagsSubmissionTest.php b/tests/src/FunctionalJavascript/GroupsTagsSubmissionTest.php index 710564cd6..9977c6cf9 100644 --- a/tests/src/FunctionalJavascript/GroupsTagsSubmissionTest.php +++ b/tests/src/FunctionalJavascript/GroupsTagsSubmissionTest.php @@ -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); @@ -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. @@ -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));