AbstractSynonymsExtractor.class.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @file
  4. * Define interface required for extracting synonyms from field types.
  5. */
  6. abstract class AbstractSynonymsExtractor {
  7. /**
  8. * Return array of supported field types for synonyms extraction.
  9. *
  10. * @return array
  11. * Array of Field API field types from which this class is able to extract
  12. * synonyms
  13. */
  14. static public abstract function fieldTypesSupported();
  15. /**
  16. * Extract synonyms from a field attached to an entity.
  17. *
  18. * We try to pass as many info about context as possible, however, normally
  19. * you will only need $items to extract the synonyms.
  20. *
  21. * @param array $items
  22. * Array of items
  23. * @param array $field
  24. * Array of field definition according to Field API
  25. * @param array $instance
  26. * Array of instance definition according to Field API
  27. * @param object $entity
  28. * Fully loaded entity object to which the $field and $instance with $item
  29. * values is attached to
  30. * @param string $entity_type
  31. * Type of the entity $entity according to Field API definition of entity
  32. * types
  33. *
  34. * @return array
  35. * Array of synonyms extracted from $items
  36. */
  37. static public abstract function synonymsExtract($items, $field, $instance, $entity, $entity_type);
  38. /**
  39. * Allow you to hook in during autocomplete suggestions generation.
  40. *
  41. * Allow you to include entities for autocomplete suggestion that are possible
  42. * candidates based on your field as a source of synonyms. This method is
  43. * void, however, you have to alter and add your condition to $query
  44. * parameter.
  45. *
  46. * @param string $tag
  47. * What user has typed in into autocomplete widget. Normally you would
  48. * run LIKE '%$tag%' on your column
  49. * @param EntityFieldQuery $query
  50. * EntityFieldQuery object where you should put your conditions to
  51. * @param array $field
  52. * Array of field definition according to Field API
  53. * @param array $instance
  54. * Array of instance definition according to Field API
  55. */
  56. static public abstract function processEntityFieldQuery($tag, EntityFieldQuery $query, $field, $instance);
  57. /**
  58. * Add an entity as a synonym into a field of another entity.
  59. *
  60. * Basically this method should be called when you want to add some entity
  61. * as a synonym to another entity (for example when you merge one entity
  62. * into another and besides merging want to add synonym of the merging
  63. * entity into the trunk entity). You should extract synonym value (according
  64. * to what value is expected in this field) and return it. We try to provide
  65. * you with as much of context as possible, but normally you would only need
  66. * $synonym_entity and $synonym_entity_type parameters. Return an empty array
  67. * if entity of type $synonym_entity_type cannot be converted into a format
  68. * expected by $field.
  69. *
  70. * @param array $items
  71. * Array items that already exist in the field into which new synonyms is to
  72. * be added
  73. * @param array $field
  74. * Field array definition according to Field API of the field into which new
  75. * synonym is to be added
  76. * @param array $instance
  77. * Instance array definition according to Field API of the instance into
  78. * which new synonym is to be added
  79. * @param object $synonym_entity
  80. * Fully loaded entity object which has to be added as synonym
  81. * @param string $synonym_entity_type
  82. * Entity type of $synonym_entity
  83. *
  84. * @return array
  85. * Array of extra items to be merged into the items that already exist in
  86. * field values
  87. */
  88. static public abstract function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type);
  89. /**
  90. * Supportive method.
  91. *
  92. * Set such a condition on $query that it will always yield no results. Should
  93. * be called from $this->processEntityFieldQuery() when for whatever reason
  94. * the object can't alter $query to include matched synonyms. As a fallback
  95. * it should call this method to make sure it filtered everything out.
  96. *
  97. * @param EntityFieldQuery $query
  98. * Query object passed to $this->processEntityFieldQuery() method
  99. * @param array $field
  100. * Field array passed to $this->processEntityFieldQuery() method
  101. */
  102. static protected function emptyResultsCondition(EntityFieldQuery $query) {
  103. $query->entityCondition('entity_id', -1);
  104. }
  105. }