You are here

class EntityReference_SelectionHandler_Generic_taxonomy_term in Entity reference 7

Override for the Taxonomy term type.

This only exists to workaround core bugs.

Hierarchy

Expanded class hierarchy of EntityReference_SelectionHandler_Generic_taxonomy_term

File

plugins/selection/EntityReference_SelectionHandler_Generic.class.php, line 510

View source
class EntityReference_SelectionHandler_Generic_taxonomy_term extends EntityReference_SelectionHandler_Generic {
  public function entityFieldQueryAlter(SelectQueryInterface $query) {

    // The Taxonomy module doesn't implement any proper taxonomy term access,
    // and as a consequence doesn't make sure that taxonomy terms cannot be viewed
    // when the user doesn't have access to the vocabulary.
    $base_table = $this
      ->ensureBaseTable($query);
    $vocabulary_alias = $query
      ->innerJoin('taxonomy_vocabulary', 'n', '%alias.vid = ' . $base_table . '.vid');
    $query
      ->addMetadata('base_table', $vocabulary_alias);

    // Pass the query to the taxonomy access control.
    $this
      ->reAlterQuery($query, 'taxonomy_vocabulary_access', $vocabulary_alias);

    // Also, the taxonomy term entity exposes a bundle, but doesn't have a bundle
    // column in the database. We have to alter the query ourself to go fetch
    // the bundle.
    $conditions =& $query
      ->conditions();
    foreach ($conditions as $key => &$condition) {
      if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === 'vocabulary_machine_name') {
        $condition['field'] = $vocabulary_alias . '.machine_name';
        break;
      }
    }
  }

  /**
   * Implements EntityReferenceHandler::getReferencableEntities().
   */
  public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
    if ($match || $limit) {
      return parent::getReferencableEntities($match, $match_operator, $limit);
    }
    $options = array();
    $entity_type = $this->field['settings']['target_type'];

    // We imitate core by calling taxonomy_get_tree().
    $entity_info = entity_get_info('taxonomy_term');
    $bundles = !empty($this->field['settings']['handler_settings']['target_bundles']) ? $this->field['settings']['handler_settings']['target_bundles'] : array_keys($entity_info['bundles']);
    foreach ($bundles as $bundle) {
      if ($vocabulary = taxonomy_vocabulary_machine_name_load($bundle)) {
        if ($terms = taxonomy_get_tree($vocabulary->vid, 0, NULL, TRUE)) {
          foreach ($terms as $term) {
            $options[$vocabulary->machine_name][$term->tid] = str_repeat('-', $term->depth) . check_plain(entity_label('taxonomy_term', $term));
          }
        }
      }
    }
    return $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityReference_SelectionHandler_Generic::buildEntityFieldQuery protected function Build an EntityFieldQuery to get referencable entities. 1
EntityReference_SelectionHandler_Generic::countReferencableEntities public function Implements EntityReferenceHandler::countReferencableEntities(). Overrides EntityReference_SelectionHandler::countReferencableEntities
EntityReference_SelectionHandler_Generic::ensureBaseTable public function Ensure a base table exists for the query.
EntityReference_SelectionHandler_Generic::getInstance public static function Implements EntityReferenceHandler::getInstance(). Overrides EntityReference_SelectionHandler::getInstance
EntityReference_SelectionHandler_Generic::getLabel public function Implements EntityReferenceHandler::getLabel(). Overrides EntityReference_SelectionHandler::getLabel 1
EntityReference_SelectionHandler_Generic::reAlterQuery protected function Helper method: pass a query to the alteration system again.
EntityReference_SelectionHandler_Generic::settingsForm public static function Implements EntityReferenceHandler::settingsForm(). Overrides EntityReference_SelectionHandler::settingsForm
EntityReference_SelectionHandler_Generic::validateAutocompleteInput public function Implements EntityReferenceHandler::validateAutocompleteInput(). Overrides EntityReference_SelectionHandler::validateAutocompleteInput
EntityReference_SelectionHandler_Generic::validateReferencableEntities public function Implements EntityReferenceHandler::validateReferencableEntities(). Overrides EntityReference_SelectionHandler::validateReferencableEntities
EntityReference_SelectionHandler_Generic::__construct protected function
EntityReference_SelectionHandler_Generic_taxonomy_term::entityFieldQueryAlter public function Implements EntityReferenceHandler::entityFieldQueryAlter(). Overrides EntityReference_SelectionHandler_Generic::entityFieldQueryAlter
EntityReference_SelectionHandler_Generic_taxonomy_term::getReferencableEntities public function Implements EntityReferenceHandler::getReferencableEntities(). Overrides EntityReference_SelectionHandler_Generic::getReferencableEntities