EntityReferenceSynonymsExtractor.class.inc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * @file
  4. * Enables Entity Reference field type to be source of synonyms.
  5. */
  6. class EntityReferenceSynonymsExtractor extends AbstractSynonymsExtractor {
  7. public static function fieldTypesSupported() {
  8. return array('entityreference');
  9. }
  10. public static function synonymsExtract($items, $field, $instance, $entity, $entity_type) {
  11. $synonyms = array();
  12. // For speading up loading all the entities at once.
  13. $target_tids = array();
  14. foreach ($items as $item) {
  15. $target_tids[] = $item['target_id'];
  16. }
  17. $entities = entity_load($field['settings']['target_type'], $target_tids);
  18. foreach ($entities as $entity) {
  19. $synonyms[] = entity_label($field['settings']['target_type'], $entity);
  20. }
  21. return $synonyms;
  22. }
  23. public static function processEntityFieldQuery($tag, EntityFieldQuery $query, $field, $instance) {
  24. // Unfortunately EntityFieldQuery does not currently support INNER JOINing
  25. // referenced entities via any field type.
  26. // Thus, we use an ugly solution -- going through all entities that exist
  27. // in such entity type trying to see if there is a match by entity's label.
  28. $efq = new EntityFieldQuery();
  29. $efq->entityCondition('entity_type', $field['settings']['target_type']);
  30. // Additionally we have to figure out which column in the entity table
  31. // represents entity label.
  32. $entity_info = entity_get_info($field['settings']['target_type']);
  33. if (!isset($entity_info['entity keys']['label'])) {
  34. // We can't get any matches if we do not know what column to query
  35. // against. So we add a condition to $query which will 100% yield empty
  36. // results.
  37. self::emptyResultsCondition($query);
  38. return;
  39. }
  40. $efq->propertyCondition($entity_info['entity keys']['label'], '%' . $tag . '%', 'LIKE');
  41. $result = $efq->execute();
  42. if (!isset($result[$field['settings']['target_type']]) || !is_array($result[$field['settings']['target_type']])) {
  43. self::emptyResultsCondition($query);
  44. return;
  45. }
  46. $result = $result[$field['settings']['target_type']];
  47. $query->fieldCondition($field, 'target_id', array_keys($result));
  48. }
  49. public static function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type) {
  50. // Firstly validating that this entity reference is able to reference to
  51. // that type of entity.
  52. $expected_synonym_entity_type = $field['settings']['target_type'];
  53. if ($expected_synonym_entity_type != $synonym_entity_type) {
  54. return array();
  55. }
  56. $synonym_entity_id = entity_id($synonym_entity_type, $synonym_entity);
  57. return array(array(
  58. 'target_id' => $synonym_entity_id,
  59. ));
  60. }
  61. }