synonyms.api.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @file
  4. * Documentation for Synonyms module.
  5. */
  6. /**
  7. * Hook to collect info about available synonyms behavior implementations.
  8. *
  9. * Hook to collect info about what PHP classes implement provided synonyms
  10. * behavior for different field types.
  11. *
  12. * @param string $behavior
  13. * Name of a synonyms behavior. This string will always be among the keys
  14. * of the return of synonyms_behaviors(), i.e. name of a ctools plugin
  15. *
  16. * @return array
  17. * Array of information about what synonyms behavior implementations your
  18. * module supplies. The return array must contain field types as keys, whereas
  19. * corresponding values should be names of PHP classes that implement the
  20. * provided behavior for that field type. Read more about how to implement a
  21. * specific behavior in the advanced help of this module. In a few words: you
  22. * will have to implement an interface that is defined in the behavior
  23. * definition. Do not forget to make sure your PHP class is visible to Drupal
  24. * auto discovery mechanism
  25. */
  26. function hook_synonyms_behavior_implementation_info($behavior) {
  27. switch ($behavior) {
  28. case 'autocomplete':
  29. return array(
  30. 'my-field-type' => 'MyFieldTypeAutocompleteSynonymsBehavior',
  31. );
  32. break;
  33. case 'another-behavior':
  34. return array(
  35. 'my-field-type-or-yet-another-field-type' => 'MyFieldTypeAnotherBehaviorSynonymsBehavior',
  36. );
  37. break;
  38. }
  39. return array();
  40. }
  41. /**
  42. * Hook to alter info about available synonyms behavior implementations.
  43. *
  44. * This hook is invoked right after hook_synonyms_behavior_implementation_info()
  45. * and is designed to let modules overwrite implementation info from some other
  46. * modules. For example, if module A provides implementation for some field
  47. * type, but your module has a better version of that implementation, you would
  48. * need to implement this hook and to overwrite the implementation info.
  49. *
  50. * @param array $info
  51. * Array of information about existing synonyms behavior implementations that
  52. * was collected from modules
  53. * @param string $behavior
  54. * Name of the behavior for which the info about implementation is being
  55. * generated
  56. */
  57. function hook_synonyms_behavior_implementation_info_alter(&$info, $behavior) {
  58. switch ($behavior) {
  59. case 'the-behavior-i-want':
  60. $info['the-field-type-i-want'] = 'MyFieldTypeAutocompleteSynonymsBehavior';
  61. break;
  62. }
  63. }
  64. /**
  65. * Example of how to implement a synonyms behavior for an arbitrary field type.
  66. */
  67. class MyFieldTypeAutocompleteSynonymsBehavior extends AbstractSynonymsSynonymsBehavior implements AutocompleteSynonymsBehavior {
  68. public function extractSynonyms($items, $field, $instance, $entity, $entity_type) {
  69. // Let's say our synonyms is stored in the 'foo' column of the field.
  70. $synonyms = array();
  71. foreach ($items as $item) {
  72. $synonyms[] = $item['foo'];
  73. }
  74. return $synonyms;
  75. }
  76. public function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type) {
  77. // Let's say we keep the synonyms as strings and under the 'foo' column, to
  78. // keep it consistent with the extractSynonyms() method.
  79. $label = entity_label($synonym_entity_type, $synonym_entity);
  80. return array(array(
  81. 'foo' => $label,
  82. ));
  83. }
  84. public function synonymItemHash($item, $field, $instance) {
  85. // Since we've agreed that the column that stores data in our imaginary
  86. // field type is "foo". Then it suffices just to implement the hash function
  87. // as the value of foo column.
  88. return $item['foo'];
  89. }
  90. public function synonymsFind(QueryConditionInterface $condition, $field, $instance) {
  91. // We only can find synonyms in SQL storage. If this field is not one, then
  92. // we have full right to throw an exception.
  93. if ($field['storage']['type'] != 'field_sql_storage') {
  94. throw new SynonymsSynonymsBehaviorException(t('Not supported storage engine %type in synonymsFind() method.', array(
  95. '%type' => $field['storage']['type'],
  96. )));
  97. }
  98. // Now we will figure out in what table $field is stored. We want to
  99. // condition the 'foo' column within that field table.
  100. $table = array_keys($field['storage']['details']['sql'][FIELD_LOAD_CURRENT]);
  101. $table = reset($table);
  102. $column = $field['storage']['details']['sql'][FIELD_LOAD_CURRENT][$table]['foo'];
  103. // Once we know full path to the column that stores synonyms as plain text,
  104. // we can use a supplementary method from AbstractSynonymsSynonymsBehavior,
  105. // which helps us to convert a column placeholder into its real value within
  106. // the condition we have received from outside.
  107. $this->synonymsFindProcessCondition($condition, $column);
  108. // Now we are all set to build a SELECT query and return its result set.
  109. $query = db_select($table);
  110. $query->fields($table, array('entity_id'));
  111. $query->addField($table, $column, 'synonym');
  112. return $query->condition($condition)
  113. ->condition('entity_type', $instance['entity_type'])
  114. ->condition('bundle', $instance['bundle'])
  115. ->execute();
  116. }
  117. }