You are here

function MefibsDisplayExtender::hideExposedFormItems in MEFIBS - More exposed forms in blocks 8

Hide form elements that should not show up for the given block id.

File

lib/Drupal/mefibs/Plugin/views/display_extender/MefibsDisplayExtender.php, line 535
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

function hideExposedFormItems(&$form, $block_id) {
  $elements = mefibs_get_expected_items_for_exposed_form_block($this->view, $block_id);
  $display_id = $this->view->current_display;
  $display = $this->view->displayHandlers
    ->get($display_id);
  $form_keys = array();
  foreach ($form['#info'] as $key => $definition) {
    list($type, $item) = explode('-', $key);
    if (in_array($item, $elements[$type])) {
      if (!isset($form['#info']['filter-' . $item]['value'])) {
        $key = $item;
      }
      else {
        $key = $form['#info']['filter-' . $item]['value'];
      }
      $form_keys[] = $key;
      if (isset($definition['operator']) && !empty($definition['operator'])) {
        $form_keys[] = $definition['operator'];
      }
    }
  }
  $mefibs_options = $display
    ->getOption('mefibs');
  if (count($elements['sort']) && $mefibs_options[$display_id]['sort_block'] == $block_id) {
    $form_keys[] = 'sort_by';
    $form_keys[] = 'sort_order';
  }
  if (count($elements['other'])) {
    $form_keys = array_merge($form_keys, $elements['other']);
  }
  $form_keys = array_merge($form_keys, array(
    'actions',
    'form_build_id',
    'form_id',
    'form_token',
  ));
  $context = array(
    'view' => clone $this->view,
    'display_id' => $display_id,
    'block_id' => $block_id,
    'type' => 'hide_items',
  );
  drupal_alter('mefibs_elements', $form_keys, $context);

  // Do some magic: hide all other elements.
  $prefix = '<div style="display: none;">';
  $suffix = '</div>';
  mefibs_set_form_property_recursive($form, 'prefix', $prefix, $form_keys);
  mefibs_set_form_property_recursive($form, 'suffix', $suffix, $form_keys);

  // Also hide the labels of hidden filters.
  $mefibs_options = $display
    ->getOption('mefibs');
  foreach ($form['#info'] as $id => $info) {
    list($type, $item) = explode('-', $id);
    if (!isset($mefibs_options[$display_id][$type])) {
      $form['#info'][$id]['label'] = '';
      continue;
    }
    if (isset($mefibs_options[$display_id][$type][$item]) && $block_id != $mefibs_options[$display_id][$type][$item] || !isset($mefibs_options[$display_id][$type][$item]) && $block_id != 'default') {
      $form['#info'][$id]['label'] = '';
    }
  }
}