synonyms.api.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. *
  72. * @return array
  73. * Array of synonyms extracted from $entity
  74. */
  75. public function extractSynonyms($entity) {
  76. $synonyms = array();
  77. // Do something with $entity in order to extract synonyms from it. Add all
  78. // those synonyms into your $synonyms array.
  79. return $synonyms;
  80. }
  81. /**
  82. * Add an entity as a synonym into another entity.
  83. *
  84. * Basically this method should be called when you want to add some entity
  85. * as a synonym to another entity (for example when you merge one entity
  86. * into another and besides merging want to add synonym of the merged entity
  87. * into the trunk entity). You should update $trunk_entity in such a way that
  88. * it holds $synonym_entity as a synonym (it all depends on how data is stored
  89. * in your behavior implementation, but probably you will store entity label
  90. * or its ID as you cannot literaly store an entity inside of another entity).
  91. * If entity of type $synonym_entity_type cannot be converted into a format
  92. * expected by your behavior implementation, just do nothing.
  93. *
  94. * @param object $trunk_entity
  95. * Entity into which another one should be added as synonym
  96. * @param object $synonym_entity
  97. * Fully loaded entity object which has to be added as synonym
  98. * @param string $synonym_entity_type
  99. * Entity type of $synonym_entity
  100. */
  101. public function mergeEntityAsSynonym($trunk_entity, $synonym_entity, $synonym_entity_type) {
  102. // If you can add $synonym_entity into $trunk_entity, then do so.
  103. // For example:
  104. $trunk_entity->synonym_storage[] = $synonym_entity;
  105. }
  106. /**
  107. * Look up entities by their synonyms within a behavior implementation.
  108. *
  109. * You are provided with a SQL condition that you should apply to the storage
  110. * of synonyms within the provided behavior implementation. And then return
  111. * result: what entities match by the provided condition through what
  112. * synonyms.
  113. *
  114. * @param QueryConditionInterface $condition
  115. * Condition that defines what to search for. Apart from normal SQL
  116. * conditions as known in Drupal, it may contain the following placeholders:
  117. * - AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER: to denote
  118. * synonyms column which you should replace with the actual column name
  119. * where the synonyms data for your provider is stored in plain text.
  120. * - AbstractSynonymsBehavior::COLUMN_ENTITY_ID_PLACEHOLDER: to denote
  121. * column that holds entity ID. You are supposed to replace this placeholder
  122. * with actual column name that holds entity ID in your case.
  123. * For ease of work with these placeholders, you may extend the
  124. * AbstractSynonymsBehavior class and then just invoke the
  125. * AbstractSynonymsBehavior->synonymsFindProcessCondition() method, so you
  126. * won't have to worry much about it
  127. *
  128. * @return Traversable
  129. * Traversable result set of found synonyms and entity IDs to which those
  130. * belong. Each element in the result set should be an object and will have
  131. * the following structure:
  132. * - synonym: (string) Synonym that was found and which satisfies the
  133. * provided condition
  134. * - entity_id: (int) ID of the entity to which the found synonym belongs
  135. */
  136. public function synonymsFind(QueryConditionInterface $condition) {
  137. // Here, as an example, we'll query an imaginary table where your module
  138. // supposedly keeps synonyms. We'll also use helpful
  139. // AbstractSynonymsBehavior::synonymsFindProcessCondition() to normalize
  140. // $condition argument.
  141. $query = db_select('my_synonyms_storage_table', 'table');
  142. $query->addField('table', 'entity_id', 'entity_id');
  143. $query->addField('table', 'synonym', 'synonym');
  144. $this->synonymsFindProcessCondition($condition, 'table.synonym', 'table.entity_id');
  145. $query->condition($condition);
  146. return $query->execute();
  147. }
  148. /**
  149. * Collect info on features pipe during invocation of hook_features_export().
  150. *
  151. * If your synonyms provider depends on some other features components, this
  152. * method should return them.
  153. *
  154. * @return array
  155. * Array of features pipe as per hook_features_export() specification
  156. */
  157. public function featuresExportPipe() {
  158. $pipe = parent::featuresExportPipe();
  159. // Here you can add any additional features components your provider
  160. // depends on.
  161. return $pipe;
  162. }
  163. }