overlay.module 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. <?php
  2. /**
  3. * @file
  4. * Displays the Drupal administration interface in an overlay.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function overlay_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#overlay':
  12. $output = '';
  13. $output .= '<h3>' . t('About') . '</h3>';
  14. $output .= '<p>' . t('The Overlay module makes the administration pages on your site display in a JavaScript overlay of the page you were viewing when you clicked the administrative link, instead of replacing the page in your browser window. Use the close link on the overlay to return to the page you were viewing when you clicked the link. For more information, see the online handbook entry for <a href="@overlay">Overlay module</a>.', array('@overlay' => 'http://drupal.org/documentation/modules/overlay')) . '</p>';
  15. return $output;
  16. }
  17. }
  18. /**
  19. * Implements hook_menu().
  20. */
  21. function overlay_menu() {
  22. $items['overlay-ajax/%'] = array(
  23. 'title' => '',
  24. 'page callback' => 'overlay_ajax_render_region',
  25. 'page arguments' => array(1),
  26. 'access arguments' => array('access overlay'),
  27. 'type' => MENU_CALLBACK,
  28. );
  29. $items['overlay/dismiss-message'] = array(
  30. 'title' => '',
  31. 'page callback' => 'overlay_user_dismiss_message',
  32. 'access arguments' => array('access overlay'),
  33. 'type' => MENU_CALLBACK,
  34. );
  35. return $items;
  36. }
  37. /**
  38. * Implements hook_admin_paths().
  39. */
  40. function overlay_admin_paths() {
  41. $paths = array(
  42. // This is marked as an administrative path so that if it is visited from
  43. // within the overlay, the user will stay within the overlay while the
  44. // callback is being processed.
  45. 'overlay/dismiss-message' => TRUE,
  46. );
  47. return $paths;
  48. }
  49. /**
  50. * Implements hook_permission().
  51. */
  52. function overlay_permission() {
  53. return array(
  54. 'access overlay' => array(
  55. 'title' => t('Access the administrative overlay'),
  56. 'description' => t('View administrative pages in the overlay.'),
  57. ),
  58. );
  59. }
  60. /**
  61. * Implements hook_theme().
  62. */
  63. function overlay_theme() {
  64. return array(
  65. 'overlay' => array(
  66. 'render element' => 'page',
  67. 'template' => 'overlay',
  68. ),
  69. 'overlay_disable_message' => array(
  70. 'render element' => 'element',
  71. ),
  72. );
  73. }
  74. /**
  75. * Implements hook_form_FORM_ID_alter().
  76. */
  77. function overlay_form_user_profile_form_alter(&$form, &$form_state) {
  78. if ($form['#user_category'] == 'account') {
  79. $account = $form['#user'];
  80. if (user_access('access overlay', $account)) {
  81. $form['overlay_control'] = array(
  82. '#type' => 'fieldset',
  83. '#title' => t('Administrative overlay'),
  84. '#weight' => 4,
  85. '#collapsible' => TRUE,
  86. );
  87. $form['overlay_control']['overlay'] = array(
  88. '#type' => 'checkbox',
  89. '#title' => t('Use the overlay for administrative pages.'),
  90. '#description' => t('Show administrative pages on top of the page you started from.'),
  91. '#default_value' => isset($account->data['overlay']) ? $account->data['overlay'] : 1,
  92. );
  93. }
  94. }
  95. }
  96. /**
  97. * Implements hook_user_presave().
  98. */
  99. function overlay_user_presave(&$edit, $account, $category) {
  100. if (isset($edit['overlay'])) {
  101. $edit['data']['overlay'] = $edit['overlay'];
  102. }
  103. }
  104. /**
  105. * Implements hook_init().
  106. *
  107. * Determine whether the current page request is destined to appear in the
  108. * parent window or in the overlay window, and format the page accordingly.
  109. *
  110. * @see overlay_set_mode()
  111. */
  112. function overlay_init() {
  113. global $user;
  114. $mode = overlay_get_mode();
  115. // Only act if the user has access to the overlay and a mode was not already
  116. // set. Other modules can also enable the overlay directly for other uses.
  117. $use_overlay = !isset($user->data['overlay']) || $user->data['overlay'];
  118. if (empty($mode) && user_access('access overlay') && $use_overlay) {
  119. $current_path = current_path();
  120. // After overlay is enabled on the modules page, redirect to
  121. // <front>#overlay=admin/modules to actually enable the overlay.
  122. if (isset($_SESSION['overlay_enable_redirect']) && $_SESSION['overlay_enable_redirect']) {
  123. unset($_SESSION['overlay_enable_redirect']);
  124. drupal_goto('<front>', array('fragment' => 'overlay=' . $current_path));
  125. }
  126. if (isset($_GET['render']) && $_GET['render'] == 'overlay') {
  127. // If a previous page requested that we close the overlay, close it and
  128. // redirect to the final destination.
  129. if (isset($_SESSION['overlay_close_dialog'])) {
  130. call_user_func_array('overlay_close_dialog', $_SESSION['overlay_close_dialog']);
  131. unset($_SESSION['overlay_close_dialog']);
  132. }
  133. // If this page shouldn't be rendered inside the overlay, redirect to the
  134. // parent.
  135. elseif (!path_is_admin($current_path)) {
  136. overlay_close_dialog($current_path, array('query' => drupal_get_query_parameters(NULL, array('q', 'render'))));
  137. }
  138. // Indicate that we are viewing an overlay child page.
  139. overlay_set_mode('child');
  140. // Unset the render parameter to avoid it being included in URLs on the page.
  141. unset($_GET['render']);
  142. }
  143. // Do not enable the overlay if we already are on an admin page.
  144. elseif (!path_is_admin($current_path)) {
  145. // Otherwise add overlay parent code and our behavior.
  146. overlay_set_mode('parent');
  147. }
  148. }
  149. }
  150. /**
  151. * Implements hook_exit().
  152. *
  153. * When viewing an overlay child page, check if we need to trigger a refresh of
  154. * the supplemental regions of the overlay on the next page request.
  155. */
  156. function overlay_exit() {
  157. // Check that we are in an overlay child page. Note that this should never
  158. // return TRUE on a cached page view, since the child mode is not set until
  159. // overlay_init() is called.
  160. if (overlay_get_mode() == 'child') {
  161. // Load any markup that was stored earlier in the page request, via calls
  162. // to overlay_store_rendered_content(). If none was stored, this is not a
  163. // page request where we expect any changes to the overlay supplemental
  164. // regions to have occurred, so we do not need to proceed any further.
  165. $original_markup = overlay_get_rendered_content();
  166. if (!empty($original_markup)) {
  167. // Compare the original markup to the current markup that we get from
  168. // rendering each overlay supplemental region now. If they don't match,
  169. // something must have changed, so we request a refresh of that region
  170. // within the parent window on the next page request.
  171. foreach (overlay_supplemental_regions() as $region) {
  172. if (!isset($original_markup[$region]) || $original_markup[$region] != overlay_render_region($region)) {
  173. overlay_request_refresh($region);
  174. }
  175. }
  176. }
  177. }
  178. }
  179. /**
  180. * Implements hook_library().
  181. */
  182. function overlay_library() {
  183. $module_path = drupal_get_path('module', 'overlay');
  184. // Overlay parent.
  185. $libraries['parent'] = array(
  186. 'title' => 'Overlay: Parent',
  187. 'website' => 'http://drupal.org/documentation/modules/overlay',
  188. 'version' => '1.0',
  189. 'js' => array(
  190. $module_path . '/overlay-parent.js' => array(),
  191. ),
  192. 'css' => array(
  193. $module_path . '/overlay-parent.css' => array(),
  194. ),
  195. 'dependencies' => array(
  196. array('system', 'ui'),
  197. array('system', 'jquery.bbq'),
  198. ),
  199. );
  200. // Overlay child.
  201. $libraries['child'] = array(
  202. 'title' => 'Overlay: Child',
  203. 'website' => 'http://drupal.org/documentation/modules/overlay',
  204. 'version' => '1.0',
  205. 'js' => array(
  206. $module_path . '/overlay-child.js' => array(),
  207. ),
  208. 'css' => array(
  209. $module_path . '/overlay-child.css' => array(),
  210. ),
  211. );
  212. return $libraries;
  213. }
  214. /**
  215. * Implements hook_drupal_goto_alter().
  216. */
  217. function overlay_drupal_goto_alter(&$path, &$options, &$http_response_code) {
  218. if (overlay_get_mode() == 'child') {
  219. // The authorize.php script bootstraps Drupal to a very low level, where
  220. // the PHP code that is necessary to close the overlay properly will not be
  221. // loaded. Therefore, if we are redirecting to authorize.php inside the
  222. // overlay, instead redirect back to the current page with instructions to
  223. // close the overlay there before redirecting to the final destination; see
  224. // overlay_init().
  225. if ($path == system_authorized_get_url() || $path == system_authorized_batch_processing_url()) {
  226. $_SESSION['overlay_close_dialog'] = array($path, $options);
  227. $path = current_path();
  228. $options = drupal_get_query_parameters();
  229. }
  230. // If the current page request is inside the overlay, add ?render=overlay
  231. // to the new path, so that it appears correctly inside the overlay.
  232. if (isset($options['query'])) {
  233. $options['query'] += array('render' => 'overlay');
  234. }
  235. else {
  236. $options['query'] = array('render' => 'overlay');
  237. }
  238. }
  239. }
  240. /**
  241. * Implements hook_batch_alter().
  242. *
  243. * If the current page request is inside the overlay, add ?render=overlay to
  244. * the success callback URL, so that it appears correctly within the overlay.
  245. *
  246. * @see overlay_get_mode()
  247. */
  248. function overlay_batch_alter(&$batch) {
  249. if (overlay_get_mode() == 'child') {
  250. if (isset($batch['url_options']['query'])) {
  251. $batch['url_options']['query']['render'] = 'overlay';
  252. }
  253. else {
  254. $batch['url_options']['query'] = array('render' => 'overlay');
  255. }
  256. }
  257. }
  258. /**
  259. * Implements hook_page_alter().
  260. */
  261. function overlay_page_alter(&$page) {
  262. // If we are limiting rendering to a subset of page regions, deny access to
  263. // all other regions so that they will not be processed.
  264. if ($regions_to_render = overlay_get_regions_to_render()) {
  265. $skipped_regions = array_diff(element_children($page), $regions_to_render);
  266. foreach ($skipped_regions as $skipped_region) {
  267. $page[$skipped_region]['#access'] = FALSE;
  268. }
  269. }
  270. $mode = overlay_get_mode();
  271. if ($mode == 'child') {
  272. // Add the overlay wrapper before the html wrapper.
  273. array_unshift($page['#theme_wrappers'], 'overlay');
  274. }
  275. elseif ($mode == 'parent' && ($message = overlay_disable_message())) {
  276. $page['page_top']['disable_overlay'] = $message;
  277. }
  278. }
  279. /**
  280. * Page callback: Dismisses the overlay accessibility message for this user.
  281. *
  282. * @return
  283. * A render array for a page containing a list of content.
  284. */
  285. function overlay_user_dismiss_message() {
  286. global $user;
  287. // It's unlikely, but possible that "access overlay" permission is granted to
  288. // the anonymous role. In this case, we do not display the message to disable
  289. // the overlay, so there is nothing to dismiss. Also, protect against
  290. // cross-site request forgeries by validating a token.
  291. if (empty($user->uid) || !isset($_GET['token']) || !drupal_valid_token($_GET['token'], 'overlay')) {
  292. return MENU_ACCESS_DENIED;
  293. }
  294. else {
  295. user_save(user_load($user->uid), array('data' => array('overlay_message_dismissed' => 1)));
  296. drupal_set_message(t('The message has been dismissed. You can change your overlay settings at any time by visiting your profile page.'));
  297. // Destination is normally given. Go to the user profile as a fallback.
  298. drupal_goto('user/' . $user->uid . '/edit');
  299. }
  300. }
  301. /**
  302. * Returns a renderable array representing a message for disabling the overlay.
  303. *
  304. * If the current user can access the overlay and has not previously indicated
  305. * that this message should be dismissed, this function returns a message
  306. * containing a link to disable the overlay. Nothing is returned for anonymous
  307. * users, because the links control per-user settings. Because some screen
  308. * readers are unable to properly read overlay contents, site builders are
  309. * discouraged from granting the "access overlay" permission to the anonymous
  310. * role.
  311. *
  312. * @see http://drupal.org/node/890284
  313. */
  314. function overlay_disable_message() {
  315. global $user;
  316. if (!empty($user->uid) && empty($user->data['overlay_message_dismissed']) && (!isset($user->data['overlay']) || $user->data['overlay']) && user_access('access overlay')) {
  317. $build = array(
  318. '#theme' => 'overlay_disable_message',
  319. '#weight' => -99,
  320. // Link to the user's profile page, where the overlay can be disabled.
  321. 'profile_link' => array(
  322. '#type' => 'link',
  323. '#title' => t('If you have problems accessing administrative pages on this site, disable the overlay on your profile page.'),
  324. '#href' => 'user/' . $user->uid . '/edit',
  325. '#options' => array(
  326. 'query' => drupal_get_destination(),
  327. 'fragment' => 'edit-overlay-control',
  328. 'attributes' => array(
  329. 'id' => 'overlay-profile-link',
  330. // Prevent the target page from being opened in the overlay.
  331. 'class' => array('overlay-exclude'),
  332. ),
  333. ),
  334. ),
  335. // Link to a menu callback that allows this message to be permanently
  336. // dismissed for the current user.
  337. 'dismiss_message_link' => array(
  338. '#type' => 'link',
  339. '#title' => t('Dismiss this message.'),
  340. '#href' => 'overlay/dismiss-message',
  341. '#options' => array(
  342. 'query' => drupal_get_destination() + array(
  343. // Add a token to protect against cross-site request forgeries.
  344. 'token' => drupal_get_token('overlay'),
  345. ),
  346. 'attributes' => array(
  347. 'id' => 'overlay-dismiss-message',
  348. // If this message is being displayed outside the overlay, prevent
  349. // this link from opening the overlay.
  350. 'class' => (overlay_get_mode() == 'parent') ? array('overlay-exclude') : array(),
  351. ),
  352. ),
  353. )
  354. );
  355. }
  356. else {
  357. $build = array();
  358. }
  359. return $build;
  360. }
  361. /**
  362. * Returns the HTML for the message about how to disable the overlay.
  363. *
  364. * @param $variables
  365. * An associative array with an 'element' element, which itself is an
  366. * associative array containing:
  367. * - profile_link: The link to this user's account.
  368. * - dismiss_message_link: The link to dismiss the overlay.
  369. *
  370. * @ingroup themeable
  371. */
  372. function theme_overlay_disable_message($variables) {
  373. $element = $variables['element'];
  374. // Add CSS classes to hide the links from most sighted users, while keeping
  375. // them accessible to screen-reader users and keyboard-only users. To assist
  376. // screen-reader users, this message appears in both the parent and child
  377. // documents, but only the one in the child document is part of the tab order.
  378. foreach (array('profile_link', 'dismiss_message_link') as $key) {
  379. $element[$key]['#options']['attributes']['class'][] = 'element-invisible';
  380. if (overlay_get_mode() == 'child') {
  381. $element[$key]['#options']['attributes']['class'][] = 'element-focusable';
  382. }
  383. }
  384. // Render the links.
  385. $output = drupal_render($element['profile_link']) . ' ' . drupal_render($element['dismiss_message_link']);
  386. // Add a heading for screen-reader users. The heading doesn't need to be seen
  387. // by sighted users.
  388. $output = '<h3 class="element-invisible">' . t('Options for the administrative overlay') . '</h3>' . $output;
  389. // Wrap in a container for styling.
  390. $output = '<div id="overlay-disable-message" class="clearfix">' . $output . '</div>';
  391. return $output;
  392. }
  393. /**
  394. * Implements hook_block_list_alter().
  395. */
  396. function overlay_block_list_alter(&$blocks) {
  397. // If we are limiting rendering to a subset of page regions, hide all blocks
  398. // which appear in regions not on that list. Note that overlay_page_alter()
  399. // does a more comprehensive job of preventing unwanted regions from being
  400. // displayed (regardless of whether they contain blocks or not), but the
  401. // reason for duplicating effort here is performance; we do not even want
  402. // these blocks to be built if they are not going to be displayed.
  403. if ($regions_to_render = overlay_get_regions_to_render()) {
  404. foreach ($blocks as $bid => $block) {
  405. if (!in_array($block->region, $regions_to_render)) {
  406. unset($blocks[$bid]);
  407. }
  408. }
  409. }
  410. }
  411. /**
  412. * Implements hook_system_info_alter().
  413. *
  414. * Add default regions for the overlay.
  415. */
  416. function overlay_system_info_alter(&$info, $file, $type) {
  417. if ($type == 'theme') {
  418. $info['overlay_regions'][] = 'content';
  419. $info['overlay_regions'][] = 'help';
  420. }
  421. }
  422. /**
  423. * Implements hook_preprocess_html().
  424. *
  425. * If the current page request is inside the overlay, add appropriate classes
  426. * to the <body> element, and simplify the page title.
  427. *
  428. * @see overlay_get_mode()
  429. */
  430. function overlay_preprocess_html(&$variables) {
  431. if (overlay_get_mode() == 'child') {
  432. // Add overlay class, so themes can react to being displayed in the overlay.
  433. $variables['classes_array'][] = 'overlay';
  434. }
  435. }
  436. /**
  437. * Implements hook_preprocess_maintenance_page().
  438. *
  439. * If the current page request is inside the overlay, add appropriate classes
  440. * to the <body> element, and simplify the page title.
  441. *
  442. * @see overlay_preprocess_maintenance_page()
  443. */
  444. function overlay_preprocess_maintenance_page(&$variables) {
  445. overlay_preprocess_html($variables);
  446. }
  447. /**
  448. * Implements template_preprocess_HOOK() for overlay.tpl.php
  449. *
  450. * If the current page request is inside the overlay, add appropriate classes
  451. * to the <body> element, and simplify the page title.
  452. *
  453. * @see template_process_overlay()
  454. * @see overlay.tpl.php
  455. */
  456. function template_preprocess_overlay(&$variables) {
  457. $variables['tabs'] = menu_primary_local_tasks();
  458. $variables['title'] = drupal_get_title();
  459. $variables['disable_overlay'] = overlay_disable_message();
  460. $variables['content_attributes_array']['class'][] = 'clearfix';
  461. }
  462. /**
  463. * Implements template_process_HOOK() for overlay.tpl.php
  464. *
  465. * Places the rendered HTML for the page body into a top level variable.
  466. *
  467. * @see template_preprocess_overlay()
  468. * @see overlay.tpl.php
  469. */
  470. function template_process_overlay(&$variables) {
  471. $variables['page'] = $variables['page']['#children'];
  472. }
  473. /**
  474. * Implements hook_preprocess_page().
  475. *
  476. * If the current page request is inside the overlay, hide the tabs.
  477. *
  478. * @see overlay_get_mode()
  479. */
  480. function overlay_preprocess_page(&$variables) {
  481. if (overlay_get_mode() == 'child') {
  482. unset($variables['tabs']['#primary']);
  483. }
  484. }
  485. /**
  486. * Stores and returns whether an empty page override is needed.
  487. *
  488. * This is used to prevent a page request which closes the overlay (for
  489. * example, a form submission) from being fully re-rendered before the overlay
  490. * is closed. Instead, we store a variable which will cause the page to be
  491. * rendered by a delivery callback function that does not actually print
  492. * visible HTML (but rather only the bare minimum scripts and styles necessary
  493. * to trigger the overlay to close), thereby allowing the dialog to be closed
  494. * faster and with less interruption, and also allowing the display of messages
  495. * to be deferred to the parent window (rather than displaying them in the
  496. * child window, which will close before the user has had a chance to read
  497. * them).
  498. *
  499. * @param $value
  500. * By default, an empty page will not be displayed. Set to TRUE to request
  501. * an empty page display, or FALSE to disable the empty page display (if it
  502. * was previously enabled on this page request).
  503. *
  504. * @return
  505. * TRUE if the current behavior is to display an empty page, or FALSE if not.
  506. *
  507. * @see overlay_page_delivery_callback_alter()
  508. */
  509. function overlay_display_empty_page($value = NULL) {
  510. $display_empty_page = &drupal_static(__FUNCTION__, FALSE);
  511. if (isset($value)) {
  512. $display_empty_page = $value;
  513. }
  514. return $display_empty_page;
  515. }
  516. /**
  517. * Implements hook_page_delivery_callback_alter().
  518. */
  519. function overlay_page_delivery_callback_alter(&$callback) {
  520. if (overlay_display_empty_page()) {
  521. $callback = 'overlay_deliver_empty_page';
  522. }
  523. }
  524. /**
  525. * Prints an empty page.
  526. *
  527. * This function is used to print out a bare minimum empty page which still has
  528. * the scripts and styles necessary in order to trigger the overlay to close.
  529. */
  530. function overlay_deliver_empty_page() {
  531. $empty_page = '<html><head><title></title>' . drupal_get_css() . drupal_get_js() . '</head><body class="overlay"></body></html>';
  532. print $empty_page;
  533. drupal_exit();
  534. }
  535. /**
  536. * Gets the current overlay mode.
  537. *
  538. * @see overlay_set_mode()
  539. */
  540. function overlay_get_mode() {
  541. return overlay_set_mode(NULL);
  542. }
  543. /**
  544. * Sets the overlay mode and adds proper JavaScript and styles to the page.
  545. *
  546. * Note that since setting the overlay mode triggers a variety of behaviors
  547. * (including hooks being invoked), it can only be done once per page request.
  548. * Therefore, the first call to this function which passes along a value of the
  549. * $mode parameter controls the overlay mode that will be used.
  550. *
  551. * @param $mode
  552. * To set the mode, pass in one of the following values:
  553. * - 'parent': This is used in the context of a parent window (a regular
  554. * browser window). If set, JavaScript is added so that administrative
  555. * links in the parent window will open in an overlay.
  556. * - 'child': This is used in the context of the child overlay window (the
  557. * page actually appearing within the overlay iframe). If set, JavaScript
  558. * and CSS are added so that Drupal behaves nicely from within the overlay.
  559. * - 'none': This is used to avoid adding any overlay-related code to the
  560. * page at all. Modules can set this to explicitly prevent the overlay from
  561. * being used. For example, since the overlay module itself sets the mode
  562. * to 'parent' or 'child' in overlay_init() when certain conditions are
  563. * met, other modules which want to override that behavior can do so by
  564. * setting the mode to 'none' earlier in the page request - e.g., in their
  565. * own hook_init() implementations, if they have a lower weight.
  566. * This parameter is optional, and if omitted, the current mode will be
  567. * returned with no action taken.
  568. *
  569. * @return
  570. * The current mode, if any has been set, or NULL if no mode has been set.
  571. *
  572. * @ingroup overlay_api
  573. * @see overlay_init()
  574. */
  575. function overlay_set_mode($mode = NULL) {
  576. global $base_path;
  577. $overlay_mode = &drupal_static(__FUNCTION__);
  578. // Make sure external resources are not included more than once. Also return
  579. // the current mode, if no mode was specified.
  580. if (isset($overlay_mode) || !isset($mode)) {
  581. return $overlay_mode;
  582. }
  583. $overlay_mode = $mode;
  584. switch ($overlay_mode) {
  585. case 'parent':
  586. drupal_add_library('overlay', 'parent');
  587. // Allow modules to act upon overlay events.
  588. module_invoke_all('overlay_parent_initialize');
  589. break;
  590. case 'child':
  591. drupal_add_library('overlay', 'child');
  592. // Allow modules to act upon overlay events.
  593. module_invoke_all('overlay_child_initialize');
  594. break;
  595. }
  596. return $overlay_mode;
  597. }
  598. /**
  599. * Implements hook_overlay_parent_initialize().
  600. */
  601. function overlay_overlay_parent_initialize() {
  602. // Let the client side know which paths are administrative.
  603. $paths = path_get_admin_paths();
  604. foreach ($paths as &$type) {
  605. $type = str_replace('<front>', variable_get('site_frontpage', 'node'), $type);
  606. }
  607. drupal_add_js(array('overlay' => array('paths' => $paths)), 'setting');
  608. $path_prefixes = array();
  609. if (module_exists('locale') && variable_get('locale_language_negotiation_url_part', LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX) == LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX) {
  610. // Get languages grouped by status and select only the enabled ones.
  611. $languages = language_list('enabled');
  612. $languages = $languages[1];
  613. $path_prefixes = array();
  614. foreach ($languages as $language) {
  615. if ($language->prefix) {
  616. $path_prefixes[] = $language->prefix;
  617. }
  618. }
  619. }
  620. drupal_add_js(array('overlay' => array('pathPrefixes' => $path_prefixes)), 'setting');
  621. // Pass along the Ajax callback for rerendering sections of the parent window.
  622. drupal_add_js(array('overlay' => array('ajaxCallback' => 'overlay-ajax')), 'setting');
  623. }
  624. /**
  625. * Implements hook_overlay_child_initialize().
  626. */
  627. function overlay_overlay_child_initialize() {
  628. // Check if the parent window needs to refresh any page regions on this page
  629. // request.
  630. overlay_trigger_refresh();
  631. // If this is a POST request, or a GET request with a token parameter, we
  632. // have an indication that something in the supplemental regions of the
  633. // overlay might change during the current page request. We therefore store
  634. // the initial rendered content of those regions here, so that we can compare
  635. // it to the same content rendered in overlay_exit(), at the end of the page
  636. // request. This allows us to check if anything actually did change, and, if
  637. // so, trigger an immediate Ajax refresh of the parent window.
  638. if (!empty($_POST) || isset($_GET['token'])) {
  639. foreach (overlay_supplemental_regions() as $region) {
  640. overlay_store_rendered_content($region, overlay_render_region($region));
  641. }
  642. // In addition, notify the parent window that when the overlay closes,
  643. // the entire parent window should be refreshed.
  644. overlay_request_page_refresh();
  645. }
  646. // Indicate that when the main page rendering occurs later in the page
  647. // request, only the regions that appear within the overlay should be
  648. // rendered.
  649. overlay_set_regions_to_render(overlay_regions());
  650. }
  651. /**
  652. * Requests that the overlay overlay closes when the page is displayed.
  653. *
  654. * @param $redirect
  655. * (optional) The path that should open in the parent window after the
  656. * overlay closes. If not set, no redirect will be performed on the parent
  657. * window.
  658. *
  659. * @param $redirect_options
  660. * (optional) An associative array of options to use when generating the
  661. * redirect URL.
  662. */
  663. function overlay_close_dialog($redirect = NULL, $redirect_options = array()) {
  664. $settings = array(
  665. 'overlayChild' => array(
  666. 'closeOverlay' => TRUE,
  667. ),
  668. );
  669. // Tell the child window to perform the redirection when requested to.
  670. if (isset($redirect)) {
  671. $settings['overlayChild']['redirect'] = url($redirect, $redirect_options);
  672. }
  673. drupal_add_js($settings, array('type' => 'setting'));
  674. // Since we are closing the overlay as soon as the page is displayed, we do
  675. // not want to show any of the page's actual content.
  676. overlay_display_empty_page(TRUE);
  677. }
  678. /**
  679. * Returns a list of page regions that appear in the overlay.
  680. *
  681. * Overlay regions correspond to the entire contents of the overlay child
  682. * window and are refreshed each time a new page request is made within the
  683. * overlay.
  684. *
  685. * @return
  686. * An array of region names that correspond to those which appear in the
  687. * overlay, within the theme that is being used to display the current page.
  688. *
  689. * @see overlay_supplemental_regions()
  690. */
  691. function overlay_regions() {
  692. return _overlay_region_list('overlay_regions');
  693. }
  694. /**
  695. * Returns a list of supplemental page regions for the overlay.
  696. *
  697. * Supplemental overlay regions are those which are technically part of the
  698. * parent window, but appear to the user as being related to the overlay
  699. * (usually because they are displayed next to, rather than underneath, the
  700. * main overlay regions) and therefore need to be dynamically refreshed if any
  701. * administrative actions taken within the overlay change their contents.
  702. *
  703. * An example of a typical overlay supplemental region would be the 'page_top'
  704. * region, in the case where a toolbar is being displayed there.
  705. *
  706. * @return
  707. * An array of region names that correspond to supplemental overlay regions,
  708. * within the theme that is being used to display the current page.
  709. *
  710. * @see overlay_regions()
  711. */
  712. function overlay_supplemental_regions() {
  713. return _overlay_region_list('overlay_supplemental_regions');
  714. }
  715. /**
  716. * Returns a list of page regions related to the overlay.
  717. *
  718. * @param $type
  719. * The type of regions to return. This can either be 'overlay_regions' or
  720. * 'overlay_supplemental_regions'.
  721. *
  722. * @return
  723. * An array of region names of the given type, within the theme that is being
  724. * used to display the current page.
  725. *
  726. * @see overlay_regions()
  727. * @see overlay_supplemental_regions()
  728. */
  729. function _overlay_region_list($type) {
  730. // Obtain the current theme. We need to first make sure the theme system is
  731. // initialized, since this function can be called early in the page request.
  732. drupal_theme_initialize();
  733. $themes = list_themes();
  734. $theme = $themes[$GLOBALS['theme']];
  735. // Return the list of regions stored within the theme's info array, or an
  736. // empty array if no regions of the appropriate type are defined.
  737. return !empty($theme->info[$type]) ? $theme->info[$type] : array();
  738. }
  739. /**
  740. * Returns a list of page regions that rendering should be limited to.
  741. *
  742. * @return
  743. * An array containing the names of the regions that will be rendered when
  744. * drupal_render_page() is called. If empty, then no limits will be imposed,
  745. * and all regions of the page will be rendered.
  746. *
  747. * @see overlay_page_alter()
  748. * @see overlay_block_list_alter()
  749. * @see overlay_set_regions_to_render()
  750. */
  751. function overlay_get_regions_to_render() {
  752. return overlay_set_regions_to_render();
  753. }
  754. /**
  755. * Sets the regions of the page that rendering will be limited to.
  756. *
  757. * @param $regions
  758. * (Optional) An array containing the names of the regions that should be
  759. * rendered when drupal_render_page() is called. Pass in an empty array to
  760. * remove all limits and cause drupal_render_page() to render all page
  761. * regions (the default behavior). If this parameter is omitted, no change
  762. * will be made to the current list of regions to render.
  763. *
  764. * @return
  765. * The current list of regions to render, or an empty array if the regions
  766. * are not being limited.
  767. *
  768. * @see overlay_page_alter()
  769. * @see overlay_block_list_alter()
  770. * @see overlay_get_regions_to_render()
  771. */
  772. function overlay_set_regions_to_render($regions = NULL) {
  773. $regions_to_render = &drupal_static(__FUNCTION__, array());
  774. if (isset($regions)) {
  775. $regions_to_render = $regions;
  776. }
  777. return $regions_to_render;
  778. }
  779. /**
  780. * Renders an individual page region.
  781. *
  782. * This function is primarily intended to be used for checking the content of
  783. * supplemental overlay regions (e.g., a region containing a toolbar). Passing
  784. * in a region that is intended to display the main page content is not
  785. * supported; the region will be rendered by this function, but the main page
  786. * content will not appear in it. In addition, although this function returns
  787. * the rendered HTML for the provided region, it does not place it on the final
  788. * page, nor add any of its associated JavaScript or CSS to the page.
  789. *
  790. * @param $region
  791. * The name of the page region that should be rendered.
  792. *
  793. * @return
  794. * The rendered HTML of the provided region.
  795. */
  796. function overlay_render_region($region) {
  797. // Indicate the region that we will be rendering, so that other regions will
  798. // be hidden by overlay_page_alter() and overlay_block_list_alter().
  799. overlay_set_regions_to_render(array($region));
  800. // Do what is necessary to force drupal_render_page() to only display HTML
  801. // from the requested region. Specifically, declare that the main page
  802. // content does not need to automatically be added to the page, and pass in
  803. // a page array that has all theme functions removed (so that overall HTML
  804. // for the page will not be added either).
  805. $system_main_content_added = &drupal_static('system_main_content_added');
  806. $system_main_content_added = TRUE;
  807. $page = array(
  808. '#type' => 'page',
  809. '#theme' => NULL,
  810. '#theme_wrappers' => array(),
  811. );
  812. // Render the region, but do not cache any JavaScript or CSS associated with
  813. // it. This region might not be included the next time drupal_render_page()
  814. // is called, and we do not want its JavaScript or CSS to erroneously appear
  815. // on the final rendered page.
  816. $original_js = drupal_add_js();
  817. $original_css = drupal_add_css();
  818. $original_libraries = drupal_static('drupal_add_library');
  819. $js = &drupal_static('drupal_add_js');
  820. $css = &drupal_static('drupal_add_css');
  821. $libraries = &drupal_static('drupal_add_library');
  822. $markup = drupal_render_page($page);
  823. $js = $original_js;
  824. $css = $original_css;
  825. $libraries = $original_libraries;
  826. // Indicate that the main page content has not, in fact, been displayed, so
  827. // that future calls to drupal_render_page() will be able to render it
  828. // correctly.
  829. $system_main_content_added = FALSE;
  830. // Restore the original behavior of rendering all regions for the next time
  831. // drupal_render_page() is called.
  832. overlay_set_regions_to_render(array());
  833. return $markup;
  834. }
  835. /**
  836. * Returns any rendered content that was stored earlier in the page request.
  837. *
  838. * @return
  839. * An array of all rendered HTML that was stored earlier in the page request,
  840. * keyed by the identifier with which it was stored. If no content was
  841. * stored, an empty array is returned.
  842. *
  843. * @see overlay_store_rendered_content()
  844. */
  845. function overlay_get_rendered_content() {
  846. return overlay_store_rendered_content();
  847. }
  848. /**
  849. * Stores strings representing rendered HTML content.
  850. *
  851. * This function is used to keep a static cache of rendered content that can be
  852. * referred to later in the page request.
  853. *
  854. * @param $id
  855. * (Optional) An identifier for the content which is being stored, which will
  856. * be used as an array key in the returned array. If omitted, no change will
  857. * be made to the current stored data.
  858. * @param $content
  859. * (Optional) A string representing the rendered data to store. This only has
  860. * an effect if $id is also provided.
  861. *
  862. * @return
  863. * An array representing all data that is currently being stored, or an empty
  864. * array if there is none.
  865. *
  866. * @see overlay_get_rendered_content()
  867. */
  868. function overlay_store_rendered_content($id = NULL, $content = NULL) {
  869. $rendered_content = &drupal_static(__FUNCTION__, array());
  870. if (isset($id)) {
  871. $rendered_content[$id] = $content;
  872. }
  873. return $rendered_content;
  874. }
  875. /**
  876. * Requests that the parent window refreshes a particular page region.
  877. *
  878. * @param $region
  879. * The name of the page region to refresh. The parent window will trigger a
  880. * refresh of this region on the next page load.
  881. *
  882. * @see overlay_trigger_refresh()
  883. * @see Drupal.overlay.refreshRegions()
  884. */
  885. function overlay_request_refresh($region) {
  886. $class = drupal_region_class($region);
  887. $_SESSION['overlay_regions_to_refresh'][] = array($class => $region);
  888. }
  889. /**
  890. * Requests that the entire parent window is reloaded when the overlay closes.
  891. *
  892. * @see overlay_trigger_refresh()
  893. */
  894. function overlay_request_page_refresh() {
  895. $_SESSION['overlay_refresh_parent'] = TRUE;
  896. }
  897. /**
  898. * Checks if the parent window needs to be refreshed on this page load.
  899. *
  900. * If the previous page load requested that any page regions be refreshed, or
  901. * if it requested that the entire page be refreshed when the overlay closes,
  902. * pass that request via JavaScript to the child window, so it can in turn pass
  903. * the request to the parent window.
  904. *
  905. * @see overlay_request_refresh()
  906. * @see overlay_request_page_refresh()
  907. * @see Drupal.overlay.refreshRegions()
  908. */
  909. function overlay_trigger_refresh() {
  910. if (!empty($_SESSION['overlay_regions_to_refresh'])) {
  911. $settings = array(
  912. 'overlayChild' => array(
  913. 'refreshRegions' => $_SESSION['overlay_regions_to_refresh'],
  914. ),
  915. );
  916. drupal_add_js($settings, array('type' => 'setting'));
  917. unset($_SESSION['overlay_regions_to_refresh']);
  918. }
  919. if (!empty($_SESSION['overlay_refresh_parent'])) {
  920. drupal_add_js(array('overlayChild' => array('refreshPage' => TRUE)), array('type' => 'setting'));
  921. unset($_SESSION['overlay_refresh_parent']);
  922. }
  923. }
  924. /**
  925. * Prints the markup obtained by rendering a single region of the page.
  926. *
  927. * This function is intended to be called via Ajax.
  928. *
  929. * @param $region
  930. * The name of the page region to render.
  931. *
  932. * @see Drupal.overlay.refreshRegions()
  933. */
  934. function overlay_ajax_render_region($region) {
  935. print overlay_render_region($region);
  936. }