You are here

function mefibs_exposed_block_form in MEFIBS - More exposed forms in blocks 7

Form builder for the additional exposed form.

Parameters

object $view: A fully loaded views object.

string $display_id: Display id of the view that the additonal filter form is associated to.

Return value

array Form API array.

1 call to mefibs_exposed_block_form()
mefibs_block_view in ./mefibs.module
Implements hook_block_view().

File

./mefibs.module, line 607
Primarily Drupal hooks and global API functions to manipulate views and to provide an additional block with an exposed filter form.

Code

function mefibs_exposed_block_form($view, $block_id) {
  $rendered =& drupal_static(__FUNCTION__);
  $display_id = $view->current_display;
  $display = $view->display[$display_id]->handler;
  $static_key = $view->name . '-' . $display_id . '-' . $block_id;
  if (isset($rendered[$static_key])) {
    return;
  }
  $rendered[$static_key] = TRUE;
  if (!mefibs_display_is_mefibs_enabled($view->display[$display_id]->handler)) {
    return;
  }

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

  // Add necessary info to JS settings.
  drupal_add_js(array(
    'mefibs' => array(
      'forms' => array(
        $block_id => array(
          'view_name' => $view->name,
          'view_display_id' => $display_id,
          'form_prefix' => mefibs_get_element_name_prefix($block_id),
        ),
      ),
    ),
  ), 'setting');

  // Inspired by the code in views_plugin_exposed_form::render_exposed_form().
  $exposed_form = $display
    ->get_plugin('exposed_form');

  // Deal with any exposed filters we may have, before building.
  $form_state = array(
    'view' => &$exposed_form->view,
    'display' => &$exposed_form->display,
    'method' => 'get',
    'rerender' => TRUE,
    'no_redirect' => TRUE,
    'always_process' => TRUE,
    'exposed_form_override' => TRUE,
    '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
    ->displays_exposed()) {
    unset($form_state['rerender']);
  }
  if (!empty($exposed_form->ajax)) {
    $form_state['ajax'] = TRUE;
  }

  // Retrieve the bare form.
  $form_state['exposed_form_plugin'] = $exposed_form;
  $form = drupal_build_form('views_exposed_form', $form_state);
  $mefibs = $display->extender['mefibs_blocks'];
  $block_settings = $mefibs
    ->get_block_options($block_id);
  $form['submit']['#value'] = $block_settings['submit_button'];
  if ($block_settings['reset_button']) {
    if (!isset($form['reset'])) {
      $form['reset'] = array(
        '#type' => 'submit',
        '#value' => $block_settings['reset_button_label'],
        '#weight' => 10,
        '#access' => TRUE,
      );
    }
    else {
      $form['reset']['#value'] = $block_settings['reset_button_label'];
    }
  }
  if (!$block_settings['reset_button']) {
    $form['reset']['#access'] = FALSE;
  }

  // Apply autosubmit values.
  if (!empty($block_settings['autosubmit'])) {
    $form = array_merge_recursive($form, array(
      '#attributes' => array(
        'class' => array(
          'ctools-auto-submit-full-form',
        ),
      ),
    ));
    $form['submit']['#attributes']['class'][] = 'ctools-use-ajax';
    $form['submit']['#attributes']['class'][] = 'ctools-auto-submit-click';
    $form['#attached']['js'][] = drupal_get_path('module', 'ctools') . '/js/auto-submit.js';

    // As this is a copy of the original form, it is possible that the classes
    // are already there. We want uniqueness.
    array_unique($form['submit']['#attributes']['class']);
    if (!empty($block_settings['autosubmit_hide'])) {
      $form['submit']['#attributes']['class'][] = 'js-hide';
    }
    else {
      mefibs_remove_value_from_array($form['submit']['#attributes']['class'], 'js-hide');
    }
  }
  else {

    // Remove autosubmit classes if set in the original form, otherwhise the
    // submit button won't show up.
    if (isset($form['submit']['#attributes']['class'])) {
      mefibs_remove_value_from_array($form['submit']['#attributes']['class'], array(
        'ctools-use-ajax',
        'ctools-auto-submit-click',
        'js-hide',
      ));
    }
  }

  // This is important! Change the form id, otherwhise strange things are going
  // to happen.
  $form['#id'] = $form['#id'] . '-' . mefibs_get_element_name_prefix($block_id);
  mefibs_set_form_id_recursive($form, mefibs_get_element_name_prefix($block_id));
  mefibs_set_element_name_recursive($form, mefibs_get_element_name_prefix($block_id));
  return $form;
}