TaxonomySynonymsBehavior.class.inc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 AbstractSynonymsSynonymsBehavior implements SynonymsSynonymsBehavior, AutocompleteSynonymsBehavior, SelectSynonymsBehavior {
  10. public function extractSynonyms($items, $field, $instance, $entity, $entity_type) {
  11. $synonyms = array();
  12. $terms = array();
  13. foreach ($items 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($items, $field, $instance, $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 array();
  27. }
  28. // Checking that $field is configured to reference the vocabulary of
  29. // $synonym_entity term.
  30. $is_allowed = FALSE;
  31. foreach ($field['settings']['allowed_values'] as $setting) {
  32. if ($synonym_entity->vocabulary_machine_name == $setting['vocabulary']) {
  33. if ($setting['parent'] == 0) {
  34. // No need to check parent property as there is no limitation on it.
  35. $is_allowed = TRUE;
  36. break;
  37. }
  38. else {
  39. foreach (taxonomy_get_parents_all($synonym_entity->tid) as $parent) {
  40. if ($parent->tid == $setting['parent']) {
  41. $is_allowed = TRUE;
  42. break(2);
  43. }
  44. }
  45. }
  46. }
  47. }
  48. if (!$is_allowed) {
  49. // Synonym term is from a vocabulary that is not expected by this field,
  50. // or under unexpected parent.
  51. return array();
  52. }
  53. return array(array(
  54. 'tid' => $synonym_entity->tid,
  55. ));
  56. }
  57. public function synonymItemHash($item, $field, $instance) {
  58. return $item['tid'];
  59. }
  60. public function synonymsFind(QueryConditionInterface $condition, $field, $instance) {
  61. if ($field['storage']['type'] != 'field_sql_storage') {
  62. throw new SynonymsSynonymsBehaviorException(t('Not supported storage engine %type in synonymsFind() method.', array(
  63. '%type' => $field['storage']['type'],
  64. )));
  65. }
  66. $table = array_keys($field['storage']['details']['sql'][FIELD_LOAD_CURRENT]);
  67. $table = reset($table);
  68. $column = $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', $instance['entity_type']);
  74. $query->condition('field.bundle', $instance['bundle']);
  75. $this->synonymsFindProcessCondition($condition, $term_alias . '.name');
  76. $query->condition($condition);
  77. return $query->execute();
  78. }
  79. }