You are here

class FacetapiFacet in Facet API 6.3

Same name and namespace in other branches
  1. 6 facetapi.adapter.inc \FacetapiFacet
  2. 7.2 plugins/facetapi/adapter.inc \FacetapiFacet
  3. 7 plugins/facetapi/adapter.inc \FacetapiFacet

Stores facet data, provides methods that build the facet's render array.

Hierarchy

Expanded class hierarchy of FacetapiFacet

File

plugins/facetapi/adapter.inc, line 881
Adapter plugin and adapter related calsses.

View source
class FacetapiFacet implements ArrayAccess {

  /**
   * The FacetapiAdapter object.
   *
   * @var FacetapiAdapter
   */
  protected $adapter;

  /**
   * The facet definition.
   *
   * @var array
   */
  protected $facet;

  /**
   * The build array for the facet items.
   *
   * @var array
   */
  protected $build = array();

  /**
   * Constructor, sets adapter and facet definition.
   *
   * @param $adapter
   *   A FacetapiAdapter object.
   * @param $facet
   *   An array containing the facet definition.
   */
  public function __construct(FacetapiAdapter $adapter, array $facet) {
    $this->adapter = $adapter;
    $this->facet = $facet;
  }

  /**
   * Whether a offset exists
   *
   * @param mixed offset
   *   An offset to check for.
   *
   * @return boolean
   */
  public function offsetExists($offset) {
    return isset($this->facet[$offset]);
  }

  /**
   * Returns the value at specified offset.
   *
   * @param mixed offset
   *   The offset to retrieve.
   *
   * @return mixed
   */
  public function offsetGet($offset) {
    return isset($this->facet[$offset]) ? $this->facet[$offset] : NULL;
  }

  /**
   * Assigns a value to the specified offset.
   *
   * @param mixed offset
   *   The offset to assign the value to.
   * @param mixed value
   *   The value to set.
   */
  public function offsetSet($offset, $value) {
    if (NULL === $offset) {
      $this->facet[] = $value;
    }
    else {
      $this->facet[$offset] = $value;
    }
  }

  /**
   * Unsets an offset.
   *
   * @param mixed offset
   *   The offset to unset.
   */
  public function offsetUnset($offset) {
    unset($this->facet[$offset]);
  }

  /**
   * Returns the adapter object.
   *
   * @return FacetapiAdapter
   *   The adapter object.
   */
  public function getAdapter() {
    return $this->adapter;
  }

  /**
   * Returns the facet definition.
   *
   * @return array
   *   An array containing the facet definition.
   */
  public function getFacet() {
    return $this->facet;
  }

  /**
   * Returns the facet definition.
   *
   * @return array
   *   An array containing the facet definition.
   */
  public function getBuild() {
    return $this->build;
  }

  /**
   * Gets facet setting for the passed realm.
   *
   * @param string|array $realm
   *   The machine readable name of the realm or realm definition. Pass null to
   *   get global settings.
   *
   * @return
   *   An object containing the settings.
   */
  public function getSettings($realm = NULL) {
    if ($realm && !is_array($realm)) {
      $realm = facetapi_realm_load($realm);
    }
    $method = $realm ? 'getFacetSettings' : 'getFacetSettingsGlobal';
    return $this->adapter
      ->{$method}($this->facet, $realm);
  }

  /**
   * Returns the facet's render array.
   *
   * @param array $realm
   *   An array containing the realm definition.
   * @param FacetapiFacetProcessor $processor
   *   The processor object.
   *
   * @return
   *   The facet's build array.
   */
  public function build(array $realm, FacetapiFacetProcessor $processor) {
    $settings = $this
      ->getSettings($realm);

    // Gets the base render array from the facet processor.
    $this->build = $processor
      ->getBuild();

    // Executes filter plugins.
    // @todo Defensive coding here for filters?
    $enabled_filters = array_filter($settings->settings['filters'], 'facetapi_filter_disabled_filters');
    uasort($enabled_filters, 'facetapi_sort_weight');
    foreach ($enabled_filters as $filter_id => $filter_settings) {
      if ($class = ctools_plugin_load_class('facetapi', 'filters', $filter_id, 'handler')) {
        $filter_plugin = new $class($filter_id, $this->adapter, $settings);
        $this->build = $filter_plugin
          ->execute($this->build);
      }
      else {
        watchdog('facetapi', 'Filter %name not valid.', array(
          '%name' => $filter_id,
        ), WATCHDOG_ERROR);
      }
    }

    // Instantiates the widget plugin and initializes.
    // @todo Defensive coding here for widgets?
    $widget_name = $settings->settings['widget'];
    if (!($class = ctools_plugin_load_class('facetapi', 'widgets', $widget_name, 'handler'))) {
      watchdog('facetapi', 'Widget %name not valid.', array(
        '%name' => $widget_name,
      ), WATCHDOG_ERROR);
      return array();
    }
    $widget_plugin = new $class($widget_name, $realm, $this, $settings);
    $widget_plugin
      ->init();
    if ($this->build) {

      // Executes widget plugin.
      $widget_plugin
        ->execute();
      $build = $widget_plugin
        ->getBuild();
    }
    else {

      // Instantiates empty behavior plugin.
      $id = $settings->settings['empty_behavior'];
      $class = ctools_plugin_load_class('facetapi', 'empty_behaviors', $id, 'handler');
      $empty_plugin = new $class($settings);

      // Executes empty behavior plugin.
      $build = $widget_plugin
        ->getBuild();
      $build[$this['field alias']] = $empty_plugin
        ->execute();
    }

    // If the element is empty, unset it.
    if (!$build[$this['field alias']]) {
      unset($build[$this['field alias']]);
    }

    // Adds JavaScript settings in a way that merges with others already set.
    $merge_settings['facetapi']['facets'][] = $widget_plugin
      ->getJavaScriptSettings();
    drupal_add_js($merge_settings, 'setting');

    // Returns array keyed by the FacetapiWidget::$key property.
    return array(
      $widget_plugin
        ->getKey() => $build,
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FacetapiFacet::$adapter protected property The FacetapiAdapter object.
FacetapiFacet::$build protected property The build array for the facet items.
FacetapiFacet::$facet protected property The facet definition.
FacetapiFacet::build public function Returns the facet's render array.
FacetapiFacet::getAdapter public function Returns the adapter object.
FacetapiFacet::getBuild public function Returns the facet definition.
FacetapiFacet::getFacet public function Returns the facet definition.
FacetapiFacet::getSettings public function Gets facet setting for the passed realm.
FacetapiFacet::offsetExists public function Whether a offset exists
FacetapiFacet::offsetGet public function Returns the value at specified offset.
FacetapiFacet::offsetSet public function Assigns a value to the specified offset.
FacetapiFacet::offsetUnset public function Unsets an offset.
FacetapiFacet::__construct public function Constructor, sets adapter and facet definition.