domain_nav.module

Tracking 5.x-1.x branch
  1. drupal
    1. 5 contributions/domain/domain_nav/domain_nav.module
    2. 6 contributions/domain/domain_nav/domain_nav.module

Navigation block and menu options for Domain Access

Functions & methods

NameDescription
domain_nav_blockImplement hook_block()
domain_nav_domainupdateImplement hook_domainupdate()
domain_nav_menuImplement hook_menu()
domain_nav_renderRenders output for the block.
theme_domain_nav_defaultThemes the domain list as a JavaScript selection form.
theme_domain_nav_menusThemes the domain list as a menu-style group of tabs.
theme_domain_nav_ulThemes the domain list as an unordered list of links.

Constants

NameDescription
DOMAIN_NAV_MENUKillswitch for hook_menu(). Set this value to FALSE to disable the Domain Nav menu items.

File

View source
  1. <?php
  2. /**
  3. * @defgroup domain_nav Domain Navigation: navigation block and menu options
  4. *
  5. * Configurable navigation and block based on active domains.
  6. */
  7. /**
  8. * @file
  9. * Navigation block and menu options for Domain Access
  10. *
  11. * @ingroup domain_nav
  12. */
  13. /**
  14. * Killswitch for hook_menu().
  15. * Set this value to FALSE to disable the Domain Nav menu items.
  16. */
  17. define('DOMAIN_NAV_MENU', TRUE);
  18. /**
  19. * Implement hook_menu()
  20. */
  21. function domain_nav_menu($may_cache) {
  22. global $base_url;
  23. $items = array();
  24. if ($may_cache && DOMAIN_NAV_MENU) {
  25. $root = domain_default();
  26. $items[] = array(
  27. 'path' => 'domain',
  28. 'title' => t('Domain'),
  29. 'description' => t('Go to main site'),
  30. 'callback' => 'drupal_goto',
  31. 'callback arguments' => array($root['path']),
  32. 'type' => MENU_SUGGESTED_ITEM,
  33. 'access' => TRUE
  34. );
  35. // Generate the list of active domains as menu items
  36. $domains = domain_domains();
  37. foreach ($domains as $domain) {
  38. // If the domain is not valid, we disable it by default.
  39. $type = MENU_NORMAL_ITEM;
  40. if (!$domain['valid']) {
  41. $type = MENU_SUGGESTED_ITEM;
  42. }
  43. $items[] = array(
  44. 'path' => 'domain/'. $domain['subdomain'], // Can't use absolute paths, so we goto.
  45. 'title' => check_plain($domain['sitename']),
  46. 'description' => t('Go to !domain', array('!domain' => $domain['subdomain'])),
  47. 'callback' => 'drupal_goto',
  48. 'callback arguments' => array($domain['path']),
  49. 'type' => $type,
  50. 'access' => TRUE
  51. );
  52. }
  53. }
  54. // In the Garland header, we have to force the block to appear correctly.
  55. drupal_add_css(drupal_get_path('module', 'domain_nav') .'/domain_nav.css');
  56. return $items;
  57. }
  58. /**
  59. * Implement hook_block()
  60. */
  61. function domain_nav_block($op='list', $delta = 0, $edit = array()) {
  62. switch ($op) {
  63. case 'list':
  64. $block[0]['info'] = t('Domain list navigator');
  65. break;
  66. case 'view':
  67. $block['subject'] = '';
  68. $block['content'] = domain_nav_render();
  69. break;
  70. case 'configure':
  71. $form['domain_nav_block'] = array(
  72. '#type' => 'radios',
  73. '#title' => t('Link paths'),
  74. '#default_value' => variable_get('domain_nav_block', 0),
  75. '#options' => array(0 => t('Link to site home page'), 1 => t('Link to active url')),
  76. );
  77. $form['domain_nav_theme'] = array(
  78. '#type' => 'radios',
  79. '#title' => t('Link theme'),
  80. '#default_value' => variable_get('domain_nav_theme', 'default'),
  81. '#options' => array(
  82. 'default' => t('JavaScript select list'),
  83. 'menus' => t('Menu-style tab links'),
  84. 'ul' => t('Unordered list of links'),
  85. ),
  86. );
  87. return $form;
  88. break;
  89. case 'save':
  90. variable_set('domain_nav_block', $edit['domain_nav_block']);
  91. variable_set('domain_nav_theme', $edit['domain_nav_theme']);
  92. break;
  93. }
  94. return $block;
  95. }
  96. /**
  97. * Renders output for the block.
  98. *
  99. * This function is extracted for use in your themes. Just call:
  100. * domain_nav_render($paths = 0, $style = 'default');
  101. *
  102. * @param $paths
  103. * A boolean flag indicating how to write links to other domains:
  104. * 0 == link to home page of selected domain
  105. * 1 == link to current url on selected domain
  106. *
  107. * @param $style
  108. * Indicates which theme function to invoke. Default options are:
  109. * 'default' == theme_domain_nav_default()
  110. * 'menus' == theme_domain_nav_menus()
  111. * 'ul' == theme_domain_nav_ul()
  112. *
  113. * @return
  114. * A themed HTML object for navigation.
  115. */
  116. function domain_nav_render($paths = NULL, $style = NULL) {
  117. global $_domain;
  118. // Get the options and set the variables.
  119. if (empty($paths)) {
  120. $paths = variable_get('domain_nav_block', 0);
  121. }
  122. if (empty($style)) {
  123. $style = variable_get('domain_nav_theme', 'default');
  124. }
  125. $options = array();
  126. $domains = domain_domains();
  127. // Select which path calculation to use.
  128. ($paths == 0) ? $func = 'domain_get_path' : $func = 'domain_get_uri';
  129. foreach ($domains as $key => $value) {
  130. $allow = TRUE;
  131. // If the domain is not valid, we disable it by default.
  132. if (!$value['valid']) {
  133. if (user_access('administer domains')) {
  134. $value['sitename'] .= ' *';
  135. }
  136. else {
  137. $allow = FALSE;
  138. }
  139. }
  140. if ($allow) {
  141. if ($_domain['subdomain'] == $value['subdomain']) {
  142. $value['active'] = TRUE;
  143. }
  144. $path = $func($value);
  145. $value['path'] = $path;
  146. // Allow other modules to add elements to the array.
  147. $extra = array();
  148. $extra = module_invoke_all('domainnav', $value);
  149. $value = array_merge($value, $extra);
  150. $options[$value['domain_id']] = $value;
  151. }
  152. }
  153. $theme = 'domain_nav_'. $style;
  154. $content = theme($theme, $options);
  155. return $content;
  156. }
  157. /**
  158. * Themes the domain list as a JavaScript selection form.
  159. *
  160. * @param $options
  161. * An array of information about each domain. Options contain the following:
  162. *
  163. * - domain_id -- the unique identifier of this domain
  164. * - subdomain -- the host path of the url for this domain
  165. * - sitename -- the human-readable name of this domain
  166. * - path -- the link path (a Drupal-formatted path)
  167. * - active -- a boolean flag indicating the currently active domain
  168. *
  169. * If hook_domainnav() is invoked, additonal elements may be present.
  170. */
  171. function theme_domain_nav_default($options) {
  172. global $_domain;
  173. $current = $options[$_domain['domain_id']];
  174. $output = '<form class="domain-list" action="">';
  175. $output .= '<select onchange="if (this.value) location.href=this.value;">';
  176. $output .= '<option value="'. $current['path'] .'">'. t('Jump to...') .'</option>';
  177. foreach ($options as $key => $value) {
  178. ($value['active']) ? $selected = ' selected' : $selected = '';
  179. $output .= '<option value="'. $value['path'] .'"'. $selected .'>'. filter_xss_admin($value['sitename']) .'</option>';
  180. }
  181. $output .= '</select>';
  182. $output .= '</form>';
  183. return $output;
  184. }
  185. /**
  186. * Themes the domain list as an unordered list of links.
  187. *
  188. * @param $options
  189. * An array of information about each domain.
  190. */
  191. function theme_domain_nav_ul($options) {
  192. foreach ($options as $key => $value) {
  193. $items[] = l($value['sitename'], $value['path']);
  194. }
  195. return theme('item_list', $items);
  196. }
  197. /**
  198. * Themes the domain list as a menu-style group of tabs.
  199. *
  200. * @param $options
  201. * An array of information about each domain.
  202. */
  203. function theme_domain_nav_menus($options) {
  204. foreach ($options as $key => $value) {
  205. ($value['active']) ? $active = 'active' : $active = '';
  206. $items[] = array(
  207. 'data' => l($value['sitename'], $value['path']),
  208. 'class' => $active
  209. );
  210. }
  211. return theme('item_list', $items, NULL, 'ul', array('class' => 'tabs primary'));
  212. }
  213. /**
  214. * Implement hook_domainupdate()
  215. */
  216. function domain_nav_domainupdate($op, $domain = array(), $edit = array()) {
  217. // Only execute if the menu is turned on.
  218. if (DOMAIN_NAV_MENU) {
  219. // We rebuild the menu, since the domain records have changed.
  220. // Clear the page cache, so that changed menus are reflected for anonymous users.
  221. cache_clear_all('*', 'cache_page', TRUE);
  222. // Also clear the menu cache.
  223. cache_clear_all('*', 'cache_menu', TRUE);
  224. }
  225. }

Related topics