You are here

tb_megamenu.module in The Better Mega Menu 7

Same filename and directory in other branches
  1. 8 tb_megamenu.module
  2. 2.x tb_megamenu.module

File

tb_megamenu.module
View source
<?php

include_once drupal_get_path('module', 'tb_megamenu') . '/tb_megamenu.functions.inc';
include_once drupal_get_path('module', 'tb_megamenu') . '/tb_megamenu.themes.inc';
include_once drupal_get_path('module', 'tb_megamenu') . '/tb_megamenu.admin.inc';

/**
 * Implements hook_menu().
 */
function tb_megamenu_menu() {
  $items['admin/structure/tb-megamenu/request'] = array(
    'title' => 'Request',
    'page callback' => 'tb_megamenu_request',
    'access arguments' => array(
      'administer tb_megamenu',
    ),
    'type' => MENU_SUGGESTED_ITEM,
    'file' => 'tb_megamenu.ajax.inc',
  );
  $items['admin/structure/tb-megamenu'] = array(
    'title' => 'TB Mega Menu',
    'description' => t('TB Mega Menu'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'tb_megamenu_admin_settings',
    ),
    'access arguments' => array(
      'administer tb_megamenu',
    ),
    'file' => 'tb_megamenu.admin.inc',
  );
  $items['admin/structure/tb-megamenu/%tb_megamenu_menu_name'] = array(
    'title' => 'Config TB Mega Menu',
    'description' => t('Config TB Mega Menu'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'tb_megamenu_configure_form',
      3,
    ),
    'type' => MENU_CALLBACK,
    'access callback' => '_tb_megamenu_access',
    'access arguments' => array(
      3,
    ),
    'file' => 'tb_megamenu.admin.inc',
  );
  $items['admin/structure/tb-megamenu/%tb_megamenu_menu_name/config'] = array(
    'title' => 'Config TB Mega Menu',
    'weight' => -10,
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function tb_megamenu_permission() {
  return array(
    'administer tb_megamenu' => array(
      'title' => t('Administer TB Mega Menu'),
    ),
  );
}

/**
 * Access callback: Checks TB Megamenu config access.
 *
 * @param $menu
 *   The menu name provided in the route.
 * @param object $account
 *   (optional) A user object representing the user for whom the operation is
 *   to be performed. Determines access for a user other than the current user.
 *
 * @return
 *   TRUE if access is allowed, FALSE otherwise.
 */
function _tb_megamenu_access($menu, $account = NULL) {
  $access =& drupal_static(__FUNCTION__, array());
  if (!$menu || !is_array($menu)) {

    // If there was no menu to check against we return access denied.
    return FALSE;
  }

  // Get the machine name for the menu.
  $menu_name = reset($menu);

  // Get the current user's account if no user was specified.
  if (!isset($account)) {
    $account = $GLOBALS['user'];
  }

  // Statically cache access by menu name and user account ID.
  $cid = $menu_name . ':' . $account->uid;
  if (!isset($access[$cid])) {

    // Perform basic permission checks first.
    if (!user_access('administer tb_megamenu', $account)) {
      return $access[$cid] = FALSE;
    }

    // Check access on the specific route.
    $access[$cid] = drupal_valid_path('admin/structure/menu/manage/' . $menu_name) ? TRUE : FALSE;
  }
  return $access[$cid];
}

/**
 * Implements hook_theme().
 *
 * @param array $existing
 * @param string $type
 * @param string $theme
 * @param string $path
 * @return array
 */
function tb_megamenu_theme($existing, $type, $theme, $path) {
  $items = array();
  $items['tb_megamenu'] = array(
    'variables' => array(
      'menu_name' => NULL,
      'content' => NULL,
      'section' => 'frontend',
    ),
    'template' => 'tb-megamenu',
    'path' => $path . '/templates',
    'preprocess functions' => array(
      'template_preprocess',
      'template_preprocess_tb_megamenu',
    ),
  );
  $items['tb_megamenu_nav'] = array(
    'variables' => array(
      'menu_name' => NULL,
      'level' => NULL,
      'items' => NULL,
      'menu_config' => NULL,
      'block_config' => NULL,
      'trail' => NULL,
      'section' => 'frontend',
    ),
    'template' => 'tb-megamenu-nav',
    'path' => $path . '/templates',
    'preprocess functions' => array(
      'template_preprocess',
      'template_preprocess_tb_megamenu_nav',
    ),
  );
  $items['tb_megamenu_item'] = array(
    'variables' => array(
      'menu_name' => NULL,
      'a_classes' => array(),
      'item' => NULL,
      'level' => NULL,
      'menu_config' => NULL,
      'block_config' => NULL,
      'trail' => NULL,
      'submenu' => NULL,
      'section' => 'frontend',
    ),
    'template' => 'tb-megamenu-item',
    'path' => $path . '/templates',
    'preprocess functions' => array(
      'template_preprocess',
      'template_preprocess_tb_megamenu_item',
    ),
  );
  $items['tb_megamenu_submenu'] = array(
    'variables' => array(
      'menu_name' => NULL,
      'parent' => NULL,
      'level' => NULL,
      'menu_config' => NULL,
      'block_config' => NULL,
      'trail' => NULL,
      'section' => 'frontend',
    ),
    'template' => 'tb-megamenu-submenu',
    'path' => $path . '/templates',
    'preprocess functions' => array(
      'template_preprocess',
      'template_preprocess_tb_megamenu_submenu',
    ),
  );
  $items['tb_megamenu_row'] = array(
    'variables' => array(
      'menu_name' => NULL,
      'row' => NULL,
      'parent' => NULL,
      'level' => NULL,
      'menu_config' => NULL,
      'block_config' => NULL,
      'trail' => NULL,
      'section' => 'frontend',
    ),
    'template' => 'tb-megamenu-row',
    'path' => $path . '/templates',
    'preprocess functions' => array(
      'template_preprocess',
      'template_preprocess_tb_megamenu_row',
    ),
  );
  $items['tb_megamenu_column'] = array(
    'variables' => array(
      'menu_name' => NULL,
      'col' => NULL,
      'parent' => NULL,
      'level' => NULL,
      'menu_config' => NULL,
      'block_config' => NULL,
      'trail' => NULL,
      'section' => 'frontend',
    ),
    'template' => 'tb-megamenu-column',
    'path' => $path . '/templates',
    'preprocess functions' => array(
      'template_preprocess',
      'template_preprocess_tb_megamenu_column',
    ),
  );
  $items['tb_megamenu_subnav'] = array(
    'variables' => array(
      'menu_name' => NULL,
      'col' => NULL,
      'level' => NULL,
      'items' => NULL,
      'menu_config' => NULL,
      'block_config' => NULL,
      'trail' => NULL,
      'section' => 'frontend',
    ),
    'template' => 'tb-megamenu-subnav',
    'path' => $path . '/templates',
    'preprocess functions' => array(
      'template_preprocess',
      'template_preprocess_tb_megamenu_subnav',
    ),
  );
  $items['tb_megamenu_block'] = array(
    'variables' => array(
      'menu_name' => NULL,
      'block_key' => NULL,
      'section' => 'frontend',
      'showblocktitle' => 1,
    ),
    'template' => 'tb-megamenu-block',
    'path' => $path . '/templates',
    'preprocess functions' => array(
      'template_preprocess',
      'template_preprocess_tb_megamenu_block',
    ),
  );
  $items['tb_megamenu_admin_settings'] = array(
    'render element' => 'form',
  );
  $items['tb_megamenu_backend'] = array(
    'variables' => array(
      'blocks' => NULL,
      'menu_name' => NULL,
      'menu_content' => NULL,
    ),
    'template' => 'tb-megamenu-backend',
    'path' => $path . '/templates/backend',
    'preprocess functions' => array(
      'template_preprocess',
      'template_preprocess_tb_megamenu_backend',
    ),
  );
  return $items;
}

/*
 * Implementation of hook_block_view()
 */
function tb_megamenu_block_view($delta = 0) {
  static $added_js_css = false;
  if (!$added_js_css) {
    $added_js_css = true;
    if (module_exists('fontawesome') && file_exists(libraries_get_path('fontawesome') . '/css/font-awesome.css')) {
      tb_megamenu_add_css(libraries_get_path('fontawesome') . '/css/font-awesome.css');
    }
    else {
      drupal_add_css(FONT_AWESOME_44, array(
        'type' => 'external',
      ));
    }
    tb_megamenu_add_css(drupal_get_path('module', 'tb_megamenu') . '/css/bootstrap.css');
    tb_megamenu_add_css(drupal_get_path('module', 'tb_megamenu') . '/css/base.css');
    tb_megamenu_add_css(drupal_get_path('module', 'tb_megamenu') . '/css/default.css');
    tb_megamenu_add_css(drupal_get_path('module', 'tb_megamenu') . '/css/compatibility.css');
    drupal_add_js(drupal_get_path('module', 'tb_megamenu') . '/js/tb-megamenu-frontend.js');
    drupal_add_js(drupal_get_path('module', 'tb_megamenu') . '/js/tb-megamenu-touch.js');
  }
  return array(
    'content' => array(
      '#type' => 'markup',
      '#markup' => theme('tb_megamenu', array(
        'menu_name' => $delta,
      )),
    ),
  );
}

/*
 * Implements hook_block_info()
 */
function tb_megamenu_block_info() {
  $menus = tb_megamenu_get_megamenus();
  $blocks = array();
  foreach ($menus as $menu) {
    $blocks[$menu->menu_name] = array(
      'info' => t('TB Mega Menu') . ': ' . $menu->title,
      'cache' => DRUPAL_NO_CACHE,
    );
  }
  return $blocks;
}
function tb_megamenu_menu_name_load($tb_megamenu_menu_name) {
  return array(
    $tb_megamenu_menu_name,
  );
}

/**
 * Implements hook_theme_registry_alter().
 */
function tb_megamenu_theme_registry_alter(&$theme_registry) {
  if (isset($theme_registry['tb_megamenu_submenu'])) {
    $submenu_registry = $theme_registry['tb_megamenu_submenu'];
    $cache = array(
      'tb_megamenu_submenu' => $submenu_registry,
    );
    $templates = tb_megamenu_find_hook_templates($cache, drupal_get_path('module', "tb_megamenu") . "/templates/submenu-types");
    foreach ($templates as $hook => $info) {
      if (!isset($theme_registry[$hook])) {
        $new_hook = $submenu_registry;
        $new_hook['path'] = $info['path'];
        $new_hook['template'] = str_replace("_", "-", $hook);
        $theme_registry[$hook] = $new_hook;
      }
    }
  }
}

/**
 * Implements hook_block_view_alter().
 */
function tb_megamenu_block_view_alter(&$data, $block) {
  if (isset($data['content']) && is_array($data['content']) && $block->module == 'tb_megamenu') {
    $data['content']['#contextual_links']['menu_config'] = array(
      'admin/structure/menu/manage',
      array(
        $block->delta,
      ),
    );
    $data['content']['#contextual_links']['tb_megamenu_config'] = array(
      'admin/structure/tb-megamenu',
      array(
        $block->delta,
      ),
    );
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function tb_megamenu_form_menu_overview_form_alter(&$form, &$form_state) {
  $form['#submit'][] = 'tb_megamenu_update_megamenus';
}

/**
 * Implements hook_features_api().
 */
function tb_megamenu_features_api() {
  return array(
    'tb_megamenu' => array(
      'name' => 'TB Mega Menu',
      'file' => drupal_get_path('module', 'tb_megamenu') . '/tb_megamenu.features.inc',
      'default_hook' => 'tb_megamenu_default_menus',
      // Write exports to a file with a name like
      // MODULENAME.features.tb_megamenu.inc.
      'default_file' => FEATURES_DEFAULTS_INCLUDED,
      // Offer this component as an option on the initial feature creation form.
      'feature_source' => TRUE,
    ),
  );
}