shs.api.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * @file
  4. * Hooks for the shs module.
  5. *
  6. * This file contains no working PHP code; it exists to provide additional
  7. * documentation for doxygen as well as to document hooks in the standard Drupal
  8. * manner.
  9. */
  10. /**
  11. * Alter information provided by shs_js() for the js module.
  12. *
  13. * @param array $settings
  14. * Settings for js module.
  15. *
  16. * @see hook_js()
  17. */
  18. function hook_shs_js_info_alter(array &$settings) {
  19. // Add pathauto as dependency for the default json callback.
  20. $settings['json']['dependencies'][] = 'pathauto';
  21. // Change the access callback.
  22. $settings['json']['access callback'] = 'my_custom_access';
  23. }
  24. /**
  25. * Alter permissions to create new terms using shs on a per-term level.
  26. *
  27. * @param int $vid
  28. * ID of vocabulary to create the term in.
  29. * @param int $parent
  30. * ID of parent term (0 for top level).
  31. * @param string $field
  32. * Name of field requesting the term list (DOM element name).
  33. * @param object $account
  34. * The user to check access for.
  35. *
  36. * @return bool
  37. * <code>FALSE</code> if no new terms should be created below the given
  38. * parent, otherwise <code>TRUE</code>.
  39. */
  40. function hook_shs_add_term_access($vid, $parent, $field, $account) {
  41. // Deny creating new terms on tid 0 and 3.
  42. if ($parent == 0 || $parent == 3) {
  43. return FALSE;
  44. }
  45. return TRUE;
  46. }
  47. /**
  48. * Alter the list of JSON callbacks supported by shs.
  49. *
  50. * JSON callback definitions are used to validate incoming data and function
  51. * requests (i.e. adding a new term).
  52. *
  53. * @param array $callbacks
  54. * List of callback definitions.
  55. */
  56. function hook_shs_json_callbacks_alter(array &$callbacks) {
  57. // Use custom callback for adding new terms.
  58. $callbacks['shs_json_term_add']['callback'] = 'my_custom_shs_json_term_add';
  59. }
  60. /**
  61. * Alter the list of terms within a level of the term hierarchy.
  62. *
  63. * @param array $terms
  64. * List of terms displayed to the user (single hierarchy level).
  65. * @param array $alter_options
  66. * - vid: ID of vocabulary or field name
  67. * - parent: ID of parent term
  68. * - settings: Additional settings (for example "language", etc.,)
  69. */
  70. function hook_shs_term_get_children_alter(array &$terms, array &$alter_options) {
  71. }
  72. /**
  73. * Alter Javascript settings of shs widgets in entity forms and views.
  74. *
  75. * @param array $settings_js
  76. * Javascript settings for shs widgets.
  77. * @param string $field_name
  78. * Name of field the provided settings are used for.
  79. * @param int|string $vocabulary_identifier
  80. * ID or machine_name of vocabulary the settings are used for.
  81. */
  82. function hook_shs_js_settings_alter(array &$settings_js, $field_name, $vocabulary_identifier) {
  83. if ($field_name == 'field_article_terms') {
  84. foreach ($settings_js['shs'] as $field => $container) {
  85. foreach ($container as $identifier => $settings) {
  86. $settings_js['shs'][$field][$identifier]['any_label'] = t('- Select an item -');
  87. }
  88. }
  89. }
  90. }
  91. /**
  92. * Alter Javascript settings for a single shs widget.
  93. *
  94. * @param array $settings_js
  95. * Javascript settings for the specified field.
  96. * @param string $field_name
  97. * Name of field the provided settings are used for.
  98. * @param int|string $vocabulary_identifier
  99. * ID or machine_name of vocabulary the settings are used for.
  100. */
  101. function hook_shs_FIELDNAME_js_settings_alter(array &$settings_js, $field_name, $vocabulary_identifier) {
  102. foreach ($settings_js['shs'] as $field => &$container) {
  103. foreach ($container as $identifier => &$settings) {
  104. // Define labels for each level.
  105. $settings['labels'] = array(
  106. // No label for first level.
  107. FALSE,
  108. t('Country'),
  109. t('City'),
  110. );
  111. // Small speed-up for anmiations (defaults to 400ms).
  112. $settings['display']['animationSpeed'] = 100;
  113. }
  114. }
  115. }