TextSynonymsBehavior.class.inc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * @file
  4. * Enables text and number field types to be source of synonyms.
  5. */
  6. /**
  7. * Definition of TextSynonymsBehavior class.
  8. */
  9. class TextSynonymsBehavior extends AbstractFieldSynonymsBehavior implements AutocompleteSynonymsBehavior, SelectSynonymsBehavior {
  10. public function extractSynonyms($entity, $langcode = NULL) {
  11. $synonyms = array();
  12. foreach ($this->entityItems($entity, $langcode) as $item) {
  13. $synonyms[] = $item['value'];
  14. }
  15. return $synonyms;
  16. }
  17. public function mergeEntityAsSynonym($trunk_entity, $synonym_entity, $synonym_entity_type) {
  18. $synonym = entity_label($synonym_entity_type, $synonym_entity);
  19. switch ($this->field['type']) {
  20. case 'text':
  21. break;
  22. // We add synonyms for numbers only if $synonym is a number.
  23. case 'number_integer':
  24. case 'number_float':
  25. case 'number_decimal':
  26. if (!is_numeric($synonym)) {
  27. return;
  28. }
  29. break;
  30. }
  31. $items = $this->entityItems($trunk_entity);
  32. $items[] = array(
  33. 'value' => $synonym,
  34. );
  35. $trunk_entity->{$this->field['field_name']}[LANGUAGE_NONE] = $this->uniqueItems($items, array('value'));
  36. }
  37. public function synonymsFind(QueryConditionInterface $condition) {
  38. if ($this->field['storage']['type'] != 'field_sql_storage') {
  39. throw new SynonymsBehaviorException(t('Not supported storage engine %type in @method() method.', array(
  40. '%type' => $this->field['storage']['type'],
  41. '@method' => __METHOD__,
  42. )));
  43. }
  44. $table = array_keys($this->field['storage']['details']['sql'][FIELD_LOAD_CURRENT]);
  45. $table = reset($table);
  46. $column = $this->field['storage']['details']['sql'][FIELD_LOAD_CURRENT][$table]['value'];
  47. $this->synonymsFindProcessCondition($condition, $column, 'entity_id');
  48. $query = db_select($table);
  49. $query->fields($table, array('entity_id'));
  50. $query->addField($table, $column, 'synonym');
  51. return $query->condition($condition)
  52. ->condition('entity_type', $this->instance['entity_type'])
  53. ->condition('bundle', $this->instance['bundle'])
  54. ->execute();
  55. }
  56. }