You are here

public function MySynonymsSynonymsBehavior::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.api.php, line 154
Documentation for Synonyms module.

Class

MySynonymsSynonymsBehavior
Example of synonyms behavior implementation class.

Code

public function synonymsFind(QueryConditionInterface $condition) {

  // Here, as an example, we'll query an imaginary table where your module
  // supposedly keeps synonyms. We'll also use helpful
  // AbstractSynonymsBehavior::synonymsFindProcessCondition() to normalize
  // $condition argument.
  $query = db_select('my_synonyms_storage_table', 'table');
  $query
    ->addField('table', 'entity_id', 'entity_id');
  $query
    ->addField('table', 'synonym', 'synonym');
  $this
    ->synonymsFindProcessCondition($condition, 'table.synonym', 'table.entity_id');
  $query
    ->condition($condition);
  return $query
    ->execute();
}