function Block::extractExposedValues

Extract values/operators from a given exposed form/values array element.

1 call to Block::extractExposedValues()
Block::blockSubmit in modules/ctools_views/src/Plugin/Display/Block.php
Handles form submission for the views block configuration form.

File

modules/ctools_views/src/Plugin/Display/Block.php, line 877

Class

Block
Provides a Block display plugin.

Namespace

Drupal\ctools_views\Plugin\Display

Code

protected function extractExposedValues($handler, $form_element, $values_element) {
    $configuration = [];
    $identifier = $handler->options['expose']['identifier'];
    // If this is an entity_autocomplete field, we need to pull out
    // the target_id for each value in order to match our schema.
    $element_type = $form_element[$identifier]['#type'] ?? FALSE;
    if ($element_type == 'entity_autocomplete') {
        $configuration['type'] = $element_type;
        $configuration['value'] = [];
        if (isset($values_element[$identifier]) && is_array($values_element[$identifier])) {
            $configuration['value'] = array_map(function ($item) {
                return $item['target_id'];
            }, $values_element[$identifier]);
        }
    }
    elseif ($handler->isAGroup()) {
        $is_multiple = $handler->multipleExposedInput();
        $value_key = $is_multiple ? 'default_group_multiple' : 'default_group';
        if ($is_multiple) {
            $configuration['group_info'][$value_key] = array_filter($values_element[$identifier]);
        }
        else {
            $configuration['group_info'][$value_key] = $values_element[$identifier];
        }
    }
    elseif (isset($handler->options['type']) && $handler->options['type'] === 'select' || $handler->options['plugin_id'] == 'list_field') {
        if (!is_array($values_element[$identifier])) {
            $value = $values_element[$identifier];
            // Values of 'All' should save as an empty array, like in views.
            if (!$handler->options['expose']['required'] && $value == 'All') {
                $configuration['value'] = [];
            }
            else {
                $configuration['value'] = [
                    $value => $value,
                ];
            }
        }
        else {
            $configuration['value'] = $values_element[$identifier];
        }
    }
    else {
        $configuration['value'] = $values_element[$identifier];
    }
    // Save operator, if exposed and not grouped.
    if ($handler->options['expose']['use_operator'] && !$handler->isAGroup()) {
        $operator_id = $handler->options['expose']['operator_id'];
        $configuration['operator'] = $values_element[$operator_id];
    }
    return $configuration;
}