TaxonomyTermReferenceTermMergeSynonymsBehavior.class.inc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * @file
  4. * Definition of TaxonomyTermReferenceTermMergeSynonymsBehavior class.
  5. */
  6. /**
  7. * Synonyms "term_merge" behavior for 'taxonomy term reference' field type.
  8. */
  9. class TaxonomyTermReferenceTermMergeSynonymsBehavior extends TaxonomySynonymsBehavior implements TermMergeSynonymsBehavior {
  10. /**
  11. * Add an entity as a synonym into another entity.
  12. *
  13. * Basically this method should be called when you want to add some entity as
  14. * a synonym to another entity (for example when you merge one entity into
  15. * another and besides merging want to add synonym of the merged entity into
  16. * the trunk entity). You should update $trunk_entity in such a way that it
  17. * holds $synonym_entity as a synonym (it all depends on how data is stored in
  18. * your behavior implementation, but probably you will store entity label or
  19. * its ID as you cannot literally store an entity inside of another entity).
  20. * If entity of type $synonym_entity_type cannot be converted into a format
  21. * expected by your behavior implementation, just do nothing.
  22. *
  23. * @param object $trunk_entity
  24. * Entity into which another one should be added as synonym
  25. * @param object $synonym_entity
  26. * Fully loaded entity object which has to be added as synonym
  27. * @param string $synonym_entity_type
  28. * Entity type of $synonym_entity
  29. */
  30. public function mergeTerm($trunk_entity, $synonym_entity, $synonym_entity_type) {
  31. // Taxonomy term reference supports only referencing of entity types
  32. // 'taxonomy_term'.. duh.
  33. if ($synonym_entity_type != 'taxonomy_term') {
  34. return;
  35. }
  36. $items = $this->entityItems($trunk_entity);
  37. // Checking that $field is configured to reference the vocabulary of
  38. // $synonym_entity term.
  39. $is_allowed = FALSE;
  40. foreach ($this->field['settings']['allowed_values'] as $setting) {
  41. if ($synonym_entity->vocabulary_machine_name == $setting['vocabulary']) {
  42. if ($setting['parent'] == 0) {
  43. // No need to check parent property as there is no limitation on it.
  44. $is_allowed = TRUE;
  45. break;
  46. }
  47. else {
  48. foreach (taxonomy_get_parents_all($synonym_entity->tid) as $parent) {
  49. if ($parent->tid == $setting['parent']) {
  50. $is_allowed = TRUE;
  51. break(2);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. if (!$is_allowed) {
  58. // Synonym term is from a vocabulary that is not expected by this field,
  59. // or under unexpected parent.
  60. return;
  61. }
  62. $items[] = array(
  63. 'tid' => $synonym_entity->tid,
  64. );
  65. $trunk_entity->{$this->field['field_name']}[LANGUAGE_NONE] = $this->uniqueItems($items, array('tid'));
  66. }
  67. }