You are here

domain_nav.module in Domain Access 5

Navigation block and menu options for Domain Access

File

domain_nav/domain_nav.module
View source
<?php

/**
 * @defgroup domain_nav Domain Navigation: navigation block and menu options
 *
 * Configurable navigation and block based on active domains.
 */

/**
 * @file
 * Navigation block and menu options for Domain Access
 *
 * @ingroup domain_nav
 */

/**
 * Killswitch for hook_menu().
 * Set this value to FALSE to disable the Domain Nav menu items.
 */
define('DOMAIN_NAV_MENU', TRUE);

/**
 * Implement hook_menu()
 */
function domain_nav_menu($may_cache) {
  global $base_url;
  $items = array();
  if ($may_cache && DOMAIN_NAV_MENU) {
    $root = domain_default();
    $items[] = array(
      'path' => 'domain',
      'title' => t('Domain'),
      'description' => t('Go to main site'),
      'callback' => 'drupal_goto',
      'callback arguments' => array(
        $root['path'],
      ),
      'type' => MENU_SUGGESTED_ITEM,
      'access' => TRUE,
    );

    // Generate the list of active domains as menu items
    $domains = domain_domains();
    foreach ($domains as $domain) {

      // If the domain is not valid, we disable it by default.
      $type = MENU_NORMAL_ITEM;
      if (!$domain['valid']) {
        $type = MENU_SUGGESTED_ITEM;
      }
      $items[] = array(
        'path' => 'domain/' . $domain['subdomain'],
        // Can't use absolute paths, so we goto.
        'title' => check_plain($domain['sitename']),
        'description' => t('Go to !domain', array(
          '!domain' => $domain['subdomain'],
        )),
        'callback' => 'drupal_goto',
        'callback arguments' => array(
          $domain['path'],
        ),
        'type' => $type,
        'access' => TRUE,
      );
    }
  }

  // In the Garland header, we have to force the block to appear correctly.
  drupal_add_css(drupal_get_path('module', 'domain_nav') . '/domain_nav.css');
  return $items;
}

/**
 * Implement hook_block()
 */
function domain_nav_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $block[0]['info'] = t('Domain list navigator');
      break;
    case 'view':
      $block['subject'] = '';
      $block['content'] = domain_nav_render();
      break;
    case 'configure':
      $form['domain_nav_block'] = array(
        '#type' => 'radios',
        '#title' => t('Link paths'),
        '#default_value' => variable_get('domain_nav_block', 0),
        '#options' => array(
          0 => t('Link to site home page'),
          1 => t('Link to active url'),
        ),
      );
      $form['domain_nav_theme'] = array(
        '#type' => 'radios',
        '#title' => t('Link theme'),
        '#default_value' => variable_get('domain_nav_theme', 'default'),
        '#options' => array(
          'default' => t('JavaScript select list'),
          'menus' => t('Menu-style tab links'),
          'ul' => t('Unordered list of links'),
        ),
      );
      return $form;
      break;
    case 'save':
      variable_set('domain_nav_block', $edit['domain_nav_block']);
      variable_set('domain_nav_theme', $edit['domain_nav_theme']);
      break;
  }
  return $block;
}

/**
 * Renders output for the block.
 *
 * This function is extracted for use in your themes.  Just call:
 *   domain_nav_render($paths = 0, $style = 'default');
 *
 * @param $paths
 *  A boolean flag indicating how to write links to other domains:
 *    0 == link to home page of selected domain
 *    1 == link to current url on selected domain
 *
 * @param $style
 *  Indicates which theme function to invoke.  Default options are:
 *    'default' == theme_domain_nav_default()
 *    'menus' == theme_domain_nav_menus()
 *    'ul' == theme_domain_nav_ul()
 *
 * @return
 *  A themed HTML object for navigation.
 */
