Skip to content

Commit

Permalink
Feature/sd 561 select field searchable checkbox (#567)
Browse files Browse the repository at this point in the history
* Added enable search checkbox for webform element forms select type

* Added enable search button to webform ui element form select term select fields

* Improvements for new elements

---------

Co-authored-by: sharmasahil <[email protected]>
Co-authored-by: Yeni Atencio <[email protected]>
Co-authored-by: vincent-gao <[email protected]>
  • Loading branch information
4 people authored Dec 30, 2024
1 parent 490073f commit beee018
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions modules/tide_webform/tide_webform.module
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,36 @@ function tide_webform_form_alter(&$form, FormStateInterface $form_state, $form_i
'255');
}
}
// Check if the field type is either 'select' or 'term_select'.
$field_type = $form['properties']['type']['#value'] ?? '';
if (in_array($field_type, ['select', 'webform_term_select'])) {
$current_path = \Drupal::service('path.current')->getPath();
$matches = [];
if (preg_match('/webform\/manage\/([^\/]+)\/element\/([^\/]+)/', $current_path, $matches)) {
// Extract the Parent Webform ID.
$webform_id = $matches[1];
}
// Fetch already saved default value from config.
$element_key = $form['properties']['element']['key']['#default_value'];
$element_settings = \Drupal::configFactory()->get('webform.settings')->get('element_settings') ?: [];
if (isset($element_key) && isset($element_settings[$webform_id][$element_key]['searchable'])) {
// Get the 'searchable' value for the specified element.
$searchable_value = $element_settings[$webform_id][$element_key]['searchable'];
}
else {
$searchable_value = 0;
}
$form['properties']['element']['searchable'] = [
'#type' => 'checkbox',
'#title' => t('Enable search'),
'#description' => t('Check this box to allow select fields to opt into the searchable display.'),
'#default_value' => $searchable_value,
];

$form['#submit'][] = 'tide_webform_webform_ui_element_form_submit';
$form['#webform_id'] = $form_id;
$form['#parent_id'] = $webform_id;
}
}

if ($form_id == 'webform_submission_filter_form') {
Expand Down Expand Up @@ -102,6 +132,43 @@ function tide_webform_form_alter(&$form, FormStateInterface $form_state, $form_i

}

/**
* Submit callback.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*/
function tide_webform_webform_ui_element_form_submit(array $form, FormStateInterface $form_state) {

// Get the searchable value.
$searchable_value = $form_state->getValue(['searchable']) ?? 0;
// Fetch key from from_state for new element.
$key_new = $form_state->getValue(['key']) ?? '';
$element_key = $form['properties']['element']['key']['#default_value'] ?? $key_new;
$element_settings = \Drupal::configFactory()->getEditable('webform.settings')->get('element_settings') ?: [];
// Save the 'searchable' value to the element settings.
$element_settings[$form['#parent_id']][$element_key]['searchable'] = $searchable_value;

// Save the updated element settings back to the configuration.
\Drupal::configFactory()->getEditable('webform.settings')
->set('element_settings', $element_settings)
->save();

// Explicitly add elemet to parent webform.
$webform = Webform::load($form['#parent_id']);
// Get all elements.
$elements = $webform->getElementsDecoded();
// Check if element exists and its type is 'select' or 'webform_term_select'.
if (isset($elements[$element_key]) && in_array($elements[$element_key]['#type'], ['select', 'webform_term_select'])) {
$elements[$element_key]['#searchable'] = $searchable_value;
// Update the Webform's elements.
$webform->setElements($elements);
$webform->save();
}
}

/**
* After_build callback.
*
Expand Down

0 comments on commit beee018

Please sign in to comment.