AbstractSynonymsExtractor.class.inc 4.2 KB

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