TextSynonymsBehavior.class.inc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 AbstractSynonymsSynonymsBehavior implements SynonymsSynonymsBehavior, AutocompleteSynonymsBehavior, SelectSynonymsBehavior {
  10. public function extractSynonyms($items, $field, $instance, $entity, $entity_type) {
  11. $synonyms = array();
  12. foreach ($items as $item) {
  13. $synonyms[] = $item['value'];
  14. }
  15. return $synonyms;
  16. }
  17. public function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type) {
  18. $synonym = entity_label($synonym_entity_type, $synonym_entity);
  19. switch ($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 array();
  28. }
  29. break;
  30. }
  31. return array(array(
  32. 'value' => $synonym,
  33. ));
  34. }
  35. public function synonymItemHash($item, $field, $instance) {
  36. return $item['value'];
  37. }
  38. public function synonymsFind(QueryConditionInterface $condition, $field, $instance) {
  39. if ($field['storage']['type'] != 'field_sql_storage') {
  40. throw new SynonymsSynonymsBehaviorException(t('Not supported storage engine %type in synonymsFind() method.', array(
  41. '%type' => $field['storage']['type'],
  42. )));
  43. }
  44. $table = array_keys($field['storage']['details']['sql'][FIELD_LOAD_CURRENT]);
  45. $table = reset($table);
  46. $column = $field['storage']['details']['sql'][FIELD_LOAD_CURRENT][$table]['value'];
  47. $this->synonymsFindProcessCondition($condition, $column);
  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', $instance['entity_type'])
  53. ->condition('bundle', $instance['bundle'])
  54. ->execute();
  55. }
  56. }