icon_menu.module in Icon API 8
Same filename and directory in other branches
icon_menu.module Implements an API for icon providers and an administrative UI for integrating icons through out the site.
File
modules/icon_menu/icon_menu.moduleView source
<?php
/**
* @file
* icon_menu.module
* Implements an API for icon providers and an administrative UI for
* integrating icons through out the site.
*/
/**
* Implements hook_icon_permission().
*/
function icon_menu_icon_permission() {
return array(
'administer menu icons' => array(
'title' => t('Administer menu icons'),
'description' => t('Grants selected roles the ability to administer icons in menu items.'),
),
);
}
/**
* Helper function to return the default icon menu values.
*/
function icon_menu_defaults() {
return array(
'bundle' => '',
'icon' => '',
'wrapper' => '',
'wrapper_class' => '',
'breadcrumb' => FALSE,
'position' => 'title_before',
'title_wrapper' => 0,
'title_wrapper_element' => 'span',
'title_wrapper_class' => 'title',
);
}
/**
* Implements hook_menu_breadcrumb_alter().
*/
function icon_menu_menu_breadcrumb_alter(&$active_trail, &$item) {
foreach ($active_trail as $key => $value) {
if (!empty($value['localized_options']['icon']) && empty($value['localized_options']['icon']['breadcrumb'])) {
unset($active_trail[$key]['localized_options']['icon']);
}
}
if (!empty($item['localized_options']['icon']) && empty($item['localized_options']['icon']['breadcrumb'])) {
unset($item['localized_options']['icon']);
}
}
/**
* Implements hook_preprocess_link().
*/
function icon_menu_preprocess_link(&$variables) {
if (!empty($variables['options']) && !empty($variables['options']['icon']) && !empty($variables['options']['icon']['bundle']) && !empty($variables['options']['icon']['icon']) && (empty($variables['options']['icon']['processed']) || !$variables['options']['icon']['processed'])) {
$settings = $variables['options']['icon'];
// @FIXME
// theme() has been renamed to _theme() and should NEVER be called directly.
// Calling _theme() directly can alter the expected output and potentially
// introduce security issues (see https://www.drupal.org/node/2195739). You
// should use renderable arrays instead.
//
//
// @see https://www.drupal.org/node/2195739
// if ($icon = theme('icon', array('bundle' => $settings['bundle'], 'icon' => $settings['icon']))) {
// // Force link to render HTML.
// $variables['options']['html'] = TRUE;
// if ($settings['title_wrapper']) {
// $variables['text'] = '<' . $settings['title_wrapper_element'] . (!empty($settings['title_wrapper_class']) ? ' class="' . $settings['title_wrapper_class'] . '"' : '') . '>' . $variables['text'] . '</' . $settings['title_wrapper_element'] . '>';
// }
// switch ($variables['options']['icon']['position']) {
// case 'title_before':
// $variables['text'] = $icon . $variables['text'];
// break;
//
// case 'title_after':
// $variables['text'] .= $icon;
// break;
//
// case 'title_invisible':
// $variables['text'] = $icon . '<span class="element-invisible element-focusable">' . $variables['text'] . '</span>';
// break;
//
// case 'title_replace':
// $variables['text'] = $icon;
// break;
// }
// $variables['options']['icon']['processed'] = TRUE;
// }
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Adds icon configuration options to the menu edit menu form.
*
* @see menu_edit_item()
* @see _icon_menu_form_alter()
*/
function icon_menu_form_menu_edit_item_alter(&$form, $form_state) {
$item = $form['original_item']['#value'];
_icon_menu_form_alter($form, $item, $form);
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Adds menu attribute options to the node's edit menu item form.
*
* @see _menu_attributes_form_alter()
*/
function icon_menu_form_node_form_alter(&$form, $form_state) {
if (isset($form['menu']['link']) && isset($form['#node']->menu)) {
$item = $form['#node']->menu;
_icon_menu_form_alter($form['menu']['link'], $item, $form);
}
}
/**
* Add the icon configuration to a menu item edit form.
*
* @param array $form
* The menu item edit form passed by reference.
* @param array $item
* The optional existing menu item for context.
* @param array $complete_form
* The entire form from menu callback.
*/
function _icon_menu_form_alter(array &$form, array $item = array(), array &$complete_form = array()) {
$access = \Drupal::currentUser()
->hasPermission('administer icons') || \Drupal::currentUser()
->hasPermission('administer menu icons');
$form['options']['#tree'] = TRUE;
$form['options']['#weight'] = 50;
// Unset the previous value so that the new values get saved.
if (isset($form['options']['#value']['icon'])) {
unset($form['options']['#value']['icon']);
}
// Determine proper parent names for states.
$parents = 'options[icon]';
if (isset($complete_form['menu']['link']) && isset($complete_form['#node']->menu)) {
$parents = 'menu[options][icon]';
}
// Get the default values.
$defaults = array_merge(icon_menu_defaults(), !empty($item['options']['icon']) ? $item['options']['icon'] : array());
// Add an icon selector input element.
$form['options']['icon'] = array(
'#access' => $access,
'#type' => 'icon_selector',
'#default_bundle' => $defaults['bundle'],
'#default_icon' => $defaults['icon'],
'#default_wrapper' => $defaults['wrapper'],
'#default_wrapper_class' => $defaults['wrapper_class'],
);
// Additional configuration on where to place the icon in the block.
$icon_state = array(
'invisible' => array(
':input[name="' . $parents . '[icon]"]' => array(
'value' => '',
),
),
);
$form['options']['icon']['position'] = array(
'#access' => $access,
'#type' => 'select',
'#title' => t('Position'),
'#description' => t('Choose where to position the icon in the menu item.'),
'#options' => array(
'title_before' => t('Before title'),
'title_after' => t('After title'),
'title_invisible' => t('Invisible title'),
'title_replace' => t('Replace title'),
),
'#default_value' => $defaults['position'],
'#states' => $icon_state,
);
$form['options']['icon']['breadcrumb'] = array(
'#access' => $access,
'#type' => 'checkbox',
'#title' => t('Show in breadcrumbs'),
'#description' => t('Choose whether to show the icon in breadcrumb links.'),
'#default_value' => $defaults['breadcrumb'],
'#states' => $icon_state,
);
$form['options']['icon']['title_wrapper'] = array(
'#access' => $access,
'#type' => 'checkbox',
'#title' => t('Title Wrapper'),
'#description' => t('Choose whether to wrap the title in a element tag or not.'),
'#default_value' => $defaults['title_wrapper'],
'#states' => $icon_state,
);
$wrap_state = array(
'visible' => array(
':input[name="' . $parents . '[title_wrapper]"]' => array(
'checked' => TRUE,
),
),
'invisible' => array(
':input[name="' . $parents . '[icon]"]' => array(
'value' => '',
),
),
);
$form['options']['icon']['title_wrapper_element'] = array(
'#access' => $access,
'#type' => 'select',
'#title' => t('Title Wrapper Element'),
'#description' => t('The type of element to use for the title wrapper.'),
'#default_value' => $defaults['title_wrapper_element'],
'#options' => array_combine(array(
'span',
'div',
), array(
'span',
'div',
)),
'#states' => $wrap_state,
);
$form['options']['icon']['title_wrapper_class'] = array(
'#access' => $access,
'#type' => 'textfield',
'#title' => t('Title Wrapper Class'),
'#description' => t('The classes to apply to the title wrapper'),
'#default_value' => $defaults['title_wrapper_class'],
'#states' => $wrap_state,
);
}
Functions
Name | Description |
---|---|
icon_menu_defaults | Helper function to return the default icon menu values. |
icon_menu_form_menu_edit_item_alter | Implements hook_form_FORM_ID_alter(). |
icon_menu_form_node_form_alter | Implements hook_form_FORM_ID_alter(). |
icon_menu_icon_permission | Implements hook_icon_permission(). |
icon_menu_menu_breadcrumb_alter | Implements hook_menu_breadcrumb_alter(). |
icon_menu_preprocess_link | Implements hook_preprocess_link(). |
_icon_menu_form_alter | Add the icon configuration to a menu item edit form. |