View source
<?php
define('MENU_ATTRIBUTES_LINK', 'attributes');
define('MENU_ATTRIBUTES_ITEM', 'item_attributes');
function menu_attributes_permission() {
return array(
'administer menu attributes' => array(
'title' => t('Administer menu attributes'),
'description' => t('Administer menu attributes configuration.'),
),
);
}
function menu_attributes_menu_link_alter(&$item, $menu) {
if (isset($item['options']['attributes']) && is_array($item['options']['attributes'])) {
foreach ($item['options']['attributes'] as $key => $value) {
if (is_array($value) && empty($value) || is_string($value) && !drupal_strlen($value)) {
unset($item['options']['attributes'][$key]);
}
}
if (isset($item['options']['attributes']['class']) && is_string($item['options']['attributes']['class'])) {
$item['options']['attributes']['class'] = array_filter(explode(' ', $item['options']['attributes']['class']));
}
}
}
function menu_attributes_menu_attribute_info() {
$info['title'] = array(
'label' => t('Title'),
'description' => t('The description displayed when hovering over the link.'),
'form' => array(
'#type' => 'textarea',
'#rows' => 2,
),
'scope' => array(
MENU_ATTRIBUTES_LINK,
),
);
$info['id'] = array(
'label' => t('ID'),
'description' => t('Specifies a unique ID for the link.'),
'item_description' => t('Specifies a unique ID to be added to the item.'),
'scope' => array(
MENU_ATTRIBUTES_LINK,
MENU_ATTRIBUTES_ITEM,
),
);
$info['name'] = array(
'label' => t('Name'),
'scope' => array(
MENU_ATTRIBUTES_LINK,
),
);
$info['rel'] = array(
'label' => t('Relationship'),
'description' => t("Specifies the relationship between the current page and the link. Enter 'nofollow' here to nofollow this link."),
'scope' => array(
MENU_ATTRIBUTES_LINK,
),
);
$info['class'] = array(
'label' => t('Classes'),
'description' => t('Enter additional classes to be added to the link.'),
'item_description' => t('Enter additional CSS class names to be added to the item.'),
'scope' => array(
MENU_ATTRIBUTES_LINK,
MENU_ATTRIBUTES_ITEM,
),
);
$info['style'] = array(
'label' => t('Style'),
'description' => t('Enter additional styles to be applied to the link.'),
'item_description' => t('Enter additional styles attribute to be applied to the item.'),
'scope' => array(
MENU_ATTRIBUTES_LINK,
MENU_ATTRIBUTES_ITEM,
),
);
$info['target'] = array(
'label' => t('Target'),
'description' => t('Specifies where to open the link. Using this attribute breaks XHTML validation.'),
'form' => array(
'#type' => 'select',
'#options' => array(
'' => t('None (i.e. same window)'),
'_blank' => t('New window (_blank)'),
'_top' => t('Top window (_top)'),
'_self' => t('Same window (_self)'),
'_parent' => t('Parent window (_parent)'),
),
),
'scope' => array(
MENU_ATTRIBUTES_LINK,
),
);
$info['accesskey'] = array(
'label' => t('Access Key'),
'description' => t('Specifies a <a href="@accesskey">keyboard shortcut</a> to access this link.', array(
'@accesskey' => url('http://en.wikipedia.org/wiki/Access_keys'),
)),
'form' => array(
'#maxlength' => 1,
'#size' => 1,
),
'scope' => array(
MENU_ATTRIBUTES_LINK,
),
);
return $info;
}
function menu_attributes_get_menu_attribute_info() {
$attributes = module_invoke_all('menu_attribute_info');
foreach ($attributes as $attribute => &$info) {
$info += array(
'form' => array(),
'enabled' => variable_get("menu_attributes_{$attribute}_enable", 1),
'default' => '',
);
$info['form'] += array(
'#type' => 'textfield',
'#title' => $info['label'],
'#description' => isset($info['description']) ? $info['description'] : '',
'#default_value' => variable_get("menu_attributes_{$attribute}_default", $info['default']),
);
}
drupal_alter('menu_attribute_info', $attributes);
return $attributes;
}
function menu_attributes_form_menu_edit_item_alter(&$form, $form_state) {
$item = $form['original_item']['#value'];
_menu_attributes_form_alter($form, $item, $form);
}
function menu_attributes_form_node_form_alter(&$form, $form_state) {
if (isset($form['menu']['link']) && isset($form['#node']->menu)) {
$item = $form['#node']->menu;
_menu_attributes_form_alter($form['menu']['link'], $item, $form);
}
}
function _menu_attributes_form_alter(array &$form, array $item = array(), array &$complete_form) {
$form['options']['#tree'] = TRUE;
$form['options']['#weight'] = 50;
unset($form['options']['#value']['attributes']);
unset($form['options']['#value']['item_attributes']);
$form['options'][MENU_ATTRIBUTES_LINK] = array(
'#type' => 'fieldset',
'#title' => t('Menu link attributes'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
$form['options'][MENU_ATTRIBUTES_ITEM] = array(
'#type' => 'fieldset',
'#title' => t('Menu item attributes'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
$attributes = menu_attributes_get_menu_attribute_info();
foreach ($attributes as $attribute => $info) {
if (!isset($info['scope'])) {
$info['scope'] = array(
MENU_ATTRIBUTES_LINK,
MENU_ATTRIBUTES_ITEM,
);
}
foreach ($info['scope'] as $group) {
if (isset($item['options'][$group][$attribute])) {
$info['form']['#default_value'] = $item['options'][$group][$attribute];
if ($attribute == 'class' && is_array($info['form']['#default_value'])) {
$info['form']['#default_value'] = implode(' ', $info['form']['#default_value']);
}
}
elseif ($item['mlid']) {
$info['form']['#default_value'] = $info['default'];
}
if ($group == MENU_ATTRIBUTES_ITEM && isset($info['item_description'])) {
$info['form']['#description'] = $info['item_description'];
}
$form['options'][$group][$attribute] = $info['form'] + array(
'#access' => $info['enabled'],
);
}
}
foreach ($item['options'] as $key => $value) {
if ($key !== 'attributes' && !isset($form['options'][$key])) {
$form['options'][$key] = array(
'#type' => 'value',
'#value' => $value,
);
}
}
foreach (array(
MENU_ATTRIBUTES_LINK,
MENU_ATTRIBUTES_ITEM,
) as $group) {
if (isset($item['options'][$group])) {
foreach ($item['options'][$group] as $key => $value) {
if (!isset($form['options'][$group][$key])) {
$form['options'][$group][$key] = array(
'#type' => 'value',
'#value' => $value,
);
}
}
}
}
if (isset($form['description'])) {
$form['description']['#access'] = FALSE;
array_unshift($complete_form['#submit'], '_menu_attributes_form_submit');
}
$has_visible_children = (bool) element_get_visible_children($form['options']['attributes']);
$user_has_access = user_access('administer menu attributes');
$form['options']['attributes']['#access'] = $has_visible_children && $user_has_access;
$has_visible_children = (bool) element_get_visible_children($form['options']['item_attributes']);
$form['options']['item_attributes']['#access'] = $has_visible_children && $user_has_access;
}
function _menu_attributes_form_submit($form, &$form_state) {
if (isset($form_state['values']['menu']['options']['attributes']['title'])) {
$form_state['values']['menu']['description'] = $form_state['values']['menu']['options']['attributes']['title'];
}
elseif (isset($form_state['values']['options']['attributes']['title'])) {
$form_state['values']['description'] = $form_state['values']['options']['attributes']['title'];
}
}
function menu_attributes_form_menu_configure_alter(&$form, $form_state) {
if (!user_access('administer menu attributes')) {
return;
}
$form['attributes_title'] = array(
'#type' => 'item',
'#title' => t('Menu item attribute options'),
);
$form['attributes_vertical_tabs'] = array(
'#type' => 'vertical_tabs',
'#attached' => array(
'js' => array(
drupal_get_path('module', 'menu_attributes') . '/menu_attributes.js',
),
),
);
$attributes = menu_attributes_get_menu_attribute_info();
foreach ($attributes as $attribute => $info) {
$form['attributes'][$attribute] = array(
'#type' => 'fieldset',
'#title' => $info['label'],
'#group' => 'attributes_vertical_tabs',
'#description' => $info['form']['#description'],
);
$form['attributes'][$attribute]["menu_attributes_{$attribute}_enable"] = array(
'#type' => 'checkbox',
'#title' => t('Enable the @attribute attribute.', array(
'@attribute' => drupal_strtolower($info['label']),
)),
'#default_value' => $info['enabled'],
);
$form['attributes'][$attribute]["menu_attributes_{$attribute}_default"] = array(
'#title' => t('Default'),
'#description' => '',
'#states' => array(
'invisible' => array(
'input[name="menu_attributes_' . $attribute . '_enable"]' => array(
'checked' => FALSE,
),
),
),
) + $info['form'];
}
}
function menu_attributes_preprocess_menu_link(&$variables) {
$options =& $variables['element']['#localized_options'];
$attributes =& $variables['element']['#attributes'];
if (isset($options['item_attributes'])) {
foreach ($options['item_attributes'] as $attribute => $value) {
if (!empty($value)) {
if ($attribute == 'class') {
$value = !is_array($value) ? explode(' ', $value) : $value;
if (isset($attributes[$attribute])) {
$value = array_merge($attributes[$attribute], $value);
}
}
$attributes[$attribute] = $value;
}
}
unset($options['item_attributes']);
}
}