You are here

public function MefibsDisplayExtender::preExecute in MEFIBS - More exposed forms in blocks 8

Set up any variables on the view prior to execution.

Overrides DisplayExtenderPluginBase::preExecute

File

lib/Drupal/mefibs/Plugin/views/display_extender/MefibsDisplayExtender.php, line 338
Contains \Drupal\mefibs\Plugin\views\display_extender\MefibsDisplayExtender.

Class

MefibsDisplayExtender
Plugin annotation @ViewsDisplayExtender( id = "mefibs", title = @Translation("Mefibs"), help = @Translation("Provides additional exposed filter blocks for this view."), no_ui = FALSE )

Namespace

Drupal\mefibs\Plugin\views\display_extender

Code

public function preExecute() {
  $view = $this->view;
  $display_id = $view->current_display;
  $display = $view->displayHandlers
    ->get($display_id);

  // Get the submitted form values
  $filters = $view
    ->getExposedInput();

  // Get old filters from the session.
  $old_filters = array();
  if (isset($_SESSION['views'][$view->storage
    ->id()][$display_id]['mefibs'])) {
    $old_filters = $_SESSION['views'][$view->storage
      ->id()][$display_id]['mefibs'];
  }

  // Get enabled blocks.
  $mefibs_blocks = $display->extender['mefibs']
    ->getEnabledBlocks();
  if (!count($mefibs_blocks)) {

    // No blocks, nothing to do.
    return;
  }

  // Find out which block has been submitted. In non-ajax context we should
  // have a filter with key mefibs_block_id, in ajax context though, this is
  // prefixed.
  $block_id = 'default';
  foreach ($filters as $filter => $value) {
    if (strpos($filter, 'mefibs_block_id') !== FALSE) {
      $block_id = $value;
      break;
    }
  }

  // Get the expected items for this block.
  $expected_items = mefibs_get_expected_items_for_exposed_form_block($view, $block_id);

  // Select the final filters we want to apply according to the submitted
  // block.
  $valid_filters = array();
  $block_id_clean = str_replace('_', '-', $block_id);
  foreach ($filters as $filter => $value) {
    if ($block_id != 'default') {

      // One of the additional forms has been used.
      if (strpos($filter, MEFIBS_VIEW_DISPLAY_PREFIX . '-' . $block_id_clean . '-') === FALSE) {
        continue;
      }
    }

    // In Ajax context the filter names will be in the form
    // 'mefibs-form-block_id-filter_name, in non-ajax context the filter
    // names are simply the original filter names. So replaceing the form
    // prefix and the block id with nothing should work in all cases.
    $filter_name = str_replace(MEFIBS_VIEW_DISPLAY_PREFIX . '-' . $block_id_clean . '-', '', $filter);

    // Check filters and extra stuff like items_per_page and offset.
    foreach (array(
      'filter',
      'other',
    ) as $type) {
      if ($type != 'filter' && in_array($filter_name, $expected_items[$type]) || $type == 'filter' && isset($expected_items[$type][$filter_name])) {

        // This is an expected argument.
        $valid_filters[$filter_name] = $value;
      }
    }

    // Check sort options.
    if ($filter_name == 'sort_by' && count($expected_items['sort']) && in_array($value, $expected_items['sort'])) {
      $valid_filters['sort_by'] = $value;
    }
    if ($filter_name == 'sort_order' && count($expected_items['sort'])) {
      $valid_filters['sort_order'] = $value;
    }
  }

  // Unset all old filters of expected items that should have been submitted if
  // they had been set.
  foreach ($expected_items as $type => $items) {
    foreach ($items as $key => $item) {
      if (isset($old_filters[$key])) {
        unset($old_filters[$key]);
      }

      // This is important for things like sort and items per page.
      if (isset($_GET[$key])) {
        unset($_GET[$key]);
      }
    }
  }

  // Fill in values from previous query.
  $filters = $valid_filters + $old_filters;

  // Allow other modules to alter the filter values.
  drupal_alter('mefibs_filter', $filters);

  // Pass the filters on to the view.
  $view
    ->setExposedInput($filters);

  // And save them in the session for later reference.
  $_SESSION['views'][$view->storage
    ->id()][$display_id]['mefibs'] = $filters;

  // Support for exposed items per page.
  if (isset($filters['items_per_page'])) {
    $view
      ->setItemsPerPage($filters['items_per_page']);
  }
  if (isset($filters['offset'])) {
    $view
      ->setOffset($filters['offset']);
  }
}