public function ContextualRangeFilterAssignmentForm::submitForm in Views Contextual Range Filter 8
Submit the form.
Overrides ConfigFormBase::submitForm
File
- src/
Form/ ContextualRangeFilterAssignmentForm.php, line 142
Class
- ContextualRangeFilterAssignmentForm
- Convert selected contextual filters to contextual range filters.
Namespace
Drupal\contextual_range_filter\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->configFactory
->getEditable('contextual_range_filter.settings');
foreach ([
'numeric',
'string',
'date',
] as $type) {
$field_names = $type . '_field_names';
// Clear out the unticked boxes.
$filters = array_filter($form_state
->getValue($field_names));
$saved_filters = $config
->get($field_names) ?: [];
$added_filters = array_diff($filters, $saved_filters);
$removed_filters = array_diff($saved_filters, $filters);
$changed_filters = array_merge($added_filters, $removed_filters);
if (empty($changed_filters)) {
continue;
}
$config
->set($field_names, $filters);
// Find corresponding Views, (un)set the (range) filter and save them.
// $form['#view_names'][node__field_price:field_price_value] is an array.
$changed_view_names = [];
foreach ($changed_filters as $filter_name) {
if (!empty($form['#view_names'][$filter_name])) {
foreach ($form['#view_names'][$filter_name] as $view_names) {
foreach ($view_names as $view_name) {
if (!in_array($view_name, $changed_view_names)) {
$changed_view_names[] = $view_name;
}
}
}
}
}
// We cycle through all the views. If the view is flagged as needing to be
// edited, we check if any of the changed filters is present in that view.
// If we find one, we (re)set the plugin id accordingly.
$range_type = $type . '_range';
foreach (Views::getAllViews() as $view) {
$view_name = $view
->get('label');
if (in_array($view_name, $changed_view_names)) {
$display =& $view
->getDisplay('default');
foreach ($changed_filters as $filter_name) {
$field_name = substr($filter_name, strpos($filter_name, ":") + 1);
if (isset($display['display_options']['arguments'][$field_name]['plugin_id'])) {
$plugin_id = in_array($filter_name, $added_filters) ? $range_type : $type;
$display['display_options']['arguments'][$field_name]['plugin_id'] = $plugin_id;
}
$this
->messenger()
->addStatus($this
->t('Updated contextual filter(s) on view %view_name.', [
'%view_name' => $view_name,
]));
$view
->save();
}
}
}
}
$config
->save();
// We now need to invoke contextual_range_filter_views_data_alter() for the
// changes to take effect. We do this by clearing the caches.
drupal_flush_all_caches();
parent::submitForm($form, $form_state);
}