You are here

public function FacetapiAdapter::getFacetSettings in Facet API 7.2

Same name and namespace in other branches
  1. 6.3 plugins/facetapi/adapter.inc \FacetapiAdapter::getFacetSettings()
  2. 7 plugins/facetapi/adapter.inc \FacetapiAdapter::getFacetSettings()

Returns realm specific settings for a facet.

Realm specific settings usually act on the facet data after it has been returned by the backend, for example the display widget and sort settings.

Parameters

array $facet: The facet definition as returned by facetapi_facet_load().

array $realm: The realm definition as returned by facetapi_realm_load().

Return value

stdClass An object containing the settings.

See also

FacetapiAdapter::initSettingsObject()

ctools_export_crud_load()

File

plugins/facetapi/adapter.inc, line 758
Adapter plugin and adapter related classes.

Class

FacetapiAdapter
Abstract class extended by Facet API adapters.

Code

public function getFacetSettings(array $facet, array $realm) {

  // Build the unique name of the configuration and check whether the setting
  // has already be loaded so defaults are processed only once per setting.
  $name = $this->info['name'] . ':' . $realm['name'] . ':' . $facet['name'];
  if (!isset($this->settings[$name])) {

    // Initialize settings and flag whether it is "new" meaning that all
    // setting defaults are used.
    $this->settings[$name] = $this
      ->initSettingsObject($name, $facet['name'], $realm['name']);
    $is_new = empty($this->settings[$name]->settings);

    // Use realm's default widget if facet doesn't define one.
    if (!empty($facet['default widget'])) {
      $widget = $facet['default widget'];
    }
    else {
      $widget = $realm['default widget'];
    }

    // Apply defaults common across all configs.
    $this->settings[$name]->settings += array(
      'weight' => 0,
      'widget' => $widget,
      'filters' => array(),
      'active_sorts' => array(),
      'sort_weight' => array(),
      'sort_order' => array(),
      'empty_behavior' => 'none',
      'facet_more_text' => 'Show more',
      'facet_fewer_text' => 'Show fewer',
    );

    // Apply default sort info only if the configuration is "new".
    if ($is_new) {
      $weight = -50;
      foreach ($facet['default sorts'] as $sort => $default) {
        $this->settings[$name]->settings['active_sorts'][$default[0]] = $default[0];
        $this->settings[$name]->settings['sort_weight'][$default[0]] = $weight++;
        $this->settings[$name]->settings['sort_order'][$default[0]] = $default[1];
      }
    }

    // Apply the widget plugin's default settings.
    $id = $this->settings[$name]->settings['widget'];
    $class = ctools_plugin_load_class('facetapi', 'widgets', $id, 'handler');

    // If we have an invalid widget, fall back to the realm's default widget.
    if (!$class) {
      $id = $this->settings[$name]->settings['widget'] = $realm['default widget'];
      $class = ctools_plugin_load_class('facetapi', 'widgets', $id, 'handler');
    }
    $plugin = new $class($id, $realm, $this
      ->getFacet($facet), $this->settings[$name]);
    $this->settings[$name]->settings += $plugin
      ->getDefaultSettings();

    // @todo Save for performance?
  }
  return $this->settings[$name];
}