You are here

class CommerceProductReferenceSynonymsBehavior in Synonyms 7

Definition of CommerceProductReferenceSynonymsBehavior class.

Hierarchy

Expanded class hierarchy of CommerceProductReferenceSynonymsBehavior

2 string references to 'CommerceProductReferenceSynonymsBehavior'
CommerceProductReferenceSynonymsBehaviorWebTestCase::getInfo in synonyms_commerce/synonyms_commerce.test
GetInfo method.
synonyms_commerce_synonyms_provider_field_behavior_implementation_info in synonyms_commerce/synonyms_commerce.module
Implements hook_synonyms_provider_field_behavior_implementation_info().

File

synonyms_commerce/includes/CommerceProductReferenceSynonymsBehavior.class.inc, line 11
Enables Commerce Product Reference field type for synonyms integration.

View source
class CommerceProductReferenceSynonymsBehavior extends AbstractFieldSynonymsBehavior {
  public function extractSynonyms($entity, $langcode = NULL) {
    $synonyms = array();
    $product_ids = array();
    foreach ($this
      ->entityItems($entity, $langcode) as $item) {
      $product_ids[] = $item['product_id'];
    }
    $entities = commerce_product_load_multiple($product_ids);
    foreach ($entities as $entity) {
      $synonyms[] = entity_label('commerce_product', $entity);
    }
    return $synonyms;
  }
  public function mergeEntityAsSynonym($trunk_entity, $synonym_entity, $synonym_entity_type) {
    if ('commerce_product' != $synonym_entity_type) {
      return;
    }
    $items = $this
      ->entityItems($trunk_entity);
    $synonym_entity_id = entity_extract_ids($synonym_entity_type, $synonym_entity);
    $items[] = array(
      'product_id' => $synonym_entity_id[0],
    );
    $trunk_entity->{$this->field['field_name']}[LANGUAGE_NONE] = $this
      ->uniqueItems($items, array(
      'product_id',
    ));
  }
  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]['product_id'];
    $query = db_select($table, 'field');
    $product_entity_type_info = entity_get_info('commerce_product');
    $product_entity_alias = $query
      ->innerJoin($product_entity_type_info['base table'], 'product', 'field.' . $column . ' = %alias.' . $product_entity_type_info['entity keys']['id']);
    $query
      ->addField($product_entity_alias, $product_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, $product_entity_alias . '.' . $product_entity_type_info['entity keys']['label'], 'field.entity_id');
    $query
      ->condition($condition);
    return $query
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractFieldSynonymsBehavior::$field protected property Field definition array on which this provider was initialized.
AbstractFieldSynonymsBehavior::$instance protected property Field instance definition on which this provider was initialized.
AbstractFieldSynonymsBehavior::entityItems protected function Retrieve items of the underlying field in this behavior implementation.
AbstractFieldSynonymsBehavior::featuresExportPipe public function Collect info on features pipe during invocation of hook_features_export(). Overrides AbstractSynonymsBehavior::featuresExportPipe
AbstractFieldSynonymsBehavior::uniqueItems protected function Filter $items only to contain unique values.
AbstractFieldSynonymsBehavior::__construct public function Overrides AbstractSynonymsBehavior::__construct
AbstractSynonymsBehavior::$behavior_implementation protected property Behavior implementation on which this class was initialized.
AbstractSynonymsBehavior::COLUMN_ENTITY_ID_PLACEHOLDER constant Constant which denotes placeholder of an entity ID column.
AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER constant Constant which denotes placeholder of a synonym column.
AbstractSynonymsBehavior::synonymsFindProcessCondition protected function Process condition in 'synonymsFind' method.
CommerceProductReferenceSynonymsBehavior::extractSynonyms public function Extract synonyms from an entity within a specific behavior implementation. Overrides SynonymsBehavior::extractSynonyms
CommerceProductReferenceSynonymsBehavior::mergeEntityAsSynonym public function Add an entity as a synonym into another entity. Overrides SynonymsBehavior::mergeEntityAsSynonym
CommerceProductReferenceSynonymsBehavior::synonymsFind public function Look up entities by their synonyms within a behavior implementation. Overrides SynonymsBehavior::synonymsFind