synonyms.api.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * @file
  4. * Documentation for Synonyms module.
  5. */
  6. /**
  7. * Collect info about available synonyms behavior implementations.
  8. *
  9. * If your module ships a synonyms behavior implementation you probably want to
  10. * implement this hook. However, exercise caution, if your synonyms behavior
  11. * implementation is a field-based one, you might be better off implementing
  12. * hook_synonyms_field_behavior_implementation_info().
  13. *
  14. * @param string $entity_type
  15. * Entity type whose synonyms behavior implementations are requested
  16. * @param string $bundle
  17. * Bundle name whose synonyms behavior implementations are requested
  18. * @param string $behavior
  19. * Behavior name whose implementations are requested
  20. *
  21. * @return array
  22. * Array of information about synonyms behavior implementations your module
  23. * exposes. Each sub array will represent a single synonyms behavior
  24. * implementation and should have the following structure:
  25. * - provider: (string) machine name of your synonyms behavior implementation.
  26. * Prefix it with your module name to make sure no name collision happens.
  27. * Also, provider must be unique within the namespace of behavior, entity
  28. * type and bundle. Basically, this is what distinguishes one behavior
  29. * implementation from another
  30. * - label: (string) Human friendly translated name of your synonyms behavior
  31. * implementation
  32. * - class: (string) Name of PHP class that implements synonyms behavior
  33. * interface, which is stated in synonyms behavior definition. This class
  34. * will do all the synonyms work. This hook serves pure declarative function
  35. * to map entity types, bundles with their synonym behavior implementations
  36. * whereas real "synonyms-related" work is implemented in your class
  37. */
  38. function hook_synonyms_behavior_implementation_info($entity_type, $bundle, $behavior) {
  39. $providers = array();
  40. switch ($entity_type) {
  41. case 'entity_type_i_want':
  42. switch ($bundle) {
  43. case 'bundle_i_want':
  44. switch ($behavior) {
  45. case 'behavior_i_want':
  46. $providers[] = array(
  47. 'provider' => 'my_module_synonyms_behavior_implementation_machine_name',
  48. 'label' => t('This is human friendly name of my synonyms behavior implementation. Put something meaningful here'),
  49. 'class' => 'MySynonymsSynonymsBehavior',
  50. );
  51. break;
  52. }
  53. break;
  54. }
  55. break;
  56. }
  57. return $providers;
  58. }
  59. /**
  60. * Example of synonyms behavior implementation class.
  61. *
  62. * You are encouraged to extend AbstractSynonymsBehavior class as that one
  63. * contains a few heuristic that make your implementation easier.
  64. */
  65. class MySynonymsSynonymsBehavior extends AbstractSynonymsBehavior implements AutocompleteSynonymsBehavior {
  66. /**
  67. * Extract synonyms from an entity within a specific behavior implementation.
  68. *
  69. * @param object $entity
  70. * Entity from which to extract synonyms
  71. * @param string $langcode
  72. * Language code for which to extract synonyms from the entity, if one is
  73. * known
  74. *
  75. * @return array
  76. * Array of synonyms extracted from $entity
  77. */
  78. public function extractSynonyms($entity, $langcode = NULL) {
  79. $synonyms = array();
  80. // Do something with $entity in order to extract synonyms from it. Add all
  81. // those synonyms into your $synonyms array.
  82. return $synonyms;
  83. }
  84. /**
  85. * Add an entity as a synonym into another entity.
  86. *
  87. * Basically this method should be called when you want to add some entity
  88. * as a synonym to another entity (for example when you merge one entity
  89. * into another and besides merging want to add synonym of the merged entity
  90. * into the trunk entity). You should update $trunk_entity in such a way that
  91. * it holds $synonym_entity as a synonym (it all depends on how data is stored
  92. * in your behavior implementation, but probably you will store entity label
  93. * or its ID as you cannot literaly store an entity inside of another entity).
  94. * If entity of type $synonym_entity_type cannot be converted into a format
  95. * expected by your behavior implementation, just do nothing.
  96. *
  97. * @param object $trunk_entity
  98. * Entity into which another one should be added as synonym
  99. * @param object $synonym_entity
  100. * Fully loaded entity object which has to be added as synonym
  101. * @param string $synonym_entity_type
  102. * Entity type of $synonym_entity
  103. */
  104. public function mergeEntityAsSynonym($trunk_entity, $synonym_entity, $synonym_entity_type) {
  105. // If you can add $synonym_entity into $trunk_entity, then do so.
  106. // For example:
  107. $trunk_entity->synonym_storage[] = $synonym_entity;
  108. }
  109. /**
  110. * Look up entities by their synonyms within a behavior implementation.
  111. *
  112. * You are provided with a SQL condition that you should apply to the storage
  113. * of synonyms within the provided behavior implementation. And then return
  114. * result: what entities match by the provided condition through what
  115. * synonyms.
  116. *
  117. * @param QueryConditionInterface $condition
  118. * Condition that defines what to search for. Apart from normal SQL
  119. * conditions as known in Drupal, it may contain the following placeholders:
  120. * - AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER: to denote
  121. * synonyms column which you should replace with the actual column name
  122. * where the synonyms data for your provider is stored in plain text.
  123. * - AbstractSynonymsBehavior::COLUMN_ENTITY_ID_PLACEHOLDER: to denote
  124. * column that holds entity ID. You are supposed to replace this
  125. * placeholder with actual column name that holds entity ID in your case.
  126. * For ease of work with these placeholders, you may extend the
  127. * AbstractSynonymsBehavior class and then just invoke the
  128. * AbstractSynonymsBehavior->synonymsFindProcessCondition() method, so you
  129. * won't have to worry much about it. Important note: if you plan on
  130. * re-using the same $condition object for multiple invocations of this
  131. * method you must pass in here a clone of your condition object, since the
  132. * internal implementation of this method will change the condition (will
  133. * swap the aforementioned placeholders with actual column names)
  134. *
  135. * @return Traversable
  136. * Traversable result set of found synonyms and entity IDs to which those
  137. * belong. Each element in the result set should be an object and will have
  138. * the following structure:
  139. * - synonym: (string) Synonym that was found and which satisfies the
  140. * provided condition
  141. * - entity_id: (int) ID of the entity to which the found synonym belongs
  142. */
  143. public function synonymsFind(QueryConditionInterface $condition) {
  144. // Here, as an example, we'll query an imaginary table where your module
  145. // supposedly keeps synonyms. We'll also use helpful
  146. // AbstractSynonymsBehavior::synonymsFindProcessCondition() to normalize
  147. // $condition argument.
  148. $query = db_select('my_synonyms_storage_table', 'table');
  149. $query->addField('table', 'entity_id', 'entity_id');
  150. $query->addField('table', 'synonym', 'synonym');
  151. $this->synonymsFindProcessCondition($condition, 'table.synonym', 'table.entity_id');
  152. $query->condition($condition);
  153. return $query->execute();
  154. }
  155. /**
  156. * Collect info on features pipe during invocation of hook_features_export().
  157. *
  158. * If your synonyms provider depends on some other features components, this
  159. * method should return them.
  160. *
  161. * @return array
  162. * Array of features pipe as per hook_features_export() specification
  163. */
  164. public function featuresExportPipe() {
  165. $pipe = parent::featuresExportPipe();
  166. // Here you can add any additional features components your provider
  167. // depends on.
  168. return $pipe;
  169. }
  170. }