You are here

class FacetapiDependencyBundle in Facet API 6.3

Same name and namespace in other branches
  1. 7.2 plugins/facetapi/dependency_bundle.inc \FacetapiDependencyBundle
  2. 7 plugins/facetapi/dependency_bundle.inc \FacetapiDependencyBundle

Adds a dependency on bundle.

Hierarchy

Expanded class hierarchy of FacetapiDependencyBundle

2 string references to 'FacetapiDependencyBundle'
facetapi_facetapi_dependencies in ./facetapi.facetapi.inc
Implements hook_facetapi_dependencies().
hook_facetapi_dependencies in ./facetapi.api.php
Define all dependency plugins provided by the module.

File

plugins/facetapi/dependency_bundle.inc, line 11
Performs a dependency check against the passed bundle.

View source
class FacetapiDependencyBundle extends FacetapiDependency {

  /**
   * Executes the dependency check.
   */
  public function execute() {
    switch ($this->settings['bundle']) {
      case 'referenced':

        // Check match between the field's bundles and active bundle items.
        foreach ($this
          ->getEnabledBundleFacets() as $facet) {
          foreach ($this->activeItems[$facet['name']] as $entity => $facet) {
            $field_info = content_fields($this->facet['field api name'], $entity);
            if ($field_info) {
              return NULL;
            }
          }
        }

        // There was no match.
        return FALSE;
      case 'selected':

        // Check match between selected bundles and active bundle items.
        $selected = array_filter($this->settings['bundle_selected']);
        foreach ($this
          ->getEnabledBundleFacets() as $facet) {
          if (array_intersect_key($this->activeItems[$facet['name']], $selected)) {
            return NULL;
          }
        }

        // There was no match.
        return FALSE;
    }
  }

  /**
   * Returns an array of enabled facets that filter by bundle.
   *
   * @return array
   *   An array of enabled bundle facets.
   */
  public function getEnabledBundleFacets() {
    $enabled = array_filter($this->adapter
      ->getEnabledFacets(), array(
      $this,
      'filterBundleFacets',
    ));
    $active = array_filter($this->activeItems);
    return array_intersect_key($enabled, $active);
  }

  /**
   * Returns TRUE if the facet contains bundle information.
   *
   * @param array $facet
   *   The facet definition beinf filtered.
   *
   * @return
   *   A boolean flagging whether the item should remain in the array.
   */
  public function filterBundleFacets($facet) {
    return !empty($facet['field api bundles']);
  }

  /**
   * Adds dependency settings to the form.
   */
  public function settingsForm(&$form, &$form_state) {

    // Builds array of options.
    $options = array();
    $options['none'] = t('No dependencies.');
    if ($this->facet['field api name']) {
      $options['referenced'] = t('A bundle this field is attached to must be active.');
    }
    $options['selected'] = t('At least one of the selected bundles must be active.');
    $form[$this->id]['bundle'] = array(
      '#title' => t('Dependency settings'),
      '#type' => 'radios',
      '#options' => $options,
      '#default_value' => $this->settings['bundle'],
    );
    $form[$this->id]['bundle_selected'] = array(
      '#title' => t('Required bundles'),
      '#type' => 'checkboxes',
      '#options' => $this
        ->getBundleOptions($form['#facetapi']['adapter']
        ->getTypes()),
      '#default_value' => $this->settings['bundle_selected'],
      '#states' => array(
        'visible' => array(
          'input[name="bundle"]' => array(
            'value' => 'selected',
          ),
        ),
      ),
      '#description' => t('At least one of the selected bundles must be active for this facet to be rendered.'),
    );
  }

  /**
   * Gets bundles.
   *
   * @param array $entity_types
   *   An array containing the machine readable name of the entities.
   *
   * @return
   *   An array of bundles associates with the entities.
   */
  public function getBundleOptions(array $entity_types) {
    $options = array();

    // No entities in D6 - Only nodes
    $bundle_types = node_get_types();
    foreach ($bundle_types as $bundle => $bundle_info) {
      $options[$bundle] = check_plain($bundle_info->name);
    }
    return $options;
  }

  /**
   * Returns defaults for settings.
   */
  public function getDefaultSettings() {
    return array(
      'bundle' => 'none',
      'bundle_selected' => array(),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FacetapiDependency::$activeItems protected property An array of active items.
FacetapiDependency::$adapter protected property The adapter object.
FacetapiDependency::$facet protected property The facet definition.
FacetapiDependency::$settings protected property An array of facet settings.
FacetapiDependency::getFacet public function Gets the facet definition.
FacetapiDependency::getId public function Gets the id of the plugin.
FacetapiDependency::__construct public function
FacetapiDependencyBundle::execute public function Executes the dependency check. Overrides FacetapiDependency::execute
FacetapiDependencyBundle::filterBundleFacets public function Returns TRUE if the facet contains bundle information.
FacetapiDependencyBundle::getBundleOptions public function Gets bundles.
FacetapiDependencyBundle::getDefaultSettings public function Returns defaults for settings. Overrides FacetapiDependency::getDefaultSettings
FacetapiDependencyBundle::getEnabledBundleFacets public function Returns an array of enabled facets that filter by bundle.
FacetapiDependencyBundle::settingsForm public function Adds dependency settings to the form. Overrides FacetapiDependency::settingsForm