synonyms.api.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * @file
  4. * Documentation for Synonyms module.
  5. */
  6. /**
  7. * Provide Synonyms module with names of synonyms extractor classes.
  8. *
  9. * Provide Synonyms module with names of classes that are able to extract
  10. * synonyms from fields. Each of the provided classes should extend
  11. * AbstractSynonymsExtractor base class.
  12. *
  13. * @return array
  14. * Array of strings, where each value is a name of synonyms extractor class
  15. */
  16. function hook_synonyms_extractor_info() {
  17. return array(
  18. // Please see below the defintion of ApiSynonymsSynonymsExtractor class
  19. // for your reference.
  20. 'ApiSynonymsSynonymsExtractor',
  21. );
  22. }
  23. /**
  24. * Dummy synonyms extractor class for documentation purposes.
  25. *
  26. * This is a copy of SynonymsSynonymsExtractor class providing an example of
  27. * how to write your own synonyms extractor class. See the definition of
  28. * AbstractSynonymsExtractor for reference and incode comments. For more
  29. * complicated examples take a look at EntityReferenceSynonymsExtractor class.
  30. */
  31. class ApiSynonymsSynonymsExtractor extends AbstractSynonymsExtractor {
  32. /**
  33. * Return array of supported field types for synonyms extraction.
  34. *
  35. * @return array
  36. * Array of Field API field types from which this class is able to extract
  37. * synonyms
  38. */
  39. static public function fieldTypesSupported() {
  40. return array('text', 'number_integer', 'number_float', 'number_decimal');
  41. }
  42. /**
  43. * Extract synonyms from a field attached to an entity.
  44. *
  45. * We try to pass as many info about context as possible, however, normally
  46. * you will only need $items to extract the synonyms.
  47. *
  48. * @param array $items
  49. * Array of items
  50. * @param array $field
  51. * Array of field definition according to Field API
  52. * @param array $instance
  53. * Array of instance definition according to Field API
  54. * @param object $entity
  55. * Fully loaded entity object to which the $field and $instance with $item
  56. * values is attached to
  57. * @param string $entity_type
  58. * Type of the entity $entity according to Field API definition of entity
  59. * types
  60. *
  61. * @return array
  62. * Array of synonyms extracted from $items
  63. */
  64. static public function synonymsExtract($items, $field, $instance, $entity, $entity_type) {
  65. $synonyms = array();
  66. foreach ($items as $item) {
  67. $synonyms[] = $item['value'];
  68. }
  69. return $synonyms;
  70. }
  71. /**
  72. * Allow you to hook in during autocomplete suggestions generation.
  73. *
  74. * Allow you to include entities for autocomplete suggestion that are possible
  75. * candidates based on your field as a source of synonyms. This method is
  76. * void, however, you have to alter and add your condition to $query
  77. * parameter.
  78. *
  79. * @param string $tag
  80. * What user has typed in into autocomplete widget. Normally you would
  81. * run LIKE '%$tag%' on your column
  82. * @param EntityFieldQuery $query
  83. * EntityFieldQuery object where you should add your conditions to
  84. * @param array $field
  85. * Array of field definition according to Field API, autocomplete on which
  86. * is fired
  87. * @param array $instance
  88. * Array of instance definition according to Field API, autocomplete on
  89. * which is fired
  90. */
  91. static public function processEntityFieldQuery($tag, EntityFieldQuery $query, $field, $instance) {
  92. $query->fieldCondition($field, 'value', '%' . $tag . '%', 'LIKE');
  93. }
  94. /**
  95. * Add an entity as a synonym into a field of another entity.
  96. *
  97. * Basically this method should be called when you want to add some entity
  98. * as a synonym to another entity (for example when you merge one entity
  99. * into another and besides merging want to add synonym of the merging
  100. * entity into the trunk entity). You should extract synonym value (according
  101. * to what value is expected in this field) and return it. We try to provide
  102. * you with as much context as possible, but normally you would only need
  103. * $synonym_entity and $synonym_entity_type parameters. Return an empty array
  104. * if entity of type $synonym_entity_type cannot be converted into a format
  105. * expected by $field.
  106. *
  107. * @param array $items
  108. * Array items that already exist in the field into which new synonyms is to
  109. * be added
  110. * @param array $field
  111. * Field array definition according to Field API of the field into which new
  112. * synonym is to be added
  113. * @param array $instance
  114. * Instance array definition according to Field API of the instance into
  115. * which new synonym is to be added
  116. * @param object $synonym_entity
  117. * Fully loaded entity object which has to be added as synonym
  118. * @param string $synonym_entity_type
  119. * Entity type of $synonym_entity
  120. *
  121. * @return array
  122. * Array of extra items to be merged into the items that already exist in
  123. * field values
  124. */
  125. static public function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type) {
  126. $synonym = entity_label($synonym_entity_type, $synonym_entity);
  127. switch ($field['type']) {
  128. case 'text':
  129. break;
  130. // We add synonyms for numbers only if $synonym is a number.
  131. case 'number_integer':
  132. case 'number_float':
  133. case 'number_decimal':
  134. if (!is_numeric($synonym)) {
  135. return array();
  136. }
  137. break;
  138. }
  139. return array(array(
  140. 'value' => $synonym,
  141. ));
  142. }
  143. }