CommerceProductReferenceSynonymsBehavior.class.inc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * @file
  4. * Enables Commerce Product Reference field type for synonyms integration.
  5. */
  6. /**
  7. * Definition of CommerceProductReferenceSynonymsBehavior class.
  8. */
  9. class CommerceProductReferenceSynonymsBehavior extends AbstractFieldSynonymsBehavior {
  10. public function extractSynonyms($entity, $langcode = NULL) {
  11. $synonyms = array();
  12. $product_ids = array();
  13. foreach ($this->entityItems($entity, $langcode) as $item) {
  14. $product_ids[] = $item['product_id'];
  15. }
  16. $entities = commerce_product_load_multiple($product_ids);
  17. foreach ($entities as $entity) {
  18. $synonyms[] = entity_label('commerce_product', $entity);
  19. }
  20. return $synonyms;
  21. }
  22. public function mergeEntityAsSynonym($trunk_entity, $synonym_entity, $synonym_entity_type) {
  23. if ('commerce_product' != $synonym_entity_type) {
  24. return;
  25. }
  26. $items = $this->entityItems($trunk_entity);
  27. $synonym_entity_id = entity_extract_ids($synonym_entity_type, $synonym_entity);
  28. $items[] = array(
  29. 'product_id' => $synonym_entity_id[0],
  30. );
  31. $trunk_entity->{$this->field['field_name']}[LANGUAGE_NONE] = $this->uniqueItems($items, array('product_id'));
  32. }
  33. public function synonymsFind(QueryConditionInterface $condition) {
  34. if ($this->field['storage']['type'] != 'field_sql_storage') {
  35. throw new SynonymsBehaviorException(t('Not supported storage engine %type in @method() method.', array(
  36. '%type' => $this->field['storage']['type'],
  37. '@method' => __METHOD__,
  38. )));
  39. }
  40. $table = array_keys($this->field['storage']['details']['sql'][FIELD_LOAD_CURRENT]);
  41. $table = reset($table);
  42. $column = $this->field['storage']['details']['sql'][FIELD_LOAD_CURRENT][$table]['product_id'];
  43. $query = db_select($table, 'field');
  44. $product_entity_type_info = entity_get_info('commerce_product');
  45. $product_entity_alias = $query->innerJoin($product_entity_type_info['base table'], 'product', 'field.' . $column . ' = %alias.' . $product_entity_type_info['entity keys']['id']);
  46. $query->addField($product_entity_alias, $product_entity_type_info['entity keys']['label'], 'synonym');
  47. $query->fields('field', array('entity_id'));
  48. $query->condition('field.entity_type', $this->instance['entity_type']);
  49. $query->condition('field.bundle', $this->instance['bundle']);
  50. $this->synonymsFindProcessCondition($condition, $product_entity_alias . '.' . $product_entity_type_info['entity keys']['label'], 'field.entity_id');
  51. $query->condition($condition);
  52. return $query->execute();
  53. }
  54. }