function Block::preBlockBuild

Same name in other branches
  1. 8.x-3.x modules/ctools_views/src/Plugin/Display/Block.php \Drupal\ctools_views\Plugin\Display\Block::preBlockBuild()

Overrides Block::preBlockBuild

File

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

Class

Block
Provides a Block display plugin.

Namespace

Drupal\ctools_views\Plugin\Display

Code

public function preBlockBuild(ViewsBlock $block) {
    parent::preBlockBuild($block);
    $allow_settings = array_filter($this->getOption('allow'));
    $config = $block->getConfiguration();
    [
        ,
        $display_id,
    ] = explode('-', $block->getDerivativeId(), 2);
    // Change pager offset settings based on block configuration.
    if (!empty($allow_settings['offset']) && isset($config['pager_offset'])) {
        $this->view
            ->setOffset($config['pager_offset']);
    }
    // Change pager style settings based on block configuration.
    if (!empty($allow_settings['pager'])) {
        $pager = $this->view->display_handler
            ->getOption('pager');
        if (!empty($config['pager']) && $config['pager'] != 'view') {
            $pager['type'] = $config['pager'];
        }
        $this->view->display_handler
            ->setOption('pager', $pager);
    }
    // Change fields output based on block configuration.
    if (!empty($allow_settings['hide_fields']) || !empty($allow_settings['sort_fields'])) {
        if (!empty($config['fields']) && $this->view
            ->getStyle()
            ->usesFields()) {
            $fields = $this->view
                ->getHandlers('field');
            uasort($config['fields'], '\\Drupal\\ctools_views\\Plugin\\Display\\Block::sortFieldsByWeight');
            $iterate_fields = !empty($allow_settings['sort_fields']) ? $config['fields'] : $fields;
            foreach (array_keys($iterate_fields) as $field_name) {
                // Remove each field in sequence and re-add them to sort
                // appropriately or hide if disabled.
                $this->view
                    ->removeHandler($display_id, 'field', $field_name);
                if (empty($allow_settings['hide_fields']) || !empty($allow_settings['hide_fields']) && empty($config['fields'][$field_name]['hide'])) {
                    $this->view
                        ->addHandler($display_id, 'field', $fields[$field_name]['table'], $fields[$field_name]['field'], $fields[$field_name], $field_name);
                }
            }
        }
    }
    // Change filters output based on block configuration.
    if (!empty($allow_settings['disable_filters'])) {
        $filters = $this->view
            ->getHandlers('filter', $display_id);
        foreach ($filters as $filter_name => $filter) {
            // If we allow disabled filters and this filter is disabled, disable it
            // and continue.
            if (!empty($allow_settings['disable_filters']) && !empty($config["filter"][$filter_name]['disable'])) {
                $this->view
                    ->removeHandler($display_id, 'filter', $filter_name);
                continue;
            }
        }
    }
    // Change sorts based on block configuration.
    if (!empty($allow_settings['configure_sorts'])) {
        $sorts = $this->view
            ->getHandlers('sort', $display_id);
        foreach ($sorts as $sort_name => $sort) {
            if (!empty($config["sort"][$sort_name])) {
                $sort['order'] = $config["sort"][$sort_name];
                $this->view
                    ->setHandler($display_id, 'sort', $sort_name, $sort);
            }
        }
    }
}