protected function BetterExposedFiltersWidgetBase::addElementToGroup in Better Exposed Filters 8.5
Same name and namespace in other branches
- 8.4 src/Plugin/BetterExposedFiltersWidgetBase.php \Drupal\better_exposed_filters\Plugin\BetterExposedFiltersWidgetBase::addElementToGroup()
Moves an exposed form element into a field group.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: Exposed views form state.
string $element: The key of the form element.
string $group: The name of the group element.
Throws
\Drupal\Component\Plugin\Exception\PluginException If the instance cannot be created, such as if the ID is invalid.
3 calls to BetterExposedFiltersWidgetBase::addElementToGroup()
- FilterWidgetBase::exposedFormAlter in src/
Plugin/ better_exposed_filters/ filter/ FilterWidgetBase.php - Manipulate views exposed from element.
- PagerWidgetBase::exposedFormAlter in src/
Plugin/ better_exposed_filters/ pager/ PagerWidgetBase.php - Manipulate views exposed from element.
- SortWidgetBase::exposedFormAlter in src/
Plugin/ better_exposed_filters/ sort/ SortWidgetBase.php - Manipulate views exposed from element.
File
- src/
Plugin/ BetterExposedFiltersWidgetBase.php, line 140
Class
- BetterExposedFiltersWidgetBase
- Base class for Better exposed filters widget plugins.
Namespace
Drupal\better_exposed_filters\PluginCode
protected function addElementToGroup(array &$form, FormStateInterface $form_state, $element, $group) {
// Ensure group is enabled.
$form[$group]['#access'] = TRUE;
// Add element to group.
$form[$element]['#group'] = $group;
// Persist state of collapsible field-sets with active elements.
if (empty($form[$group]['#open'])) {
// Use raw user input to determine if field-set should be open or closed.
$user_input = $form_state
->getUserInput()[$element] ?? [
0,
];
// Take multiple values into account.
if (!is_array($user_input)) {
$user_input = [
$user_input,
];
}
// Check if one or more values are set for our current element.
$options = $form[$element]['#options'] ?? [];
$default_value = $form[$element]['#default_value'] ?? key($options);
$has_values = array_reduce($user_input, function ($carry, $value) use ($form, $element, $default_value) {
return $carry || ($value === $default_value ? '' : $value || $default_value === 0);
}, FALSE);
if ($has_values) {
$form[$group]['#open'] = TRUE;
}
}
}