context.core.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. /**
  3. * Implementation of hook_help().
  4. */
  5. function context_help($path, $arg) {
  6. switch ($path) {
  7. case 'admin/help#context':
  8. $output = file_get_contents(drupal_get_path('module', 'context') . '/README.txt');
  9. return module_exists('markdown') ? filter_xss_admin(module_invoke('markdown', 'filter', 'process', 0, -1, $output)) : '<pre>' . check_plain($output) . '</pre>';
  10. }
  11. }
  12. /**
  13. * Implementation of hook_theme().
  14. */
  15. function context_theme() {
  16. $items = array();
  17. if (!module_exists('block')) {
  18. $items['block'] = array(
  19. 'render element' => 'elements',
  20. 'template' => 'block',
  21. 'path' => drupal_get_path('module', 'block'),
  22. 'file' => 'block.module',
  23. 'template' => 'block',
  24. );
  25. }
  26. $items['context_block_form'] = array(
  27. 'render element' => 'form',
  28. 'path' => drupal_get_path('module', 'context') . '/theme',
  29. 'file' => 'context_reaction_block.theme.inc',
  30. );
  31. $items['context_block_regions_form'] = array(
  32. 'render element' => 'form',
  33. 'path' => drupal_get_path('module', 'context') . '/theme',
  34. 'file' => 'context_reaction_block.theme.inc',
  35. );
  36. $items['context_block_editor'] = array(
  37. 'render element' => 'form',
  38. 'path' => drupal_get_path('module', 'context') . '/theme',
  39. 'file' => 'context_reaction_block.theme.inc',
  40. );
  41. $items['context_block_browser'] = array(
  42. 'variables' => array('blocks' => array(), 'context' => array()),
  43. 'path' => drupal_get_path('module', 'context') . '/theme',
  44. 'template' => 'context-block-browser',
  45. 'file' => 'context_reaction_block.theme.inc',
  46. );
  47. $items['context_block_browser_item'] = array(
  48. 'variables' => array('block' => array()),
  49. 'path' => drupal_get_path('module', 'context') . '/theme',
  50. 'template' => 'context-block-browser-item',
  51. 'file' => 'context_reaction_block.theme.inc',
  52. );
  53. $items['context_block_script_placeholder'] = array(
  54. 'variables' => array('text' => NULL),
  55. 'path' => drupal_get_path('module', 'context') . '/theme',
  56. 'file' => 'context_reaction_block.theme.inc',
  57. );
  58. $items['context_block_edit_wrap'] = array(
  59. 'render element' => 'element',
  60. 'path' => drupal_get_path('module', 'context') . '/theme',
  61. 'file' => 'context_reaction_block.theme.inc',
  62. );
  63. return $items;
  64. }
  65. /**
  66. * Implementation of hook_theme_registry_alter().
  67. */
  68. function context_theme_registry_alter(&$theme_registry) {
  69. // Push theme_page() through a context_preprocess to provide
  70. // context-sensitive menus and variables. Ensure that
  71. // context_preprocess_page() comes immediately after
  72. // template_preprocess_page().
  73. $position = array_search('context_preprocess_page', $theme_registry['page']['preprocess functions']);
  74. if ($position !== FALSE) {
  75. unset($theme_registry['page']['preprocess functions'][$position]);
  76. }
  77. // Prevent conflict with i18n_menu.
  78. if (module_exists('i18n_menu')) {
  79. $position = array_search('i18n_menu_preprocess_page', $theme_registry['page']['preprocess functions']);
  80. }
  81. else {
  82. $position = array_search('template_preprocess_page', $theme_registry['page']['preprocess functions']);
  83. }
  84. $position = $position ? $position + 1 : 2;
  85. array_splice($theme_registry['page']['preprocess functions'], $position, 0, 'context_preprocess_page');
  86. }
  87. /**
  88. * Implementation of hook_ctools_render_alter().
  89. * Used to detect the presence of a page manager node view or node form.
  90. */
  91. function context_ctools_render_alter($info, $page, $data) {
  92. extract($data);
  93. if ($page && in_array($task['name'], array('node_view', 'node_edit'), TRUE)) {
  94. foreach ($contexts as $ctools_context) {
  95. if (in_array('node', $ctools_context->type) && !empty($ctools_context->data)) {
  96. context_node_condition($ctools_context->data, $task['name'] === 'node_view' ? 'view' : 'form');
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * Implementation of hook_entity_prepare_view().
  104. */
  105. function context_entity_prepare_view($prepare, $entity_type) {
  106. if ($entity_type === 'taxonomy_term' && count($prepare) === 1) {
  107. $term = reset($prepare);
  108. $menu = menu_get_object('taxonomy_term', 2);
  109. if ($menu && $term->tid == $menu->tid && $plugin = context_get_plugin('condition', 'taxonomy_term')) {
  110. $plugin->execute($term, 'view');
  111. }
  112. }
  113. }
  114. /**
  115. * Implementation of hook_node_view().
  116. */
  117. function context_node_view($node, $view_mode) {
  118. $object = menu_get_object();
  119. if (isset($object->nid) && $object->nid === $node->nid) {
  120. context_node_condition($node, 'view');
  121. }
  122. }
  123. /**
  124. * Implementation of hook_form_alter().
  125. */
  126. function context_form_alter(&$form, $form_state, $form_id) {
  127. // If the form is an admin for, flag it so that we can force a rebuild if needed.
  128. if (path_is_admin($_GET['q'])) {
  129. $form['#submit'][] = 'context_admin_form_submit';
  130. }
  131. // Trigger the condition in an after_build function to avoid being skipped
  132. // when there are validation errors.
  133. $form['#after_build'][] = 'context_form_alter_node_after_build';
  134. }
  135. /**
  136. * Form #after_build callback for context_form_alter().
  137. */
  138. function context_form_alter_node_after_build($form, &$form_state) {
  139. // Prevent this from firing on admin pages... damn form driven apis...
  140. if (!empty($form['#node_edit_form']) && arg(0) != 'admin') {
  141. context_node_condition($form['#node'], 'form');
  142. }
  143. return $form;
  144. }
  145. /**
  146. * Clear out block info cache when an admin area form is submitted.
  147. */
  148. function context_admin_form_submit(&$form, $form_state) {
  149. if ($plugin = context_get_plugin('reaction', 'block')) {
  150. $plugin->rebuild_needed(TRUE);
  151. }
  152. }
  153. /**
  154. * Centralized node condition call function for the ever increasing number of
  155. * ways to get at a node view / node form.
  156. */
  157. function context_node_condition(&$node, $op) {
  158. if ($plugin = context_get_plugin('condition', 'node')) {
  159. $plugin->execute($node, $op);
  160. }
  161. if (module_exists('taxonomy')) {
  162. if ($plugin = context_get_plugin('condition', 'node_taxonomy')) {
  163. $plugin->execute($node, $op);
  164. }
  165. }
  166. if (module_exists('book')) {
  167. if ($plugin = context_get_plugin('condition', 'book')) {
  168. $plugin->execute($node, $op);
  169. }
  170. if ($plugin = context_get_plugin('condition', 'bookroot')) {
  171. $plugin->execute($node, $op);
  172. }
  173. }
  174. // Allow other plugins to easily be triggered on node-related events.
  175. drupal_alter('context_node_condition', $node, $op);
  176. }
  177. /**
  178. * Implementation of hook_form_alter() for system_modules_form.
  179. */
  180. function context_form_system_modules_form_alter(&$form, $form_state) {
  181. context_invalidate_cache();
  182. }
  183. /**
  184. * Implementation of hook_form_alter() for user_profile_form.
  185. */
  186. function context_form_user_profile_form_alter(&$form, $form_state) {
  187. if ($plugin = context_get_plugin('condition', 'user_page')) {
  188. $plugin->execute($form['#user'], 'form');
  189. }
  190. }
  191. /**
  192. * Implementation of hook_form_alter() for user_register_form.
  193. */
  194. function context_form_user_register_form_alter(&$form, $form_state) {
  195. if ($plugin = context_get_plugin('condition', 'user_page')) {
  196. $plugin->execute($form['#user'], 'register');
  197. }
  198. }
  199. /**
  200. * Implementation of hook_form_alter() for comment_form.
  201. */
  202. function context_form_comment_form_alter(&$form, $form_state) {
  203. if ($nid = $form['nid']['#value']) {
  204. $node = node_load($nid);
  205. context_node_condition($node, 'comment');
  206. }
  207. }
  208. /**
  209. * Implementation of hook_views_pre_view().
  210. */
  211. function context_views_pre_view($view, $display) {
  212. if ($plugin = context_get_plugin('condition', 'views')) {
  213. $plugin->execute($view);
  214. }
  215. // Support Views overrides of specific entity paths.
  216. if ($view->display_handler->has_path()) {
  217. switch ($view->display_handler->get_option('path')) {
  218. case 'taxonomy/term/%':
  219. if (($term = taxonomy_term_load(arg(2))) && ($plugin = context_get_plugin('condition', 'taxonomy_term'))) {
  220. $plugin->execute($term, 'view');
  221. }
  222. break;
  223. case 'node/%':
  224. if ($node = node_load(arg(1))) {
  225. context_node_condition($node, 'view');
  226. }
  227. break;
  228. case 'user/%':
  229. if (($account = user_load(arg(1))) && ($plugin = context_get_plugin('condition', 'user_page'))) {
  230. $plugin->execute($account, 'view');
  231. }
  232. break;
  233. }
  234. }
  235. }
  236. /**
  237. * Implementation of hook_user().
  238. */
  239. function context_user_view($account, $view_mode) {
  240. if ($view_mode === 'full' && $plugin = context_get_plugin('condition', 'user_page')) {
  241. $plugin->execute($account, 'view');
  242. }
  243. }
  244. /**
  245. * Implements hook_page_build().
  246. */
  247. function context_page_build(&$page) {
  248. module_invoke_all('context_page_condition');
  249. module_invoke_all('context_page_reaction');
  250. if ($plugin = context_get_plugin('reaction', 'block')) {
  251. $plugin->execute($page);
  252. }
  253. // See block_page_build. Clear static cache b/c in overlay form submissions
  254. // hook_page_build can get called more than once per page load.
  255. drupal_static_reset('context_reaction_block_list');
  256. }
  257. /**
  258. * THEME FUNCTIONS & RELATED ==========================================
  259. */
  260. /**
  261. * Generates an array of links (suitable for use with theme_links)
  262. * to the node forms of types associated with current active contexts.
  263. */
  264. function context_links($reset = FALSE) {
  265. static $links;
  266. if (!$links || $reset) {
  267. $contexts = context_active_contexts();
  268. $active_types = array();
  269. $conditions = array('node', 'bookroot');
  270. foreach ($conditions as $condition) {
  271. foreach ($contexts as $k => $v) {
  272. if (!empty($v->conditions[$condition]['values'])) {
  273. $active_types = array_merge($active_types, array_filter($v->conditions[$condition]['values']));
  274. }
  275. }
  276. }
  277. $links = array();
  278. if (!empty($active_types)) {
  279. // Iterate over active contexts
  280. foreach ($active_types as $type) {
  281. $add_url = 'node/add/' . str_replace('_', '-', $type);
  282. $item = menu_get_item($add_url);
  283. if ($item && $item['access'] && strpos($_GET['q'], $add_url) !== 0) {
  284. $links[$type] = array('title' => t('Add @type', array('@type' => node_type_get_name($type))), 'href' => $add_url);
  285. }
  286. }
  287. }
  288. drupal_alter('context_links', $links);
  289. uasort($links, 'element_sort');
  290. }
  291. return $links;
  292. }
  293. /**
  294. * Implementation of hook_context_page_condition().
  295. */
  296. function context_context_page_condition() {
  297. if ($plugin = context_get_plugin('condition', 'menu')) {
  298. $plugin->execute();
  299. }
  300. if ($plugin = context_get_plugin('condition', 'default')) {
  301. $plugin->execute(1);
  302. }
  303. if ($plugin = context_get_plugin('condition', 'context')) {
  304. $plugin->execute();
  305. }
  306. if ($plugin = context_get_plugin('condition', 'context_all')) {
  307. $plugin->execute();
  308. }
  309. }
  310. /**
  311. * Implementation of hook_context_page_reaction().
  312. */
  313. function context_context_page_reaction() {
  314. if ($plugin = context_get_plugin('reaction', 'css_injector')) {
  315. $plugin->execute();
  316. }
  317. if ($plugin = context_get_plugin('reaction', 'debug')) {
  318. $plugin->execute();
  319. }
  320. }
  321. /**
  322. * Implementation of hook_preprocess_page().
  323. */
  324. function context_preprocess_page(&$vars) {
  325. if ($plugin = context_get_plugin('reaction', 'theme')) {
  326. $plugin->execute($vars);
  327. }
  328. if ($plugin = context_get_plugin('reaction', 'template_suggestions')) {
  329. $plugin->execute($vars);
  330. }
  331. /*
  332. if ($context_links = context_links()) {
  333. $vars['context_links'] = theme('links', $context_links);
  334. }
  335. else {
  336. $vars['context_links'] = '';
  337. }
  338. */
  339. }
  340. /**
  341. * Implementation of hook_delivery_callback_alter().
  342. * Based on menu_position's and menu_trail_by_path's implementations.
  343. */
  344. function context_page_delivery_callback_alter() {
  345. if ($plugin = context_get_plugin('reaction', 'menu')) {
  346. $plugin->execute();
  347. }
  348. if ($plugin = context_get_plugin('reaction', 'breadcrumb')) {
  349. $plugin->execute();
  350. }
  351. }
  352. /**
  353. * Implementation of hook_preprocess_html().
  354. */
  355. function context_preprocess_html(&$vars) {
  356. if ($plugin = context_get_plugin('reaction', 'theme_html')) {
  357. $plugin->execute($vars);
  358. }
  359. }