EntityReferenceTermMergeSynonymsBehavior.class.inc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * @file
  4. * Definition of EntityReferenceTermMergeSynonymsBehavior class.
  5. */
  6. /**
  7. * Synonyms "term_merge" behavior for 'entityreference' field type.
  8. */
  9. class EntityReferenceTermMergeSynonymsBehavior extends EntityReferenceSynonymsBehavior 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. // Firstly validating that this entity reference is able to reference that
  32. // type of entity.
  33. $expected_synonym_entity_type = $this->field['settings']['target_type'];
  34. $items = $this->entityItems($trunk_entity);
  35. if ($expected_synonym_entity_type != $synonym_entity_type) {
  36. return;
  37. }
  38. $synonym_entity_id = entity_extract_ids($synonym_entity_type, $synonym_entity);
  39. $synonym_entity_id = $synonym_entity_id[0];
  40. $items[] = array(
  41. 'target_id' => $synonym_entity_id,
  42. );
  43. $trunk_entity->{$this->field['field_name']}[LANGUAGE_NONE] = $this->uniqueItems($items, array('target_id'));
  44. }
  45. }