CommercePriceSynonymsBehavior.class.inc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * @file
  4. * Enables Commerce Price field type for synonyms integration.
  5. */
  6. /**
  7. * Definition of CommercePriceSynonymsBehavior class.
  8. */
  9. class CommercePriceSynonymsBehavior extends AbstractFieldSynonymsBehavior {
  10. public function extractSynonyms($entity, $langcode = NULL) {
  11. $synonyms = array();
  12. foreach ($this->entityItems($entity, $langcode) as $item) {
  13. $synonyms[] = commerce_currency_format($item['amount'], $item['currency_code'], $entity);
  14. }
  15. return $synonyms;
  16. }
  17. public function mergeEntityAsSynonym($trunk_entity, $synonym_entity, $synonym_entity_type) {
  18. // TODO: remove this thing.
  19. }
  20. public function synonymsFind(QueryConditionInterface $condition) {
  21. if ($this->field['storage']['type'] != 'field_sql_storage') {
  22. throw new SynonymsBehaviorException(t('Not supported storage engine %type in @method() method.', array(
  23. '%type' => $this->field['storage']['type'],
  24. '@method' => __METHOD__,
  25. )));
  26. }
  27. $table = array_keys($this->field['storage']['details']['sql'][FIELD_LOAD_CURRENT]);
  28. $table = reset($table);
  29. $columns = $this->field['storage']['details']['sql'][FIELD_LOAD_CURRENT][$table];
  30. $query = db_select($table, 'field');
  31. $query->fields('field', array('entity_id'));
  32. $query->addField('field', $columns['amount'], 'amount');
  33. $query->addField('field', $columns['currency_code'], 'currency_code');
  34. $query->condition('field.entity_type', $this->instance['entity_type']);
  35. $query->condition('field.bundle', $this->instance['bundle']);
  36. $this->synonymsFindProcessCondition($condition, 'field.' . $columns['amount'], 'field.entity_id');
  37. $query->condition($condition);
  38. $result = $query->execute();
  39. $matches = array();
  40. foreach ($result as $row) {
  41. $matches[] = (object) array(
  42. 'entity_id' => $row->entity_id,
  43. 'synonym' => commerce_currency_format($row->amount, $row->currency_code),
  44. );
  45. }
  46. return $matches;
  47. }
  48. }