context.api.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by Context.
  5. */
  6. /**
  7. * CTools plugin API hook for Context. Note that a proper entry in
  8. * hook_ctools_plugin_api() must exist for this hook to be called.
  9. */
  10. function hook_context_plugins() {
  11. $plugins = array();
  12. $plugins['foo_context_condition_bar'] = array(
  13. 'handler' => array(
  14. 'path' => drupal_get_path('module', 'foo') .'/plugins',
  15. 'file' => 'foo_context_condition_bar.inc',
  16. 'class' => 'foo_context_condition_bar',
  17. 'parent' => 'context_condition',
  18. ),
  19. );
  20. $plugins['foo_context_reaction_baz'] = array(
  21. 'handler' => array(
  22. 'path' => drupal_get_path('module', 'foo') .'/plugins',
  23. 'file' => 'foo_context_reaction_baz.inc',
  24. 'class' => 'foo_context_reaction_baz',
  25. 'parent' => 'context_reaction',
  26. ),
  27. );
  28. return $plugins;
  29. }
  30. /**
  31. * Registry hook for conditions & reactions.
  32. *
  33. * Each entry associates a condition or reaction with the CTools plugin to be
  34. * used as its plugin class.
  35. */
  36. function hook_context_registry() {
  37. return array(
  38. 'conditions' => array(
  39. 'bar' => array(
  40. 'title' => t('Name of condition "bar"'),
  41. 'plugin' => 'foo_context_condition_bar',
  42. ),
  43. ),
  44. 'reactions' => array(
  45. 'baz' => array(
  46. 'title' => t('Name of reaction "baz"'),
  47. 'plugin' => 'foo_context_reaction_baz',
  48. ),
  49. ),
  50. );
  51. }
  52. /**
  53. * Execute Context page conditions
  54. *
  55. * Allows modules to hook into Context's hook_page_build to execute their
  56. * conditions at an appropriate time before the firing of reactions.
  57. */
  58. function hook_context_page_condition() {
  59. if ($plugin = context_get_plugin('condition', 'bar')) {
  60. $plugin->execute();
  61. }
  62. }
  63. /**
  64. * Execute Context page reactions
  65. *
  66. * Allows modules to hook into Context's hook_page_build to execute their
  67. * reactions at an appropriate time after the firing of conditions.
  68. */
  69. function hook_context_page_reaction() {
  70. if ($plugin = context_get_plugin('reaction', 'baz')) {
  71. $plugin->execute();
  72. }
  73. }
  74. /**
  75. * Alter the registry.
  76. *
  77. * Allows modules to alter the registry. Default plugins can be replaced by
  78. * custom ones declared in hook_context_plugins().
  79. *
  80. * @param $registry
  81. * The registry, passed by reference.
  82. */
  83. function hook_context_registry_alter(&$registry) {
  84. if (isset($registry['reactions']['baz'])) {
  85. $registry['reactions']['baz']['plugin'] = 'custom_context_reaction_baz';
  86. }
  87. }
  88. /**
  89. * Alter/add a condition to a node-related event.
  90. *
  91. * Allows modules to add one or more context condition plugin executions to a
  92. * node view, form, etc.
  93. *
  94. * @param $node
  95. * The node object.
  96. * @param $op
  97. * The node-related operation: 'node', 'form', 'comment'.
  98. */
  99. function hook_context_node_condition_alter(&$node, $op) {
  100. if ($plugin = context_get_plugin('condition', 'bar')) {
  101. $plugin->execute($node, $op);
  102. }
  103. }
  104. /**
  105. * Alter a context directly after it has been loaded. Allows modules to alter
  106. * a context object's reactions. While you may alter conditions, this will
  107. * generally have no effect as conditions are cached for performance and
  108. * contexts are loaded after conditions are checked, not before.
  109. *
  110. * @param &$context
  111. * The context object by reference.
  112. */
  113. function hook_context_load_alter(&$context) {
  114. if ($context->name === 'foo' && isset($context->reactions['block'])) {
  115. $context->reactions['block']['blocks']['locale-0'] = array(
  116. 'module' => 'locale',
  117. 'delta' => '0',
  118. 'region' => 'header',
  119. 'weight' => '2',
  120. );
  121. }
  122. }
  123. /**
  124. * Allows for finer grained access mechanisms to using the json
  125. * rendering capabilities of the block reaction when a user isn't
  126. * granted the administer contexts or context ajax block access
  127. * permission
  128. * @param $block_id
  129. * ID of block in module-delta format
  130. */
  131. function hook_context_allow_ajax_block_access($block_id) {
  132. }