diff --git a/src/AdminForm.php b/src/AdminForm.php index 1d9ec8929..f0fbecb7d 100644 --- a/src/AdminForm.php +++ b/src/AdminForm.php @@ -1554,6 +1554,9 @@ private function addItem($fid, $field) { if ($field['type'] != 'hidden') { $options += ['create_civicrm_webform_element' => t('- User Select -')]; } + if ($name == 'group') { + $options += ['public_groups' => t('- User Select - (public groups)')]; + } $options += $this->utils->wf_crm_field_options($field, 'config_form', $this->data); $item += [ '#type' => 'select', @@ -1924,7 +1927,9 @@ public function postProcess() { } elseif (!isset($enabled[$key])) { $val = (array) $val; - if (in_array('create_civicrm_webform_element', $val, TRUE) || (!empty($val[0]) && $field['type'] == 'hidden')) { + 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))) { // Restore disabled component if (isset($disabled[$key])) { webform_component_update($disabled[$key]); @@ -2200,7 +2205,7 @@ private function getFieldsToDelete($fields) { // Find fields to delete foreach ($fields as $key => $val) { $val = (array) wf_crm_aval($this->settings, $key); - if (((in_array('create_civicrm_webform_element', $val, TRUE)) && $this->settings['nid']) + if (((in_array('create_civicrm_webform_element', $val, TRUE) || in_array('public_groups', $val, TRUE)) && $this->settings['nid']) || strpos($key, 'fieldset') !== FALSE) { unset($fields[$key]); } diff --git a/src/FieldOptions.php b/src/FieldOptions.php index 7c6f2b61d..d91140cbd 100644 --- a/src/FieldOptions.php +++ b/src/FieldOptions.php @@ -67,7 +67,12 @@ public function get($field, $context, $data) { $ret = $utils->wf_crm_get_tags($ent, wf_crm_aval($split, 1)); } elseif (isset($field['table']) && $field['table'] === 'group') { - $ret = $utils->wf_crm_apivalues('group', 'get', ['is_hidden' => 0], 'title'); + $params = ['is_hidden' => 0]; + $options = wf_crm_aval($data, "contact:$c:other:1:group"); + if (!empty($options) && !empty($options['public_groups'])) { + $params['visibility'] = "Public Pages"; + } + $ret = $utils->wf_crm_apivalues('group', 'get', $params, 'title'); } elseif ($name === 'survey_id') { $ret = $utils->wf_crm_get_surveys(wf_crm_aval($data, "activity:$c:activity:1", [])); diff --git a/src/WebformCivicrmPostProcess.php b/src/WebformCivicrmPostProcess.php index d71593933..c1018a41c 100644 --- a/src/WebformCivicrmPostProcess.php +++ b/src/WebformCivicrmPostProcess.php @@ -2515,6 +2515,9 @@ private function fillDataFromSubmission() { } if (substr($name, 0, 6) === 'custom' || ($table == 'other' && in_array($name, ['group', 'tag']))) { $val = array_filter($val); + if ($name === 'group') { + unset($val['public_groups']); + } } // We need to handle items being de-selected too and provide an array to pass to Entity.create API diff --git a/tests/src/FunctionalJavascript/GroupsTagsSubmissionTest.php b/tests/src/FunctionalJavascript/GroupsTagsSubmissionTest.php index d4757d390..710564cd6 100644 --- a/tests/src/FunctionalJavascript/GroupsTagsSubmissionTest.php +++ b/tests/src/FunctionalJavascript/GroupsTagsSubmissionTest.php @@ -23,6 +23,42 @@ protected function setUp(): void { } } + /** + * Test the display of public groups on webform. + */ + public function testPublicGroups() { + // Make GroupA and GroupB as public + $this->utils->wf_civicrm_api('Group', 'create', [ + 'id' => $this->groups['GroupA'], + 'visibility' => "Public Pages", + ]); + $this->utils->wf_civicrm_api('Group', 'create', [ + 'id' => $this->groups['GroupB'], + 'visibility' => "Public Pages", + ]); + + $this->drupalLogin($this->rootUser); + $this->drupalGet(Url::fromRoute('entity.webform.civicrm', [ + 'webform' => $this->webform->id(), + ])); + $this->enableCivicrmOnWebform(); + + // 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->htmlOutput(); + $this->saveCiviCRMSettings(); + + // Visit the form. + $this->drupalGet($this->webform->toUrl('canonical')); + $this->assertPageNoErrorMessages(); + + $this->assertSession()->pageTextContains('GroupA'); + $this->assertSession()->pageTextContains('GroupB'); + $this->assertSession()->pageTextNotContains('GroupC'); + } + public function testSubmitWebform() { $this->drupalLogin($this->rootUser); $this->drupalGet(Url::fromRoute('entity.webform.civicrm', [ @@ -54,7 +90,7 @@ public function testSubmitWebform() { $this->drupalGet($this->webform->toUrl('edit-form')); $this->htmlOutput(); - //Change type of group field to checkbox. + // Change type of group field to checkbox. $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', [