You are here

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

Render a mefibs form.

This is essentially a pimped version of ExposedFormPluginBase::renderExposedForm().

File

lib/Drupal/mefibs/Plugin/views/display_extender/MefibsDisplayExtender.php, line 449
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 renderExposedForm($block_id) {
  $js_settings =& drupal_static(__FUNCTION__);
  $view = $this->view;

  // return;
  $display_id = $view->current_display;
  $display = $view->displayHandlers
    ->get($display_id);
  if (!mefibs_display_is_mefibs_enabled($display)) {
    return;
  }

  // Our custom form prefix, used to alter the form ids.
  $form_prefix = MEFIBS_VIEW_DISPLAY_PREFIX . '-' . str_replace('_', '-', $block_id);

  // Add javascript needed for ajax form behaviors.
  drupal_add_js(drupal_get_path('module', 'mefibs') . '/js/mefibs.js');

  // Add necessary info to JS settings.
  if (!isset($js_settings['mefibs'])) {
    $js_settings = array(
      'mefibs' => array(
        'forms' => array(),
      ),
    );
  }
  $js_settings['mefibs']['forms'][] = array(
    'view_name' => $this->view->storage
      ->id(),
    'view_display_id' => $display_id,
    'form_prefix' => $form_prefix,
  );
  drupal_add_js($js_settings, 'setting');

  // We need to initiate the handlers otherwhise there are a lot of warnings
  // thrown in views_exposed_form_validate() and views_exposed_form_submit().
  $this->view
    ->initHandlers();

  // Deal with any exposed filters we may have, before building.
  $form_state = array(
    'view' => &$this->view,
    'display' => &$this->view->display_handler->display,
    'method' => 'get',
    'rerender' => TRUE,
    'no_redirect' => TRUE,
    'always_process' => TRUE,
    'exposed_form_override' => TRUE,
    // Custom property.
    'mefibs_block_id' => $block_id,
  );

  // Some types of displays (eg. attachments) may wish to use the exposed
  // filters of their parent displays instead of showing an additional
  // exposed filter form for the attachment as well as that for the parent.
  if (!$display
    ->displaysExposed()) {
    unset($form_state['rerender']);
  }
  if (!empty($this->ajax)) {
    $form_state['ajax'] = TRUE;
  }
  $form_state['exposed_form_plugin'] = $display
    ->getPlugin('exposed_form');
  $form = drupal_build_form('views_exposed_form', $form_state);

  // This is important! Change the form id, otherwhise strange things are
  // going to happen.
  $form['#id'] = $form['#id'] . '-' . $form_prefix;
  mefibs_set_form_id_recursive($form, MEFIBS_VIEW_DISPLAY_PREFIX . '-' . $form_prefix);
  if (!$display
    ->displaysExposed()) {
    return array();
  }
  else {
    return $form;
  }
}