menu_target.module in Menu target 7
Same filename and directory in other branches
Allows content editors to choose wether or not to open menu items in a new window
File
menu_target.moduleView source
<?php
/**
* @file
* Allows content editors to choose wether or not to open menu items in a
* new window
*/
/**
* Implements hook_form_alter().
*/
function menu_target_form_alter(&$form, $form_state, $form_id) {
// Only proceed if the current form is a node edit or menu item edit form.
if (isset($form['#node']) && $form['#node']->type . '_node_form' == $form_id || $form_id == 'menu_edit_item') {
module_load_include('inc', 'menu_target', 'menu_target.admin');
menu_target_edit_form_extra_elements($form, $form_id != 'menu_edit_item');
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function menu_target_form_menu_configure_alter(&$form, $form_state) {
module_load_include('inc', 'menu_target', 'menu_target.admin');
menu_target_menu_configure_extra_elements($form);
}
/**
* Implements hook_preprocess_page().
*/
function menu_target_preprocess_page(&$variables) {
// Only attach the javascript behavior when needed.
if (variable_get('menu_target_enabled', 1) && variable_get('menu_target_type', 'html') == 'javascript') {
drupal_add_js(drupal_get_path('module', 'menu_target') . '/menu_target.js');
}
foreach (array(
'main_menu',
'secondary_menu',
) as $name) {
if (isset($variables[$name])) {
foreach ($variables[$name] as $mlid_name => &$link) {
$mlid = str_replace('menu-', '', $mlid_name);
if (_menu_target_retarget_link($mlid)) {
$targeting = variable_get('menu_target_type', 'html');
// What kind of attributes depend on the settings
if ($targeting == 'html') {
$link['attributes']['target'] = "_blank";
}
else {
$link['attributes']['class'][] = "target-blank";
}
}
}
}
}
}
/**
* Implements hook_menu_link().
*
* @see _menu_target_retarget_link()
*/
function menu_target_preprocess_menu_link(&$variables) {
// Li container attributes
// $variables['element']['#attributes']
// link attributes
// $variables['element']['#localized_options']['attributes']
// Should we add attributes to this link?
if (_menu_target_retarget_link($variables['element']['#original_link']['mlid'])) {
$targeting = variable_get('menu_target_type', 'html');
// What kind of attributes depend on the settings
if ($targeting == 'html') {
$variables['element']['#localized_options']['attributes']['target'] = "_blank";
}
else {
$variables['element']['#localized_options']['attributes']['class'][] = "target-blank";
}
}
}
/**
* Implements hook_menu_link_delete().
* Removes entry from menu_target_links when menu entry is deleted.
* This hook is not detected unless present in the .module file
*
* @see menu_target_edit_form_extra_elements()
* @see menu_target_retarget_link()
* @see _menu_target_drop_link()
*/
function menu_target_menu_link_delete($link) {
module_load_include('inc', 'menu_target', 'menu_target.admin');
if (_menu_target_retarget_link($link['mlid'])) {
_menu_target_drop_link($link['mlid']);
}
}
/**
* Implements hook_menu_link_insert().
* When a new menu item is created, check to see if they selected the
* target attribute. If so, save an entry in the menu_target_links table
*
* @see menu_target_edit_form_extra_elements()
* @see menu_target_set_link()
*/
function menu_target_menu_link_insert($link) {
if (!empty($link['target'])) {
_menu_target_set_link($link['mlid']);
}
}
/**
* Given an mlid, determines whether this link should be altered
*/
function _menu_target_retarget_link($mlid) {
return db_select('menu_target_links', 'm')
->fields('m')
->condition('mlid', $mlid, '=')
->countQuery()
->execute()
->fetchField();
}
/**
* Adds the mlid to the menu_target_links table
*/
function _menu_target_set_link($mlid) {
$data = (object) array(
'mlid' => $mlid,
);
if ($saved = drupal_write_record('menu_target_links', $data)) {
drupal_set_message(t('The menu item for this content will now open in a new window.'));
}
else {
drupal_set_message(t('An error occured while setting the menu target.'), 'error');
}
}
/**
* Removes the mlid from the menu_target_links table
*/
function _menu_target_drop_link($mlid) {
$drop = db_delete('menu_target_links')
->condition('mlid', $mlid)
->execute();
if ($drop) {
drupal_set_message(t('The menu target attribute has been removed.'));
}
else {
drupal_set_message(t('Unable to remove the menu target attribute.'), 'error');
}
}
/**
* Implements hook_form_submit().
* This is only needed when the menu_target attribute is updated, since
* the mlid is not available to this function when the menu item is new.
*
* @see _menu_target_retarget_link()
* @see _menu_target_set_link()
* @see _menu_target_drop_link()
*/
function _menu_target_edit_form_extra_elements_submit($form, &$form_state) {
// Depending on whether we're in a node form or a menu item form, the
// 'mlid' values are stored in a diffrent place of the form state values
// tree.
$mlid = isset($form['#node']) ? $form_state['values']['menu']['mlid'] : $form_state['values']['mlid'];
// mlid will be '0' for new or previously unset values which will instead be
// handled by hook_link_insert
if ($mlid) {
$current = (bool) _menu_target_retarget_link($mlid);
$target = isset($form['#node']) ? $form_state['values']['menu']['target'] : $form_state['values']['target'];
if ($target && !$current) {
// Box has been checked, but record does not exist
_menu_target_set_link($mlid);
}
elseif (!$target && $current) {
// Box is not checked and record exists
_menu_target_drop_link($mlid);
}
}
}
Functions
Name![]() |
Description |
---|---|
menu_target_form_alter | Implements hook_form_alter(). |
menu_target_form_menu_configure_alter | Implements hook_form_FORM_ID_alter(). |
menu_target_menu_link_delete | Implements hook_menu_link_delete(). Removes entry from menu_target_links when menu entry is deleted. This hook is not detected unless present in the .module file |
menu_target_menu_link_insert | Implements hook_menu_link_insert(). When a new menu item is created, check to see if they selected the target attribute. If so, save an entry in the menu_target_links table |
menu_target_preprocess_menu_link | Implements hook_menu_link(). |
menu_target_preprocess_page | Implements hook_preprocess_page(). |
_menu_target_drop_link | Removes the mlid from the menu_target_links table |
_menu_target_edit_form_extra_elements_submit | Implements hook_form_submit(). This is only needed when the menu_target attribute is updated, since the mlid is not available to this function when the menu item is new. |
_menu_target_retarget_link | Given an mlid, determines whether this link should be altered |
_menu_target_set_link | Adds the mlid to the menu_target_links table |