[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/menu_attributes/ -> menu_attributes.module (source)

   1  <?php
   2  // $Id: menu_attributes.module,v 1.6 2009/04/20 23:00:50 schoonzie Exp $
   3  
   4  /**
   5   * @file
   6   * Alters the menu form to allow the administrator to specify additional attributes for the menu link
   7   */
   8  
   9  
  10  /**
  11   * Implementation of hook_help().
  12   */
  13  function menu_attributes_help($path, $arg) {
  14    switch ($path) {
  15      case 'admin/settings/menu_attributes':
  16        return t('The menu attributes module allows you to set HTML attributes on the &lt;a&gt; element of individual menu items without hacking theme files. Use the settings below to choose what menu attributes are available to be set on the menu form and their defaults for <strong>new</strong> menu items.');
  17    }
  18  }
  19  
  20  /**
  21   * Implementation of hook_menu().
  22   */
  23  function menu_attributes_menu() {
  24    $items = array();
  25    $items['admin/settings/menu_attributes'] = array(
  26      'title' => 'Menu Attributes Settings',
  27      'description' => 'Configure the Menu Attributes module',
  28      'page callback' => 'drupal_get_form',
  29      'page arguments' =>  array('menu_attributes_admin'),
  30      'access arguments' => array('administer site configuration'),
  31      'file' => 'menu_attributes.admin.inc',
  32    );
  33  
  34    return $items;
  35  }
  36  
  37  /**
  38   * Implementation of hook_perm().
  39   */
  40  function menu_attributes_perm() {
  41    return array('administer menu attributes');
  42  }
  43  
  44  function menu_attributes_form_alter(&$form, $form_state, $form_id) {
  45    if ((isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) || ('menu_edit_item' == $form_id)) {
  46      if ($form['#node']->type .'_node_form' == $form_id) { // It's the node edit form
  47        $item = $form['#node']->menu;
  48      }
  49      else {
  50        $item = $form['menu']['#item'];
  51      }
  52      
  53      if (isset($form['menu'])) { // Check to see whether the menu form exists
  54        $form['menu']['options'] = array(
  55            '#type' => 'fieldset',
  56            '#title' => t('Menu item attributes'),
  57            '#access' => user_access('administer menu attributes'),
  58            '#collapsible' => TRUE,
  59            '#collapsed' => TRUE,
  60            '#tree' => TRUE,
  61            '#weight' => 50,
  62        );
  63        
  64        if (variable_get('menu_attributes_id_enable', 1)) {
  65          $form['menu']['options']['attributes']['id'] = array(
  66            '#type' => 'textfield',
  67            '#title' => t('Id attribute'),
  68            '#default_value' => $item['options']['attributes']['id'],
  69            '#required' => FALSE,
  70          );
  71        }
  72        if (variable_get('menu_attributes_name_enable', 1)) {
  73          $form['menu']['options']['attributes']['name'] = array(
  74            '#type' => 'textfield',
  75            '#title' => t('Name attribute'),
  76            // For #default_value we check to see if it has been set for this item, if not we check to see if it is an existing menu item, if not, we use the default
  77            '#default_value' => isset($item['options']['attributes']['name']) ? $item['options']['attributes']['name'] : ($item['mlid'] ? NULL : variable_get('menu_attributes_name_default', NULL)),
  78            '#required' => FALSE,
  79          );
  80        }
  81        if (variable_get('menu_attributes_target_enable', 1)) {
  82          $form['menu']['options']['attributes']['target'] = array(
  83            '#type' => 'select',
  84            '#title' => t('Target attribute'),
  85            '#description' => t('Enter the target for this link, default &lt;none&gt;'),
  86            // For #default_value we check to see if it has been set for this item, if not we check to see if it is an existing menu item, if not, we use the default
  87            '#default_value' => isset($item['options']['attributes']['target']) ? $item['options']['attributes']['target'] : ($item['mlid'] ? NULL : variable_get('menu_attributes_target_default', NULL)),
  88            '#options' => array(
  89              '' => '<none> (i.e. same window)',
  90              '_blank' => 'New Window (_blank)',
  91              '_top' => 'Top Window (_top)',
  92              '_self' => 'Same Window (_self)',
  93              '_parent' => 'Parent Window (_parent)',
  94            ),
  95            '#required' => FALSE,
  96          );
  97        }
  98        if (variable_get('menu_attributes_rel_enable', 1)) {
  99          $form['menu']['options']['attributes']['rel'] = array(
 100            '#type' => 'textfield',
 101            '#title' => t('Rel attribute'),
 102            '#description' => t('Enter \'nofollow\' here to nofollow this link'),
 103            // For #default_value we check to see if it has been set for this item, if not we check to see if it is an existing menu item, if not, we use the default
 104            '#default_value' => isset($item['options']['attributes']['rel']) ? $item['options']['attributes']['rel'] : ($item['mlid'] ? NULL : variable_get('menu_attributes_rel_default', NULL)),
 105            '#required' => FALSE,
 106          );
 107        }
 108        if (variable_get('menu_attributes_class_enable', 1)) {
 109          $form['menu']['options']['attributes']['class'] = array(
 110            '#type' => 'textfield',
 111            '#title' => t('Class attribute'),
 112            '#description' => t('Enter additional classes to be added to the menu item'),
 113            // For #default_value we check to see if it has been set for this item, if not we check to see if it is an existing menu item, if not, we use the default
 114            '#default_value' => isset($item['options']['attributes']['class']) ? $item['options']['attributes']['class'] : ($item['mlid'] ? NULL : variable_get('menu_attributes_class_default', NULL)),
 115            '#required' => FALSE,
 116          );
 117        }
 118        if (variable_get('menu_attributes_style_enable', 1)) {
 119          $form['menu']['options']['attributes']['style'] = array(
 120            '#type' => 'textfield',
 121            '#title' => t('Style attribute'),
 122            '#description' => t('Enter additional styles to be applied to the menu item'),
 123            // For #default_value we check to see if it has been set for this item, if not we check to see if it is an existing menu item, if not, we use the default
 124            '#default_value' => isset($item['options']['attributes']['style']) ? $item['options']['attributes']['style'] : ($item['mlid'] ? NULL : variable_get('menu_attributes_style_default', NULL)),
 125            '#required' => FALSE,
 126          );
 127        }
 128        if (variable_get('menu_attributes_accesskey_enable', 1)) {
 129          $form['menu']['options']['attributes']['accesskey'] = array(
 130            '#type' => 'textfield',
 131            '#title' => t('Accesskey attribute'),
 132            '#description' => t('Specify an !accesskey for this menu item', array('!accesskey' => l('accesskey', 'http://en.wikipedia.org/wiki/Access_keys'))),
 133            // For #default_value we check to see if it has been set for this item, if not we check to see if it is an existing menu item, if not, we use the default
 134            '#default_value' => isset($item['options']['attributes']['accesskey']) ? $item['options']['attributes']['accesskey'] : ($item['mlid'] ? NULL : variable_get('menu_attributes_accesskey_default', NULL)),
 135            '#required' => FALSE,
 136            '#maxlength' => 1,
 137          );
 138        }
 139      }
 140    }
 141  }
 142  
 143  function menu_attributes_menu_link_alter(&$item, $menu) {
 144    if (is_array($item['options']['attributes'])) {
 145      foreach ($item['options']['attributes'] as $key => $value) {
 146        if ($value == "") {
 147          unset($item['options']['attributes'][$key]);
 148        }
 149      }
 150    }
 151  }
 152  
 153  


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7