Skip to content

Commit

Permalink
Merge pull request #895 from jitendrapurohit/public-group-select
Browse files Browse the repository at this point in the history
Add  option to enable public groups on the webform
  • Loading branch information
KarinG authored Sep 24, 2023
2 parents 0ce84d5 + 02132a7 commit 1d4d431
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/AdminForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
}
Expand Down
7 changes: 6 additions & 1 deletion src/FieldOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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", []));
Expand Down
3 changes: 3 additions & 0 deletions src/WebformCivicrmPostProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 37 additions & 1 deletion tests/src/FunctionalJavascript/GroupsTagsSubmissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down Expand Up @@ -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', [
Expand Down

0 comments on commit 1d4d431

Please sign in to comment.