Skip to content

Instantly share code, notes, and snippets.

@jitendrapurohit
Created January 27, 2021 11:48
Show Gist options
  • Save jitendrapurohit/dfcf9fb2cc8e5ab1fa29b7e959353a4b to your computer and use it in GitHub Desktop.
Save jitendrapurohit/dfcf9fb2cc8e5ab1fa29b7e959353a4b to your computer and use it in GitHub Desktop.
diff --git a/src/AdminForm.php b/src/AdminForm.php
index 8988231..e7cbefe 100644
--- a/src/AdminForm.php
+++ b/src/AdminForm.php
@@ -1279,10 +1279,13 @@ class AdminForm implements AdminFormInterface {
'#group' => 'webform_civicrm',
'#title' => t('Additional Options'),
'#attributes' => array('class' => array('civi-icon-prefs')),
- '#description' => '<p>' .
- t('To have this form auto-filled for anonymous users, enable the "Existing Contact" field for :contact and send the following link from CiviMail:', array(':contact' => $utils->wf_crm_contact_label(1, $this->data, TRUE))) .
- '<br /><code>' . Url::fromRoute('entity.webform.canonical', ['webform' => $this->webform->id()], ['query' => ['cid1' => '']])->toString() . '{contact.contact_id}&amp;{contact.checksum}</code></p>',
);
+ $this->form['options']['checksum_text'] = [
+ '#type' => 'item',
+ '#markup' => '<p>' .
+ t('To have this form auto-filled for anonymous users, enable the "Existing Contact" field for :contact and send the following link from CiviMail:', array(':contact' => $utils->wf_crm_contact_label(1, $this->data, 'escape'))) .
+ '<br /><code>' . Url::fromRoute('entity.webform.canonical', ['webform' => $this->webform->id()], ['query' => ['cid1' => '']])->toString() . '{contact.contact_id}&amp;{contact.checksum}</code></p>',
+ ];
$this->form['options']['create_fieldsets'] = array(
'#type' => 'checkbox',
'#title' => t('Create Fieldsets'),
@@ -1465,6 +1468,9 @@ class AdminForm implements AdminFormInterface {
if ($field['type'] != 'hidden') {
$options += array('create_civicrm_webform_element' => t('- User Select -'));
}
+ if ($name == 'group') {
+ $options += ['public_groups' => t('- Only Public Groups -')];
+ }
$options += $utils->wf_crm_field_options($field, 'config_form', $this->data);
$item += array(
'#type' => 'select',
@@ -1824,7 +1830,9 @@ class AdminForm implements AdminFormInterface {
$field = $utils->wf_crm_get_field($key);
if (!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]);
@@ -2031,7 +2039,7 @@ class AdminForm implements AdminFormInterface {
// 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/Element/CivicrmSelectOptions.php b/src/Element/CivicrmSelectOptions.php
index dd37396..a8dc288 100644
--- a/src/Element/CivicrmSelectOptions.php
+++ b/src/Element/CivicrmSelectOptions.php
@@ -46,10 +46,10 @@ class CivicrmSelectOptions extends FormElement {
}
}
- protected static function getFieldOptions($form_key) {
+ protected static function getFieldOptions($form_key, $data = []) {
\Drupal::getContainer()->get('civicrm')->initialize();
$field_options = \Drupal::service('webform_civicrm.field_options');
- return $field_options->get(['form_key' => $form_key], 'create', []);
+ return $field_options->get(['form_key' => $form_key], 'create', $data);
}
public static function processSelectOptions(&$element, FormStateInterface $form_state, &$complete_form) {
@@ -105,7 +105,9 @@ class CivicrmSelectOptions extends FormElement {
$current_options = $element['#default_value'];
$weight = 0;
- $field_options = static::getFieldOptions($element['#form_key']);
+ $webform = $form_state->getFormObject()->getWebform();
+ $data = $webform->getHandler('webform_civicrm')->getConfiguration()['settings']['data'] ?? [];
+ $field_options = static::getFieldOptions($element['#form_key'], $data);
// Sort the field options by the current options.
if (!$element['#live_options']) {
@@ -178,8 +180,10 @@ class CivicrmSelectOptions extends FormElement {
* Validates webform options element.
*/
public static function validateSelectOptions(&$element, FormStateInterface $form_state, &$complete_form) {
+ $webform = $form_state->getFormObject()->getWebform();
+ $data = $webform->getHandler('webform_civicrm')->getConfiguration()['settings']['data'] ?? [];
if ($element['#live_options']) {
- $options_value = self::getFieldOptions($element['#form_key']);
+ $options_value = self::getFieldOptions($element['#form_key'], $data);
}
else {
$raw_values = $form_state->getValue($element['options']['#parents']);
diff --git a/src/FieldOptions.php b/src/FieldOptions.php
index 52d7c68..97cf6bc 100644
--- a/src/FieldOptions.php
+++ b/src/FieldOptions.php
@@ -64,7 +64,12 @@ class FieldOptions implements FieldOptionsInterface {
$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', array('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", array()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment