You are here

public function EntityReferenceSynonymsBehavior::synonymsFind in Synonyms 7

Look up entities by their synonyms within a behavior implementation.

You are provided with a SQL condition that you should apply to the storage of synonyms within the provided behavior implementation. And then return result: what entities match by the provided condition through what synonyms.

Parameters

QueryConditionInterface $condition: Condition that defines what to search for. Apart from normal SQL conditions as known in Drupal, it may contain the following placeholders:

For ease of work with these placeholders, you may extend the AbstractSynonymsBehavior class and then just invoke the AbstractSynonymsBehavior->synonymsFindProcessCondition() method, so you won't have to worry much about it. Important note: if you plan on re-using the same $condition object for multiple invocations of this method you must pass in here a clone of your condition object, since the internal implementation of this method will change the condition (will swap the aforementioned placeholders with actual column names)

Return value

Traversable Traversable result set of found synonyms and entity IDs to which those belong. Each element in the result set should be an object and will have the following structure:

  • synonym: (string) Synonym that was found and which satisfies the provided condition
  • entity_id: (int) ID of the entity to which the found synonym belongs

Overrides SynonymsBehavior::synonymsFind

File

synonyms_provider_field/includes/EntityReferenceSynonymsBehavior.class.inc, line 46
Enables Entity Reference field type to be source of synonyms.

Class

EntityReferenceSynonymsBehavior
Definition of EntityReferenceSynonymsBehavior class.

Code

public function synonymsFind(QueryConditionInterface $condition) {
  if ($this->field['storage']['type'] != 'field_sql_storage') {
    throw new SynonymsBehaviorException(t('Not supported storage engine %type in @method() method.', array(
      '%type' => $this->field['storage']['type'],
      '@method' => __METHOD__,
    )));
  }
  $table = array_keys($this->field['storage']['details']['sql'][FIELD_LOAD_CURRENT]);
  $table = reset($table);
  $column = $this->field['storage']['details']['sql'][FIELD_LOAD_CURRENT][$table]['target_id'];
  $query = db_select($table, 'field');
  $target_entity_type_info = entity_get_info($this->field['settings']['target_type']);
  if (!isset($target_entity_type_info['base table']) || !$target_entity_type_info['base table']) {
    throw new SynonymsBehaviorException(t('Target entity type %entity_type is not stored in database.', array(
      '%entity_type' => $this->field['settings']['target_type'],
    )));
  }
  if (!isset($target_entity_type_info['entity keys']['id'])) {
    throw new SynonymsBehaviorException(t('Target entity type %entity_type does not declare primary key.', array(
      '%entity_type' => $this->field['settings']['target_type'],
    )));
  }
  if (!isset($target_entity_type_info['entity keys']['label'])) {
    throw new SynonymsBehaviorException(t('Target entity type %entity_type does not declare label column.', array(
      '%entity_type' => $this->field['settings']['target_type'],
    )));
  }
  $target_entity_alias = $query
    ->innerJoin($target_entity_type_info['base table'], 'target_entity', 'field.' . $column . ' = target_entity.' . $target_entity_type_info['entity keys']['id']);
  $query
    ->addField($target_entity_alias, $target_entity_type_info['entity keys']['label'], 'synonym');
  $query
    ->fields('field', array(
    'entity_id',
  ));
  $query
    ->condition('field.entity_type', $this->instance['entity_type']);
  $query
    ->condition('field.bundle', $this->instance['bundle']);
  $this
    ->synonymsFindProcessCondition($condition, $target_entity_alias . '.' . $target_entity_type_info['entity keys']['label'], 'field.entity_id');
  $query
    ->condition($condition);
  return $query
    ->execute();
}