You are here

function facetapi_multiselect_form in Facetapi Multiselect 7

Form which displays facets as a multiselect element.

Parameters

$widget: The widget object (e.g. of class FacetapiMultiSelectWidget) used to build the facet.

$element: The facet element, containing information about each available facet in the group.

1 string reference to 'facetapi_multiselect_form'
facetapi_multiselect_forms in ./facetapi_multiselect.module
Implements hook_forms().

File

./facetapi_multiselect.module, line 46
Displays search facets as a multiselect widget.

Code

function facetapi_multiselect_form($form, &$form_state, $widget, $element) {

  // Record the widget and facet name in an easy-to-access part of the
  // $form_state array. Also record a class that can be used to access the
  // widget in front-end code.
  $facet_name = $widget
    ->getKey();
  $form_state['facetapi_multiselect'] = array(
    'widget' => $widget,
    'facet_name' => $facet_name,
    'facet_class' => drupal_html_class("facetapi-multiselect-{$facet_name}"),
  );

  // Record this in the form too, in case someone needs to access it there.
  $form['#facetapi_multiselect'] = $form_state['facetapi_multiselect'];

  // Get an array of the facet options
  $options = $widget
    ->buildOptions($element);

  // Because of an issue with iOS and multiselect fields, we need to add an
  // empty option. This option must have the `disabled` attribute, so  we'll
  // handle that in preprocess.
  // @see http://stackoverflow.com/questions/34985606/ios-9-2-select-list-multiple-incorrectly-firing-change-event-and-setting-valu
  $options = array(
    'disabled' => '',
  ) + $options;

  // Build the form.
  $form['facets'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#options' => $options,
    '#default_value' => $widget
      ->buildDefaultValue($element),
    '#attributes' => array(
      'class' => array(
        'facetapi-multiselect',
        $form_state['facetapi_multiselect']['facet_class'],
      ),
    ),
    '#post_render' => array(
      '_facetapi_multiselect_disable_options',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  $build = $widget
    ->getBuild();
  $settings = $build['#settings']->settings;
  if ($settings['auto_submit']) {
    $form['#attached']['js'][] = drupal_get_path('module', 'facetapi_multiselect') . '/js/facetapi_multiselect_autosubmit.js';
  }
  if ($settings['placeholder_label']) {
    $form['facets']['#attributes']['data-placeholder'] = t($settings['placeholder_label']);
  }
  return $form;
}