You are here

class FacetapiUrlProcessorStandard in Facet API 7

Same name and namespace in other branches
  1. 6.3 plugins/facetapi/url_processor_standard.inc \FacetapiUrlProcessorStandard
  2. 7.2 plugins/facetapi/url_processor_standard.inc \FacetapiUrlProcessorStandard

Url processor plugin that retrieves facet data from the query string.

This plugin retrieves facet data from $_GET, and stored all information in the "f" query string variable by default.

Hierarchy

Expanded class hierarchy of FacetapiUrlProcessorStandard

3 string references to 'FacetapiUrlProcessorStandard'
FacetapiAdapter::loadUrlProcessor in plugins/facetapi/adapter.inc
Loads the URL processor associated with this adapter.
facetapi_facetapi_url_processors in ./facetapi.facetapi.inc
Implements hook_facetapi_url_processors().
hook_facetapi_url_processors in ./facetapi.api.php
Define all URL processor plugins provided by the module.

File

plugins/facetapi/url_processor_standard.inc, line 14
The standard url processor class.

View source
class FacetapiUrlProcessorStandard extends FacetapiUrlProcessor {

  /**
   * Stores the "limit_active_items" settings for each facet.
   *
   * @var array
   */
  protected $limitActiveItems = array();

  /**
   * Implements FacetapiUrlProcessor::fetchParams().
   *
   * Use $_GET as the source for facet data.
   */
  public function fetchParams() {
    return $_GET;
  }

  /**
   * Implements FacetapiUrlProcessor::normalizeParams().
   *
   * Strips the "q" and "page" variables from the params array.
   */
  public function normalizeParams(array $params, $filter_key = 'f') {
    return drupal_get_query_parameters($params, array(
      'q',
      'page',
    ));
  }

  /**
   * Implements FacetapiUrlProcessor::getQueryString().
   */
  public function getQueryString(array $facet, array $values, $active) {
    $qstring = $this->params;
    $active_items = $this->adapter
      ->getActiveItems($facet);

    // Appends to qstring if inactive, removes if active.
    foreach ($values as $value) {
      if ($active && isset($active_items[$value])) {
        unset($qstring[$this->filterKey][$active_items[$value]['pos']]);
      }
      elseif (!$active) {
        $field_alias = rawurlencode($facet['field alias']);

        // Strips all other filters for this facet if limit option is set.
        if ($this
          ->limitActiveItems($facet)) {
          foreach ($qstring[$this->filterKey] as $pos => $filter) {

            // Identify a filter by field alias.
            if (0 === strpos($filter, "{$field_alias}:")) {
              unset($qstring[$this->filterKey][$pos]);
            }
          }
        }

        // Adds the filter to the query string.
        $qstring[$this->filterKey][] = $field_alias . ':' . $value;
      }
    }

    // Removes duplicates, resets array keys and returns query string.
    // @see http://drupal.org/node/1340528
    $qstring[$this->filterKey] = array_values(array_unique($qstring[$this->filterKey]));
    return array_filter($qstring);
  }

  /**
   * Checks the facet's global "limit_active_items" settings.
   *
   * @return int
   *   Whether or not to limit active items to one per facet.
   */
  public function limitActiveItems(array $facet) {
    if (!isset($this->limitActiveItems[$facet['name']])) {
      $settings = $this->adapter
        ->getFacetSettingsGlobal($facet);
      $this->limitActiveItems[$facet['name']] = $settings->settings['limit_active_items'];
    }
    return $this->limitActiveItems[$facet['name']];
  }

