context.core.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. // Check for page handlers.
  94. if ($page) {
  95. // Check for node page handler.
  96. if (in_array($task['name'], array('node_view', 'node_edit'), TRUE)) {
  97. foreach ($contexts as $ctools_context) {
  98. if (in_array('node', $ctools_context->type) && !empty($ctools_context->data)) {
  99. context_node_condition($ctools_context->data, $task['name'] === 'node_view' ? 'view' : 'form');
  100. break;
  101. }
  102. }
  103. }
  104. // Check for taxonomy term page handler.
  105. elseif (in_array($task['name'], array('term_view', 'term_edit'), TRUE)) {
  106. foreach ($contexts as $ctools_context) {
  107. if (in_array('taxonomy_term', $ctools_context->type) && !empty($ctools_context->data)) {
  108. if ($plugin = context_get_plugin('condition', 'taxonomy_term')) {
  109. $plugin->execute($ctools_context->data, $task['name'] === 'term_view' ? 'view' : 'form');
  110. }
  111. break;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. /**
  118. * Implementation of hook_entity_prepare_view().
  119. */
  120. function context_entity_prepare_view($prepare, $entity_type) {
  121. if ($entity_type === 'taxonomy_term' && count($prepare) === 1) {
  122. $term = reset($prepare);
  123. $menu = menu_get_object('taxonomy_term', 2);
  124. if ($menu && $term->tid == $menu->tid && $plugin = context_get_plugin('condition', 'taxonomy_term')) {
  125. $plugin->execute($term, 'view');
  126. }
  127. }
  128. }
  129. /**
  130. * Implementation of hook_node_view().
  131. */
  132. function context_node_view($node, $view_mode) {
  133. $object = menu_get_object();
  134. if (isset($object->nid) && $object->nid === $node->nid) {
  135. context_node_condition($node, 'view');
  136. }
  137. }
  138. /**
  139. * Implementation of hook_form_alter().
  140. */
  141. function context_form_alter(&$form, $form_state, $form_id) {
  142. // If the form is an admin for, flag it so that we can force a rebuild if needed.
  143. if (path_is_admin($_GET['q'])) {
  144. $form['#submit'][] = 'context_admin_form_submit';
  145. }
  146. // Trigger the condition in an after_build function to avoid being skipped
  147. // when there are validation errors.
  148. $form['#after_build'][] = 'context_form_alter_node_after_build';
  149. }
  150. /**
  151. * Form #after_build callback for context_form_alter().
  152. */
  153. function context_form_alter_node_after_build($form, &$form_state) {
  154. // Prevent this from firing on admin pages... damn form driven apis...
  155. if (!empty($form['#node_edit_form']) && arg(0) != 'admin') {
  156. context_node_condition($form['#node'], 'form');
  157. }
  158. return $form;
  159. }
  160. /**
  161. * Clear out block info cache when an admin area form is submitted.
  162. */
  163. function context_admin_form_submit(&$form, $form_state) {
  164. if ($plugin = context_get_plugin('reaction', 'block')) {
  165. $plugin->rebuild_needed(TRUE);
  166. }
  167. }
  168. /**
  169. * Centralized node condition call function for the ever increasing number of
  170. * ways to get at a node view / node form.
  171. */
  172. function context_node_condition(&$node, $op) {
  173. if ($plugin = context_get_plugin('condition', 'node')) {
  174. $plugin->execute($node, $op);
  175. }
  176. if (module_exists('taxonomy')) {
  177. if ($plugin = context_get_plugin('condition', 'node_taxonomy')) {
  178. $plugin->execute($node, $op);
  179. }
  180. }
  181. if (module_exists('book')) {
  182. if ($plugin = context_get_plugin('condition', 'book')) {
  183. $plugin->execute($node, $op);
  184. }
  185. if ($plugin = context_get_plugin('condition', 'bookroot')) {
  186. $plugin->execute($node, $op);
  187. }
  188. }
  189. // Allow other plugins to easily be triggered on node-related events.
  190. drupal_alter('context_node_condition', $node, $op);
  191. }
  192. /**
  193. * Implementation of hook_form_alter() for system_modules_form.
  194. */
  195. function context_form_system_modules_form_alter(&$form, $form_state) {
  196. context_invalidate_cache();
  197. }
  198. /**
  199. * Implementation of hook_form_alter() for user_profile_form.
  200. */
  201. function context_form_user_profile_form_alter(&$form, $form_state) {
  202. if ($plugin = context_get_plugin('condition', 'user_page')) {
  203. $plugin->execute($form['#user'], 'form');
  204. }
  205. }
  206. /**
  207. * Implementation of hook_form_alter() for user_register_form.
  208. */
  209. function context_form_user_register_form_alter(&$form, $form_state) {
  210. if ($plugin = context_get_plugin('condition', 'user_page')) {
  211. $plugin->execute($form['#user'], 'register');
  212. }
  213. }
  214. /**
  215. * Implementation of hook_form_alter() for comment_form.
  216. */
  217. function context_form_comment_form_alter(&$form, $form_state) {
  218. if ($nid = $form['nid']['#value']) {
  219. $node = node_load($nid);
  220. context_node_condition($node, 'comment');
  221. }
  222. }
  223. /**
  224. * Implementation of hook_views_pre_view().
  225. */
  226. function context_views_pre_view($view, $display) {
  227. if ($plugin = context_get_plugin('condition', 'views')) {
  228. $plugin->execute($view);
  229. }
  230. // Support Views overrides of specific entity paths.
  231. if ($view->display_handler->has_path()) {
  232. switch ($view->display_handler->get_option('path')) {
  233. case 'taxonomy/term/%':
  234. if (($term = taxonomy_term_load(arg(2))) && ($plugin = context_get_plugin('condition', 'taxonomy_term'))) {
  235. $plugin->execute($term, 'view');
  236. }
  237. break;
  238. case 'node/%':
  239. if ($node = node_load(arg(1))) {
  240. context_node_condition($node, 'view');
  241. }
  242. break;
  243. case 'user/%':
  244. if (($account = user_load(arg(1))) && ($plugin = context_get_plugin('condition', 'user_page'))) {
  245. $plugin->execute($account, 'view');
  246. }
  247. break;
  248. }
  249. }
  250. }
  251. /**
  252. * Implementation of hook_user().
  253. */
  254. function context_user_view($account, $view_mode) {
  255. if ($view_mode === 'full' && $plugin = context_get_plugin('condition', 'user_page')) {
  256. $plugin->execute($account, 'view');
  257. }
  258. }
  259. /**
  260. * Implements hook_page_build().
  261. */
  262. function context_page_build(&$page) {
  263. module_invoke_all('context_page_condition');
  264. module_invoke_all('context_page_reaction');
  265. if ($plugin = context_get_plugin('reaction', 'block')) {
  266. $plugin->execute($page);
  267. }
  268. // See block_page_build. Clear static cache b/c in overlay form submissions
  269. // hook_page_build can get called more than once per page load.
  270. drupal_static_reset('context_reaction_block_list');
  271. }
  272. /**
  273. * THEME FUNCTIONS & RELATED ==========================================
  274. */
  275. /**
  276. * Generates an array of links (suitable for use with theme_links)
  277. * to the node forms of types associated with current active contexts.
  278. */
  279. function context_links($reset = FALSE) {
  280. static $links;
  281. if (!$links || $reset) {
  282. $contexts = context_active_contexts();
  283. $active_types = array();
  284. $conditions = array('node', 'bookroot');
  285. foreach ($conditions as $condition) {
  286. foreach ($contexts as $k => $v) {
  287. if (!empty($v->conditions[$condition]['values'])) {
  288. $active_types = array_merge($active_types, array_filter($v->conditions[$condition]['values']));
  289. }
  290. }
  291. }
  292. $links = array();
  293. if (!empty($active_types)) {
  294. // Iterate over active contexts
  295. foreach ($active_types as $type) {
  296. $add_url = 'node/add/' . str_replace('_', '-', $type);
  297. $item = menu_get_item($add_url);
  298. if ($item && $item['access'] && strpos($_GET['q'], $add_url) !== 0) {
  299. $links[$type] = array('title' => t('Add @type', array('@type' => node_type_get_name($type))), 'href' => $add_url);
  300. }
  301. }
  302. }
  303. drupal_alter('context_links', $links);
  304. uasort($links, 'element_sort');
  305. }
  306. return $links;
  307. }
  308. /**
  309. * Implementation of hook_context_page_condition().
  310. */
  311. function context_context_page_condition() {
  312. if ($plugin = context_get_plugin('condition', 'menu')) {
  313. $plugin->execute();
  314. }
  315. if ($plugin = context_get_plugin('condition', 'default')) {
  316. $plugin->execute(1);
  317. }
  318. if ($plugin = context_get_plugin('condition', 'context')) {
  319. $plugin->execute();
  320. }
  321. if ($plugin = context_get_plugin('condition', 'context_all')) {
  322. $plugin->execute();
  323. }
  324. }
  325. /**
  326. * Implementation of hook_context_page_reaction().
  327. */
  328. function context_context_page_reaction() {
  329. if ($plugin = context_get_plugin('reaction', 'css_injector')) {
  330. $plugin->execute();
  331. }
  332. if ($plugin = context_get_plugin('reaction', 'debug')) {
  333. $plugin->execute();
  334. }
  335. }
  336. /**
  337. * Implementation of hook_preprocess_page().
  338. */
  339. function context_preprocess_page(&$vars) {
  340. if ($plugin = context_get_plugin('reaction', 'theme')) {
  341. $plugin->execute($vars);
  342. }
  343. if ($plugin = context_get_plugin('reaction', 'template_suggestions')) {
  344. $plugin->execute($vars);
  345. }
  346. /*
  347. if ($context_links = context_links()) {
  348. $vars['context_links'] = theme('links', $context_links);
  349. }
  350. else {
  351. $vars['context_links'] = '';
  352. }
  353. */
  354. }
  355. /**
  356. * Implementation of hook_delivery_callback_alter().
  357. * Based on menu_position's and menu_trail_by_path's implementations.
  358. */
  359. function context_page_delivery_callback_alter() {
  360. if ($plugin = context_get_plugin('reaction', 'menu')) {
  361. $plugin->execute();
  362. }
  363. if ($plugin = context_get_plugin('reaction', 'breadcrumb')) {
  364. $plugin->execute();
  365. }
  366. }
  367. /**
  368. * Implementation of hook_preprocess_html().
  369. */
  370. function context_preprocess_html(&$vars) {
  371. if ($plugin = context_get_plugin('reaction', 'theme_html')) {
  372. $plugin->execute($vars);
  373. }
  374. }