TaxonomySynonymsBehavior.class.inc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * @file
  4. * Enables Taxonomy Term Reference field type to be source of synonyms.
  5. */
  6. /**
  7. * Definition of TaxonomySynonymsBehavior class.
  8. */
  9. class TaxonomySynonymsBehavior extends AbstractFieldSynonymsBehavior implements AutocompleteSynonymsBehavior, SelectSynonymsBehavior {
  10. public function extractSynonyms($entity, $langcode = NULL) {
  11. $synonyms = array();
  12. $terms = array();
  13. foreach ($this->entityItems($entity, $langcode) as $item) {
  14. $terms[] = $item['tid'];
  15. }
  16. $terms = taxonomy_term_load_multiple($terms);
  17. foreach ($terms as $term) {
  18. $synonyms[] = entity_label('taxonomy_term', $term);
  19. }
  20. return $synonyms;
  21. }
  22. public function mergeEntityAsSynonym($trunk_entity, $synonym_entity, $synonym_entity_type) {
  23. // Taxonomy term reference supports only referencing of entity types
  24. // 'taxonomy_term'.. duh.
  25. if ($synonym_entity_type != 'taxonomy_term') {
  26. return;
  27. }
  28. $items = $this->entityItems($trunk_entity);
  29. // Checking that $field is configured to reference the vocabulary of
  30. // $synonym_entity term.
  31. $is_allowed = FALSE;
  32. foreach ($this->field['settings']['allowed_values'] as $setting) {
  33. if ($synonym_entity->vocabulary_machine_name == $setting['vocabulary']) {
  34. if ($setting['parent'] == 0) {
  35. // No need to check parent property as there is no limitation on it.
  36. $is_allowed = TRUE;
  37. break;
  38. }
  39. else {
  40. foreach (taxonomy_get_parents_all($synonym_entity->tid) as $parent) {
  41. if ($parent->tid == $setting['parent']) {
  42. $is_allowed = TRUE;
  43. break(2);
  44. }
  45. }
  46. }
  47. }
  48. }
  49. if (!$is_allowed) {
  50. // Synonym term is from a vocabulary that is not expected by this field,
  51. // or under unexpected parent.
  52. return;
  53. }
  54. $items[] = array(
  55. 'tid' => $synonym_entity->tid,
  56. );
  57. $trunk_entity->{$this->field['field_name']}[LANGUAGE_NONE] = $this->uniqueItems($items, array('tid'));
  58. }
  59. public function synonymsFind(QueryConditionInterface $condition) {
  60. if ($this->field['storage']['type'] != 'field_sql_storage') {
  61. throw new SynonymsBehaviorException(t('Not supported storage engine %type in @method() method.', array(
  62. '%type' => $this->field['storage']['type'],
  63. '@method' => __METHOD__,
  64. )));
  65. }
  66. $table = array_keys($this->field['storage']['details']['sql'][FIELD_LOAD_CURRENT]);
  67. $table = reset($table);
  68. $column = $this->field['storage']['details']['sql'][FIELD_LOAD_CURRENT][$table]['tid'];
  69. $query = db_select($table, 'field');
  70. $term_alias = $query->innerJoin('taxonomy_term_data', 'term', 'field.' . $column . ' = term.tid');
  71. $query->addField($term_alias, 'name', 'synonym');
  72. $query->fields('field', array('entity_id'));
  73. $query->condition('field.entity_type', $this->instance['entity_type']);
  74. $query->condition('field.bundle', $this->instance['bundle']);
  75. $this->synonymsFindProcessCondition($condition, $term_alias . '.name', 'field.entity_id');
  76. $query->condition($condition);
  77. return $query->execute();
  78. }
  79. }