  /**
   * Implements FacetapiUrlProcessor::setBreadcrumb().
   */
  public function setBreadcrumb() {
    $breadcrumb = drupal_get_breadcrumb();

    // Gets search keys and active items form the adapter.
    $keys = $this->adapter
      ->getSearchKeys();
    $active_items = $this->adapter
      ->getAllActiveItems();
    $item = menu_get_item();

    // Initializes base breadcrumb query.
    $query = $this->params;
    unset($query[$this->filterKey]);

    // Adds the current search to the query.
    if ($keys) {

      // The last item should be text, not a link.
      $breadcrumb[] = $active_items ? l($keys, current_path(), array(
        'query' => $query,
      )) : check_plain($keys);
    }

    // Adds filters to the breadcrumb trail.
    $last = end($active_items);
    foreach ($active_items as $item) {
      $query[$this->filterKey][] = rawurlencode($item['field alias']) . ':' . $item['value'];

      // Replaces with the mapped value.
      $value = $this->adapter
        ->getMappedValue($item['facets'][0], $item['value']);

      // The last item should be text, not a link.
      if ($last == $item) {
        $breadcrumb[] = !empty($value['#html']) ? $value['#markup'] : check_plain($value['#markup']);
      }
      else {

        // Appends the filter to the breadcrumb trail.
        $breadcrumb[] = l($value['#markup'], current_path(), array(
          'query' => $query,
          'html' => !empty($value['#html']),
        ));
      }
    }

    // Sets the breadcrumb trail with the keys and filters.
    drupal_set_breadcrumb($breadcrumb);
  }

  /**
   * Allows for processor specific overrides to the settings form.
   */
  public function settingsForm(&$form, &$form_state) {
    $facet = $form['#facetapi']['facet'];
    $settings = $this->adapter
      ->getFacetSettingsGlobal($facet);
    $form['global']['limit_active_items'] = array(
      '#type' => 'checkbox',
      '#title' => t('Limit to one active item'),
      '#prefix' => '<div class="facetapi-global-setting">',
      '#suffix' => '</div>',
      '#default_value' => !empty($settings->settings['limit_active_items']),
      '#description' => t('Enabling this option allows only one item to be active at a time.'),
    );
  }

  /**
   * Provides default values for the backend specific settings.
   *
   * @return array
   *   The defaults keyed by setting name to value.
   */
  public function getDefaultSettings() {
    return array(
      'limit_active_items' => 0,
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FacetapiUrlProcessor::$adapter protected property The adapter that the url processor plugin is associated with.
FacetapiUrlProcessor::$filterKey protected property The array key in FacetapiUrlProcessor::params containing the facet data.
FacetapiUrlProcessor::$params protected property An array of facet params, usually $_GET.
FacetapiUrlProcessor::getFacetPath public function Returns the path for a facet item.
FacetapiUrlProcessor::getFilterKey public function Returns the filter key.
FacetapiUrlProcessor::getParams public function Returns the params.
FacetapiUrlProcessor::removeParam public function Removes an item from the $this->params array.
FacetapiUrlProcessor::setParams public function Sets the normalized parameters.
FacetapiUrlProcessor::__construct public function Constructs a FacetapiUrlProcessor object.
FacetapiUrlProcessorStandard::$limitActiveItems protected property Stores the "limit_active_items" settings for each facet.
FacetapiUrlProcessorStandard::fetchParams public function Implements FacetapiUrlProcessor::fetchParams(). Overrides FacetapiUrlProcessor::fetchParams
FacetapiUrlProcessorStandard::getDefaultSettings public function Provides default values for the backend specific settings. Overrides FacetapiUrlProcessor::getDefaultSettings
FacetapiUrlProcessorStandard::getQueryString public function Implements FacetapiUrlProcessor::getQueryString(). Overrides FacetapiUrlProcessor::getQueryString
FacetapiUrlProcessorStandard::limitActiveItems public function Checks the facet's global "limit_active_items" settings.
FacetapiUrlProcessorStandard::normalizeParams public function Implements FacetapiUrlProcessor::normalizeParams(). Overrides FacetapiUrlProcessor::normalizeParams
FacetapiUrlProcessorStandard::setBreadcrumb public function Implements FacetapiUrlProcessor::setBreadcrumb(). Overrides FacetapiUrlProcessor::setBreadcrumb
FacetapiUrlProcessorStandard::settingsForm public function Allows for processor specific overrides to the settings form. Overrides FacetapiUrlProcessor::settingsForm