public function CommercePriceSynonymsBehavior::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:
- AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER: to denote synonyms column which you should replace with the actual column name where the synonyms data for your provider is stored in plain text.
- AbstractSynonymsBehavior::COLUMN_ENTITY_ID_PLACEHOLDER: to denote column that holds entity ID. You are supposed to replace this placeholder with actual column name that holds entity ID in your case.
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_commerce/
includes/ CommercePriceSynonymsBehavior.class.inc, line 27 - Enables Commerce Price field type for synonyms integration.
Class
- CommercePriceSynonymsBehavior
- Definition of CommercePriceSynonymsBehavior 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);
$columns = $this->field['storage']['details']['sql'][FIELD_LOAD_CURRENT][$table];
$query = db_select($table, 'field');
$query
->fields('field', array(
'entity_id',
));
$query
->addField('field', $columns['amount'], 'amount');
$query
->addField('field', $columns['currency_code'], 'currency_code');
$query
->condition('field.entity_type', $this->instance['entity_type']);
$query
->condition('field.bundle', $this->instance['bundle']);
$this
->synonymsFindProcessCondition($condition, 'field.' . $columns['amount'], 'field.entity_id');
$query
->condition($condition);
$result = $query
->execute();
$matches = array();
foreach ($result as $row) {
$matches[] = (object) array(
'entity_id' => $row->entity_id,
'synonym' => commerce_currency_format($row->amount, $row->currency_code),
);
}
return $matches;
}