function Block::elementPreRender

Same name in other branches
  1. 4.0.x modules/ctools_views/src/Plugin/Display/Block.php \Drupal\ctools_views\Plugin\Display\Block::elementPreRender()

Exposed widgets.

Exposed widgets typically only work with ajax in Drupal core, however #2605218 totally breaks the rest of the functionality in this display and in Core's Block display as well, so we allow non-ajax block views to use exposed filters and manually set the #action to the current request uri.

Overrides DisplayPluginBase::elementPreRender

File

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

Class

Block
Provides a Block display plugin.

Namespace

Drupal\ctools_views\Plugin\Display

Code

public function elementPreRender(array $element) {
    
    /** @var \Drupal\views\ViewExecutable $view */
    $view = $element['#view'];
    // Exposed widgets typically only work with Ajax in core, but #2605218
    // breaks the rest of the functionality in this display and in the core
    // Block display as well. We allow non-Ajax block views to use exposed
    // filters by manually setting the #action to the current request URI.
    if (!empty($view->exposed_widgets['#action']) && !$view->ajaxEnabled()) {
        $view->exposed_widgets['#action'] = $this->request
            ->getRequestUri();
    }
    // Allow the parent pre-render function to set the #exposed array on the
    // element. This allows us to bypass hiding widgets if the array is emptied.
    $element = parent::elementPreRender($element);
    // Loop over the filters on the current View looking for exposed filters
    // whose values have been derived from block configuration.
    if (!empty($element['#exposed'])) {
        $allow_settings = array_filter($this->getOption('allow'));
        if (!empty($allow_settings['configure_filters'])) {
            foreach ($view->getDisplay()
                ->getHandlers('filter') as $id => $handler) {
                
                /** @var \Drupal\views\Plugin\views\Filter\FilterPluginBase $handler */
                // If the current handler meets the conditions, hide its exposed
                // widget.
                if ($handler->canExpose() && $handler->isExposed()) {
                    $value_exposed = $handler->options['value_exposed_to_user'] ?? FALSE;
                    $operator_exposed = $handler->options['operator_exposed_to_user'] ?? FALSE;
                    if ($handler->isAGroup()) {
                        $identifier = $handler->options['group_info']['identifier'];
                        $operator_id = FALSE;
                    }
                    else {
                        $identifier = $handler->options['expose']['identifier'] ?? FALSE;
                        $operator_id = $handler->options['expose']['use_operator'] ? $handler->options['expose']['operator_id'] : FALSE;
                    }
                    // If a wrapper is being used, store its key for later use.
                    $wrapper = !empty($element['#exposed'][$identifier . '_wrapper']) ? $identifier . '_wrapper' : FALSE;
                    if ($wrapper) {
                        $element['#exposed'][$wrapper][$identifier]['#access'] = $value_exposed;
                        if ($operator_id) {
                            $element['#exposed'][$wrapper][$operator_id]['#access'] = $operator_exposed;
                        }
                        if (!$value_exposed && !$operator_exposed) {
                            $element['#exposed'][$wrapper]['#access'] = FALSE;
                        }
                    }
                    else {
                        $element['#exposed'][$identifier]['#access'] = $value_exposed;
                        if ($operator_id) {
                            $element['#exposed'][$operator_id]['#access'] = $operator_exposed;
                        }
                    }
                }
            }
            // If there are no accessible child elements in the #exposed array other
            // than the actions, reset it to an empty array.
            if (Element::getVisibleChildren($element['#exposed']) == [
                'actions',
            ]) {
                $element['#exposed'] = [];
            }
        }
    }
    return $element;
}