You are here

class FacetapiAjaxWidgetCheckboxes in Ajax facets 7

Same name and namespace in other branches
  1. 7.3 plugins/facetapi/ajax_widget_checkboxes.inc \FacetapiAjaxWidgetCheckboxes
  2. 7.2 plugins/facetapi/ajax_widget_checkboxes.inc \FacetapiAjaxWidgetCheckboxes

Widget that renders facets as a list of clickable links.

Links make it easy for users to narrow down their search results by clicking on them. The render arrays use theme_item_list() to generate the HTML markup.

Hierarchy

Expanded class hierarchy of FacetapiAjaxWidgetCheckboxes

1 string reference to 'FacetapiAjaxWidgetCheckboxes'
ajax_facets_facetapi_widgets in ./ajax_facets.module
Implements hook_facetapi_widgets().

File

plugins/facetapi/ajax_widget_checkboxes.inc, line 14
The facetapi_links and facetapi_checkbox_links widget plugin classes.

View source
class FacetapiAjaxWidgetCheckboxes extends FacetapiWidgetCheckboxLinks {
  public $update_result;

  /**
   * Overrides FacetapiWidgetCheckboxLinks::init().
   */
  public function init() {
    parent::init();
    ajax_facets_add_ajax_js($this->facet);
  }

  /**
   * Overrides FacetapiWidget::settingsForm().
   */
  function settingsForm(&$form, &$form_state) {
    $form['widget']['widget_settings']['links'][$this->id]['index_id'] = array(
      '#type' => 'value',
      '#title' => t('Index id'),
      '#default_value' => $form['#facetapi']['facet']['map options']['index id'],
      '#description' => t('Limits the number of displayed facets via JavaScript.'),
      '#states' => array(
        'visible' => array(
          'select[name="widget"]' => array(
            'value' => $this->id,
          ),
        ),
      ),
    );
    $form['widget']['widget_settings']['links'][$this->id]['checkboxes_update_results'] = array(
      '#type' => 'checkbox',
      '#title' => t('Update results by ajax'),
      '#default_value' => !empty($this->settings->settings['checkboxes_update_results']),
      '#description' => t('If active, then search results will be updated by AJAX when facet was changed.
       If not active, then after than facet will be updated, user will see special popup with link for filter results.'),
      '#states' => array(
        'visible' => array(
          'select[name="widget"]' => array(
            'value' => $this->id,
          ),
        ),
      ),
    );
    $form['widget']['widget_settings']['links'][$this->id]['checkboxes_show_reset_link'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display reset link'),
      '#default_value' => !empty($this->settings->settings['checkboxes_show_reset_link']),
      '#description' => t('Display link for reset facet.'),
      '#states' => array(
        'visible' => array(
          'select[name="widget"]' => array(
            'value' => $this->id,
          ),
        ),
      ),
    );
    if ($this->facet['hierarchy callback']) {
      $form['widget']['widget_settings']['links'][$this->id]['show_expanded'] = array(
        '#type' => 'checkbox',
        '#title' => t('Expand hierarchy'),
        '#default_value' => !empty($this->settings->settings['show_expanded']),
        '#description' => t('Show the entire tree regardless of whether the parent items are active.'),
        '#states' => array(
          'visible' => array(
            'select[name="widget"]' => array(
              'value' => $this->id,
            ),
          ),
        ),
      );
    }
  }

  /**
   * Overrides FacetapiWidget::getDefaultSettings().
   */
  function getDefaultSettings() {
    return array(
      'show_expanded' => 0,
      'checkboxes_update_results' => 1,
      'checkboxes_show_reset_link' => 0,
    );
  }

  /**
   * Transforms the render array for use with theme_item_list().
   *
   * The recursion allows this function to act on the various levels of a
   * hierarchical data set.
   *
   * @param array $build
   *   The items in the facet's render array being transformed.
   *
   * @return array
   *   The "items" parameter for theme_item_list().
   */
  function buildListItems($build) {
    $have_active = FALSE;

    // Builds rows.
    $items = array();
    $items_count = count($build);
    $active_items = array();
    foreach ($build as $value => $item) {
      $row = array(
        'class' => array(),
      );
      $attributes = array(
        'class' => array(
          'facet-multiselect-checkbox',
        ),
        'data-facet' => urlencode($this->settings->facet),
      );

      // Respect current selection.
      if ($item['#active']) {
        $attributes['checked'] = 'checked';
        $have_active = TRUE;
        $active_items[] = $this->key . ':' . $item['#markup'];
      }
      $checkbox = array(
        '#id' => 'ajax-facets-checkboxes-' . str_replace(array(
          '_',
          ' ',
        ), '-', $this->key) . '-' . drupal_strtolower($value),
        '#name' => urlencode($this->key) . ':' . $value,
        '#type' => 'checkbox',
        '#title' => $item['#markup'] . '(' . $item['#count'] . ')',
        '#attributes' => $attributes,
      );
      $row['data'] = drupal_render($checkbox);
      if ($items_count == 1) {
        $row['class'][] = 'single-leaf';
      }
      if (!empty($item['#item_children'])) {
        if ($item['#active'] || !empty($this->settings->settings['show_expanded'])) {
          $row['class'][] = 'expanded';
          $row['children'] = $this
            ->buildListItems($item['#item_children']);
        }
        else {
          $row['class'][] = 'collapsed';
        }
      }
      $items[] = $row;
    }
    $this->jsSettings['haveActiveSelection'] = $this->settings->settings['have_active_selection'] = $have_active;
    sort($active_items);
    $this->jsSettings['activeItems'] = $active_items;

    // Generate reset path on server side to make possible to use aliases.
    if ($have_active) {
      $this->jsSettings['resetPath'] = ajax_facets_facet_build_reset_path($this->facet
        ->getFacet(), $this->facet
        ->getAdapter());
    }
    return $items;
  }

  /**
   * Implements FacetapiWidget::execute().
   *
   * Transforms the render array into something that can be themed by
   * theme_item_list().
   *
   * @see FacetapiWidgetLinks::setThemeHooks()
   * @see FacetapiWidgetLinks::buildListItems()
   */
  public function execute() {
    $element =& $this->build[$this->facet['field alias']];

    // Sets each item's theme hook, builds item list.
    $this
      ->setThemeHooks($element);
    $item_list = array(
      '#theme' => 'item_list',
      '#items' => $this
        ->buildListItems($element),
      '#attributes' => $this->build['#attributes'],
    );
    $reset_link = '';
    if (!empty($this->settings->settings['checkboxes_show_reset_link']) && $this->settings->settings['checkboxes_show_reset_link']) {
      $reset_link = '<a class="reset-link" href="#">' . t('Reset filter') . '</a>';
    }
    $element = array(
      '#markup' => '<div class="facet-wrapper-checkboxes ' . $this->build['#attributes']['id'] . '">' . $reset_link . render($item_list) . '</div>',
    );
  }

}

Members