TaxonomySynonymsExtractor.class.inc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @file
  4. * Enables Taxonomy Term Reference field types to be source of synonyms.
  5. */
  6. class TaxonomySynonymsExtractor extends AbstractSynonymsExtractor {
  7. public static function fieldTypesSupported() {
  8. return array('taxonomy_term_reference');
  9. }
  10. public static function synonymsExtract($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 static function processEntityFieldQuery($tag, EntityFieldQuery $query, $field, $instance) {
  23. // Unfortunately EntityFieldQuery does not currently support INNER JOINing
  24. // term entity that is referenced via taxonomy_term_reference field type.
  25. // Thus, we use an ugly solution -- going through all terms that exist in
  26. // vocabulary and trying to see if there is a match by term's name.
  27. $tids = array();
  28. foreach ($field['settings']['allowed_values'] as $settings) {
  29. $efd = new EntityFieldQuery();
  30. $efd->entityCondition('bundle', $settings['vocabulary'])
  31. ->entityCondition('entity_type', 'taxonomy_term')
  32. ->propertyCondition('name', '%' . $tag . '%', 'LIKE');
  33. $result = $efd->execute();
  34. if (isset($result['taxonomy_term'])) {
  35. foreach ($result['taxonomy_term'] as $tid) {
  36. $tids[] = $tid->tid;
  37. }
  38. }
  39. }
  40. // Now we have tids of terms from the referenced vocabulary which names
  41. // LIKE %$tag%, suggested are the terms that refer to any of these $tids.
  42. if (empty($tids)) {
  43. // No possible suggestions were found. We have to make sure $query yields
  44. // no results.
  45. self::emptyResultsCondition($query);
  46. return;
  47. }
  48. $query->fieldCondition($field, 'tid', $tids);
  49. }
  50. public static function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type) {
  51. // Taxonomy term reference supports only referencing of entity types
  52. // 'taxonomy_term'.. duh.
  53. if ($synonym_entity_type != 'taxonomy_term') {
  54. return array();
  55. }
  56. // Checking that $field is configured to reference the vocabulary of
  57. // $synonym_entity term.
  58. $is_allowed = FALSE;
  59. foreach ($field['settings']['allowed_values'] as $setting) {
  60. if ($synonym_entity->vocabulary_machine_name == $setting['vocabulary']) {
  61. if ($setting['parent'] == 0) {
  62. // No need to check parent property as there is no limitation on it.
  63. $is_allowed = TRUE;
  64. break;
  65. }
  66. else {
  67. foreach (taxonomy_get_parents_all($synonym_entity->tid) as $parent) {
  68. if ($parent->tid == $setting['parent']) {
  69. $is_allowed = TRUE;
  70. break(2);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. if (!$is_allowed) {
  77. // Synonym term is from a vocabulary that is not expected by this field,
  78. // or under unexpected parent.
  79. return array();
  80. }
  81. return array(array(
  82. 'tid' => $synonym_entity->tid,
  83. ));
  84. }
  85. }