Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to enable public groups on the webform #895

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/AdminForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,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 @@ -1918,7 +1921,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 @@ -2194,7 +2199,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 @@ -2514,6 +2514,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