function domain_nav_render($paths = NULL, $style = NULL) {
  global $_domain;

  // Get the options and set the variables.
  if (empty($paths)) {
    $paths = variable_get('domain_nav_block', 0);
  }
  if (empty($style)) {
    $style = variable_get('domain_nav_theme', 'default');
  }
  $options = array();
  $domains = domain_domains();

  // Select which path calculation to use.
  $paths == 0 ? $func = 'domain_get_path' : ($func = 'domain_get_uri');
  foreach ($domains as $key => $value) {
    $allow = TRUE;

    // If the domain is not valid, we disable it by default.
    if (!$value['valid']) {
      if (user_access('administer domains')) {
        $value['sitename'] .= ' *';
      }
      else {
        $allow = FALSE;
      }
    }
    if ($allow) {
      if ($_domain['subdomain'] == $value['subdomain']) {
        $value['active'] = TRUE;
      }
      $path = $func($value);
      $value['path'] = $path;

      // Allow other modules to add elements to the array.
      $extra = array();
      $extra = module_invoke_all('domainnav', $value);
      $value = array_merge($value, $extra);
      $options[$value['domain_id']] = $value;
    }
  }
  $theme = 'domain_nav_' . $style;
  $content = theme($theme, $options);
  return $content;
}

/**
 * Themes the domain list as a JavaScript selection form.
 *
 * @param $options
 *  An array of information about each domain.  Options contain the following:
 *
 *    - domain_id -- the unique identifier of this domain
 *    - subdomain -- the host path of the url for this domain
 *    - sitename -- the human-readable name of this domain
 *    - path -- the link path (a Drupal-formatted path)
 *    - active -- a boolean flag indicating the currently active domain
 *
 *  If hook_domainnav() is invoked, additonal elements may be present.
 */
function theme_domain_nav_default($options) {
  global $_domain;
  $current = $options[$_domain['domain_id']];
  $output = '<form class="domain-list" action="">';
  $output .= '<select onchange="if (this.value) location.href=this.value;">';
  $output .= '<option value="' . $current['path'] . '">' . t('Jump to...') . '</option>';
  foreach ($options as $key => $value) {
    $value['active'] ? $selected = ' selected' : ($selected = '');
    $output .= '<option value="' . $value['path'] . '"' . $selected . '>' . filter_xss_admin($value['sitename']) . '</option>';
  }
  $output .= '</select>';
  $output .= '</form>';
  return $output;
}

/**
 * Themes the domain list as an unordered list of links.
 *
 * @param $options
 *  An array of information about each domain.
 */
function theme_domain_nav_ul($options) {
  foreach ($options as $key => $value) {
    $items[] = l($value['sitename'], $value['path']);
  }
  return theme('item_list', $items);
}

/**
 * Themes the domain list as a menu-style group of tabs.
 *
 * @param $options
 *  An array of information about each domain.
 */
function theme_domain_nav_menus($options) {
  foreach ($options as $key => $value) {
    $value['active'] ? $active = 'active' : ($active = '');
    $items[] = array(
      'data' => l($value['sitename'], $value['path']),
      'class' => $active,
    );
  }
  return theme('item_list', $items, NULL, 'ul', array(
    'class' => 'tabs primary',
  ));
}

/**
 * Implement hook_domainupdate()
 */
function domain_nav_domainupdate($op, $domain = array(), $edit = array()) {

  // Only execute if the menu is turned on.
  if (DOMAIN_NAV_MENU) {

    // We rebuild the menu, since the domain records have changed.
    // Clear the page cache, so that changed menus are reflected for anonymous users.
    cache_clear_all('*', 'cache_page', TRUE);

    // Also clear the menu cache.
    cache_clear_all('*', 'cache_menu', TRUE);
  }
}

Related topics

Functions

Namesort descending Description
domain_nav_block Implement hook_block()
domain_nav_domainupdate Implement hook_domainupdate()
domain_nav_menu Implement hook_menu()
domain_nav_render Renders output for the block.
theme_domain_nav_default Themes the domain list as a JavaScript selection form.
theme_domain_nav_menus Themes the domain list as a menu-style group of tabs.
theme_domain_nav_ul Themes the domain list as an unordered list of links.

Constants

Namesort descending Description
DOMAIN_NAV_MENU Killswitch for hook_menu(). Set this value to FALSE to disable the Domain Nav menu items.