You are here

public function FacetapiAdapter::buildRealm in Facet API 7.2

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

Uses each facet's widget to build the realm's render array.

This array is passed to Drupal's rendering layer for display. The widget plugins are executed to convert the base render arrays constructed by FacetapiAdapter::processFacets() to a realm specific render array.

Parameters

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

Return value

array The realm's render array.

See also

FacetapiAdapter::processFacets()

File

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

Class

FacetapiAdapter
Abstract class extended by Facet API adapters.

Code

public function buildRealm($realm_name) {

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

  // Make sure facet builds are initialized and breadcrumb trail is set.
  $this
    ->processFacets();

  // Add JavaScript, initializes the realm specific render array.
  $build = array(
    '#adapter' => $this,
    '#realm' => $realm,
  );

  // Iterate over the realm's enabled facets and build their render arrays.
  foreach ($this
    ->getEnabledFacets($realm['name']) as $facet) {

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

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

    // Try to be smart when merging the render arrays. Crazy things happen
    // when merging facets with the same field alias. In these instances we
    // want to merge only the values.
    foreach (element_children($facet_build) as $child) {

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

      // Our attempt at merging 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;
}