From ec38fd6496ca4f4d740c98db506675d5f8e28d75 Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Sun, 3 Dec 2023 15:24:29 +0530 Subject: [PATCH] Replace group usage with crmgroup --- src/AdminForm.php | 5 +- src/ContactComponent.php | 10 ++- src/FieldOptions.php | 2 +- src/Fields.php | 2 +- src/Plugin/WebformElement/CivicrmContact.php | 20 +---- src/WebformCivicrmBase.php | 1 + src/WebformCivicrmPostProcess.php | 4 +- .../ContactSubmissionTest.php | 4 +- .../GroupsTagsSubmissionTest.php | 6 +- .../FunctionalJavascript/LocationTypeTest.php | 2 +- .../WebformCivicrmTestBase.php | 6 +- webform_civicrm.install | 76 +++++++++++++++++++ 12 files changed, 103 insertions(+), 35 deletions(-) diff --git a/src/AdminForm.php b/src/AdminForm.php index 852cd7b78..bf59ac6d7 100644 --- a/src/AdminForm.php +++ b/src/AdminForm.php @@ -2,7 +2,6 @@ namespace Drupal\webform_civicrm; - use Drupal\Core\Session\AnonymousUserSession; use Drupal\webform\Entity\Webform; use Drupal\Component\Render\FormattableMarkup; @@ -1573,7 +1572,7 @@ private function addItem($fid, $field) { if ($field['type'] != 'hidden') { $options += ['create_civicrm_webform_element' => t('- User Select -')]; } - if ($name == 'group') { + if ($name == 'crmgroup') { $options += ['public_groups' => t('- User Select - (public groups)')]; } $options += $this->utils->wf_crm_field_options($field, 'config_form', $this->data); @@ -1948,7 +1947,7 @@ public function postProcess() { $val = (array) $val; if (in_array('create_civicrm_webform_element', $val, TRUE) || (!empty($val[0]) && $field['type'] == 'hidden') - || (preg_match('/_group$/', $key) && in_array('public_groups', $val, TRUE))) { + || (preg_match('/_crmgroup$/', $key) && in_array('public_groups', $val, TRUE))) { // Restore disabled component if (isset($disabled[$key])) { webform_component_update($disabled[$key]); diff --git a/src/ContactComponent.php b/src/ContactComponent.php index 61c1bc31c..9cbfd90f9 100644 --- a/src/ContactComponent.php +++ b/src/ContactComponent.php @@ -338,7 +338,7 @@ function wf_crm_search_filters($node, array $component) { 'contact_type', 'contact_sub_type', 'tag', - 'group', + 'crmgroup', 'relationship' => [ 'contact', 'types', @@ -364,9 +364,13 @@ function wf_crm_search_filters($node, array $component) { $this->wf_crm_search_filterArray($filterVal); if ($filterVal) { switch ($filter) { - case 'group': + case 'crmgroup': + $filter = 'groups'; + $op = 'IN'; + break; + case 'tag': - $filter .= 's'; + $filter = 'tags'; $op = 'IN'; break; diff --git a/src/FieldOptions.php b/src/FieldOptions.php index 38169536b..bf3cc4a8e 100644 --- a/src/FieldOptions.php +++ b/src/FieldOptions.php @@ -68,7 +68,7 @@ public function get($field, $context, $data) { } elseif (isset($field['table']) && $field['table'] === 'group') { $params = ['is_hidden' => 0]; - $options = wf_crm_aval($data, "contact:$c:other:1:group"); + $options = wf_crm_aval($data, "contact:$c:other:1:crmgroup"); if (!empty($options) && !empty($options['public_groups'])) { $params['visibility'] = "Public Pages"; } diff --git a/src/Fields.php b/src/Fields.php index 56ee791ad..676f41605 100644 --- a/src/Fields.php +++ b/src/Fields.php @@ -448,7 +448,7 @@ protected function wf_crm_get_fields($var = 'fields') { 'type' => 'select', 'expose_list' => TRUE, ]; - $fields['other_group'] = [ + $fields['other_crmgroup'] = [ 'name' => t('Group(s)'), 'type' => 'select', 'civicrm_live_options' => 1, diff --git a/src/Plugin/WebformElement/CivicrmContact.php b/src/Plugin/WebformElement/CivicrmContact.php index 310756f7b..a8bb9342a 100644 --- a/src/Plugin/WebformElement/CivicrmContact.php +++ b/src/Plugin/WebformElement/CivicrmContact.php @@ -62,7 +62,7 @@ public function getDefaultProperties() { 'filter_relationship_types' => [], 'filter_relationship_contact' => [], 'contact_sub_type' => '', - 'group' => [], + 'crmgroup' => [], 'tag' => [], 'check_permissions' => 1, // Set for custom fields. @@ -81,15 +81,6 @@ public function prepare(array &$element, WebformSubmissionInterface $webform_sub \Drupal::service('civicrm')->initialize(); $element['#form_key'] = $element['#form_key'] ?? $element['#webform_key']; - // Avoid call to Drupal\Core\Render\Element\RenderElement::processGroup() as this module uses the - // 'group' property key to filter the contacts loaded in the autocomplete field. - if ($element['#widget'] === 'autocomplete') { - $element['#process'] = [ - ['Drupal\Core\Render\Element\Textfield', 'processAutocomplete'], - ['Drupal\Core\Render\Element\Textfield', 'processAjaxForm'], - ['Drupal\Core\Render\Element\Textfield', 'processPattern'], - ]; - } // Webform removes values which equal their defaults but does not populate // they keys. $ensure_keys_have_values = [ @@ -391,12 +382,12 @@ public function form(array $form, FormStateInterface $form_state) { '#default_value' => $element_properties['contact_sub_type'], ]; } - $form['filters']['group'] = [ + $form['filters']['crmgroup'] = [ '#type' => 'select', '#multiple' => TRUE, '#title' => $this->t('Groups'), '#options' => ['' => '- ' . $this->t('None') . ' -'] + $utils->wf_crm_apivalues('group_contact', 'getoptions', ['field' => 'group_id']), - '#default_value' => $element_properties['group'], + '#default_value' => $element_properties['crmgroup'], '#description' => $this->t('Listed contacts must be members of at least one of the selected groups (leave blank to not filter by group).'), ]; $form['filters']['tag'] = [ @@ -648,10 +639,7 @@ public static function validateRequired(&$element, FormStateInterface &$form_sta $args = [ '%name' => empty($element['#title']) ? $element['#parents'][0] : $element['#title'], ]; - // Avoid error while calling form_state which expects '#group' as a string value :(. - $static_element = $element; - unset($static_element['#group']); - $form_state->setError($static_element, t('%name field is required.', $args)); + $form_state->setError($element, t('%name field is required.', $args)); } } diff --git a/src/WebformCivicrmBase.php b/src/WebformCivicrmBase.php index 9f73dbf09..7551e5949 100644 --- a/src/WebformCivicrmBase.php +++ b/src/WebformCivicrmBase.php @@ -214,6 +214,7 @@ protected function loadContact($c, $exclude = []) { if (strpos($fid, $prefix . 'other') !== FALSE) { list(, , , , , $ent) = explode('_', $fid); list(, , , , , $field) = explode('_', $fid, 6); + $ent = ($ent === 'crmgroup') ? 'group' : $ent; // Cheap way to avoid fetching the same data twice from the api if (!is_array($api[$ent])) { $api[$ent] = $this->utils->wf_civicrm_api($api[$ent], 'get', ['contact_id' => $cid]); diff --git a/src/WebformCivicrmPostProcess.php b/src/WebformCivicrmPostProcess.php index 9adfff5e3..05a9dfa85 100644 --- a/src/WebformCivicrmPostProcess.php +++ b/src/WebformCivicrmPostProcess.php @@ -2490,9 +2490,9 @@ private function fillDataFromSubmission() { if (!empty($this->data[$ent][$c][$table][$n][$name]) && is_array($this->data[$ent][$c][$table][$n][$name])) { $val = array_unique(array_merge($val, $this->data[$ent][$c][$table][$n][$name])); } - if (substr($name, 0, 6) === 'custom' || ($table == 'other' && in_array($name, ['group', 'tag']))) { + if (substr($name, 0, 6) === 'custom' || ($table == 'other' && in_array($name, ['crmgroup', 'tag']))) { $val = array_filter($val); - if ($name === 'group') { + if ($name === 'crmgroup') { unset($val['public_groups']); } } diff --git a/tests/src/FunctionalJavascript/ContactSubmissionTest.php b/tests/src/FunctionalJavascript/ContactSubmissionTest.php index f3463cc6a..7c9a526f3 100644 --- a/tests/src/FunctionalJavascript/ContactSubmissionTest.php +++ b/tests/src/FunctionalJavascript/ContactSubmissionTest.php @@ -80,7 +80,7 @@ public function testAutocompleteWithGroupFilter() { 'selector' => 'edit-webform-ui-elements-civicrm-1-contact-1-contact-existing-operations', 'widget' => 'Autocomplete', 'filter' => [ - 'group' => $this->group['id'], + 'crmgroup' => $this->group['id'], ], ]; $this->editContactElement($editContact); @@ -123,7 +123,7 @@ public function testSelectContactElement() { 'selector' => 'edit-webform-ui-elements-civicrm-1-contact-1-contact-existing-operations', 'widget' => 'Select List', 'filter' => [ - 'group' => $this->group['id'], + 'crmgroup' => $this->group['id'], ], ]; $this->editContactElement($editContact); diff --git a/tests/src/FunctionalJavascript/GroupsTagsSubmissionTest.php b/tests/src/FunctionalJavascript/GroupsTagsSubmissionTest.php index 8202a787f..834009a93 100644 --- a/tests/src/FunctionalJavascript/GroupsTagsSubmissionTest.php +++ b/tests/src/FunctionalJavascript/GroupsTagsSubmissionTest.php @@ -49,7 +49,7 @@ public function testPublicGroups() { // Enable Groups Field and then set it to -User Select (Public Group)- $this->getSession()->getPage()->selectFieldOption('contact_1_number_of_other', 'Yes'); $this->assertSession()->assertWaitOnAjaxRequest(); - $this->getSession()->getPage()->selectFieldOption("civicrm_1_contact_1_other_group[]", 'public_groups'); + $this->getSession()->getPage()->selectFieldOption("civicrm_1_contact_1_other_crmgroup[]", 'public_groups'); $this->htmlOutput(); $this->saveCiviCRMSettings(); @@ -85,7 +85,7 @@ public function testSubmitWebform() { // Enable Tags and Groups Fields and then set Tag(s) to -User Select- $this->getSession()->getPage()->selectFieldOption('contact_1_number_of_other', 'Yes'); $this->assertSession()->assertWaitOnAjaxRequest(); - $this->getSession()->getPage()->selectFieldOption("civicrm_1_contact_1_other_group[]", 'create_civicrm_webform_element'); + $this->getSession()->getPage()->selectFieldOption("civicrm_1_contact_1_other_crmgroup[]", 'create_civicrm_webform_element'); $this->getSession()->getPage()->selectFieldOption("civicrm_1_contact_1_other_tag[]", 'create_civicrm_webform_element'); $this->htmlOutput(); $this->saveCiviCRMSettings(); @@ -94,7 +94,7 @@ public function testSubmitWebform() { $this->htmlOutput(); // Change type of group field to checkbox. - $this->editCivicrmOptionElement('edit-webform-ui-elements-civicrm-1-contact-1-other-group-operations', FALSE, FALSE, NULL, 'checkboxes'); + $this->editCivicrmOptionElement('edit-webform-ui-elements-civicrm-1-contact-1-other-crmgroup-operations', FALSE, FALSE, NULL, 'checkboxes'); $majorDonorTagID = $this->utils->wf_civicrm_api('Tag', 'get', [ 'name' => (version_compare(\CRM_Core_BAO_Domain::version(), '5.68.alpha1', '<') ? "Major Donor" : "Major_Donor"), diff --git a/tests/src/FunctionalJavascript/LocationTypeTest.php b/tests/src/FunctionalJavascript/LocationTypeTest.php index e22f7e0a5..6fdb0fd12 100644 --- a/tests/src/FunctionalJavascript/LocationTypeTest.php +++ b/tests/src/FunctionalJavascript/LocationTypeTest.php @@ -127,7 +127,7 @@ public function testLockedAddressSubmission() { 'widget' => 'Autocomplete', 'hide_fields' => 'address', 'filter' => [ - 'group' => '- None -', + 'crmgroup' => '- None -', ], ]; $this->editContactElement($editContact); diff --git a/tests/src/FunctionalJavascript/WebformCivicrmTestBase.php b/tests/src/FunctionalJavascript/WebformCivicrmTestBase.php index 311ea05da..fb05a7728 100644 --- a/tests/src/FunctionalJavascript/WebformCivicrmTestBase.php +++ b/tests/src/FunctionalJavascript/WebformCivicrmTestBase.php @@ -492,7 +492,7 @@ protected function setDefaultValue($selector, $value) { * 'widget' => 'Static', * 'default' => 'relationship', * 'filter' => [ - * 'group' => group_id, + * 'crmgroup' => group_id, * ], * 'default_relationship' => [ * 'default_relationship_to' => 'Contact 3', @@ -567,8 +567,8 @@ protected function editContactElement($params) { // Apply contact filter. if (!empty($params['filter'])) { $this->assertSession()->elementExists('css', '[data-drupal-selector="edit-filters"]')->click(); - if (!empty($params['filter']['group'])) { - $this->getSession()->getPage()->selectFieldOption('Groups', $params['filter']['group']); + if (!empty($params['filter']['crmgroup'])) { + $this->getSession()->getPage()->selectFieldOption('Groups', $params['filter']['crmgroup']); } if (!empty($params['filter']['filter_relationship_types'])) { $this->getSession()->getPage()->selectFieldOption('properties[filter_relationship_types][]', $params['filter']['filter_relationship_types']); diff --git a/webform_civicrm.install b/webform_civicrm.install index 70e8f8290..4c4812d1a 100644 --- a/webform_civicrm.install +++ b/webform_civicrm.install @@ -443,3 +443,79 @@ function webform_civicrm_update_8007() { } } } + +/** + * Replace element with a new element key. + */ +function _recursiveKeyReplace(array &$elements, $searchKey, $replacementKey) { + foreach ($elements as $key => &$value) { + if ($key === $searchKey) { + $keys = array_keys($elements); + $index = array_search($key, $keys, true); + if ($index !== false) { + array_splice($keys, $index, 1, $replacementKey); + $elements = array_combine($keys, array_values($elements)); + } + } + elseif (is_array($value)) { + _recursiveKeyReplace($value, $searchKey, $replacementKey); + } + } +} + +/** + * Update usage of group to crmgroup + */ +function webform_civicrm_update_8008() { + $webforms = Webform::loadMultiple(); + + foreach ($webforms as $webform) { + $handler = $webform->getHandlers('webform_civicrm'); + $config = $handler->getConfiguration(); + $elements = $webform->getElementsDecodedAndFlattened(); + if (empty($config['webform_civicrm'])) { + continue; + } + $settings = &$config['webform_civicrm']['settings']; + foreach ($elements as $key => $value) { + [$civifield] = explode('_', $key); + if ($civifield != 'civicrm') { + continue; + } + if (preg_match('/_contact_existing$/', $key)) { + $element = $webform->getElement($key); + if (isset($element['#group'])) { + $element['#crmgroup'] = $element['#group']; + unset($element['#group']); + $webform->setElementProperties($key, $element); + } + } + if (preg_match('/_other_group$/', $key)) { + $element = $webform->getElement($key); + $new_key = str_replace('_other_group', '_other_crmgroup', $key); + $element['#form_key'] = $element['#webform_key'] = $new_key; + if (isset($element['#webform_id'])) { + $element['#webform_id'] = str_replace('_other_group', '_other_crmgroup', $element['#webform_id']); + } + if (isset($element['#webform_parents']) && is_array($element['#webform_parents'])) { + foreach ($element['#webform_parents'] as $k => $v) { + $element['#webform_parents'][$k] = str_replace('_other_group', '_other_crmgroup', $v); + } + } + if (isset($settings[$key])) { + $settings[$new_key] = $settings[$key]; + unset($settings[$key]); + } + + // Place element at the correct position. + $elements = $webform->getElementsDecoded(); + _recursiveKeyReplace($elements, $key, $new_key); + $webform->setElements($elements); + + $webform->setElementProperties($new_key, $element); + } + } + $handler->setConfiguration($config); + $webform->save(); + } +}