You are here

public function FacetapiAdapter::buildRealm in Facet API 6.3

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

Builds the render array for facets in a realm.

Parameters

string $realm_name: The machine readable name of the realm.

Return value

array The render array.

File

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

Class

FacetapiAdapter
Abstract class extended by search backends that retrieves facet information from the database.

Code

public function buildRealm($realm_name) {

  // Bails if realm isn't valid.
  // @todo Call watchdog()?
  if (!($realm = facetapi_realm_load($realm_name))) {
    return array();
  }

  // Makes sure facet builds are initialized.
  $this
    ->processFacets();

  // Adds JavaScript, initializes render array.
  drupal_add_js(drupal_get_path('module', 'facetapi') . '/facetapi.js');
  $build = array(
    '#adapter' => $this,
    '#realm' => $realm,
  );

  // Builds each facet in the realm, merges into realm's render array.
  foreach ($this
    ->getEnabledFacets($realm['name']) as $facet) {

    // Continue to the next facet if this one failed its dependencies.
    if (!$this->dependenciesPassed[$facet['name']]) {
      continue;
    }

    // Gets the initialized build.
    $field_alias = $facet['field alias'];
    $processor = $this->processors[$facet['name']];
    $facet_build = $this
      ->getFacet($facet)
      ->build($realm, $processor);

    // Tries to be smart when merging the render arrays. Crazy things happen
    // when merging facets with the same field alias such as taxonomy terms in
    // the fieldset realm. We want to merge only the values.
    foreach (element_children($facet_build) as $child) {

      // Bails if there is nothing to render.
      if (!element_children($facet_build[$child])) {
        continue;
      }

      // Attempts to merge gracefully.
      if (!isset($build[$child])) {
        $build = array_merge_recursive($build, $facet_build);
      }
      else {
        if (isset($build[$child][$field_alias]) && isset($facet_build[$child][$field_alias])) {
          $build[$child][$field_alias] = array_merge_recursive($build[$child][$field_alias], $facet_build[$child][$field_alias]);
        }
        elseif (isset($build[$child]['#options']) && isset($facet_build[$child]['#options'])) {
          $build[$child]['#options'] = array_merge_recursive($build[$child]['#options'], $facet_build[$child]['#options']);
        }
        else {
          $build = array_merge_recursive($build, $facet_build);
        }
      }
    }
  }
  return $build;
}