Skip to content

Instantly share code, notes, and snippets.

@mordonez
Created February 28, 2012 10:08
Show Gist options
  • Save mordonez/1931704 to your computer and use it in GitHub Desktop.
Save mordonez/1931704 to your computer and use it in GitHub Desktop.
Drupal 7: Adding images for menu items using attributes
<?php
/**
* @file
*
*/
/**
* Implements hook_form_BASE_FORM_ID_alter().
*
* Adds menu item fields to the node form.
*
* @see menu_node_submit()
*/
function ymbra_menu_images_form_node_form_alter(&$form, $form_state) {
if (!empty($form['menu'])) {
if (isset($form['menu']['link']['mlid']['#value'])) {
$options = unserialize(db_query('SELECT options FROM {menu_links} WHERE mlid = :mlid', array(':mlid' => $form['menu']['link']['mlid']['#value']))->fetchField());
}
if (!isset($options) || !isset($options['menu_image'])) {
$options = array('menu_image' => array('image_fid' => NULL));
}
$form['menu']['link']['image_fid'] = array(
'#title' => t('Image'),
'#type' => 'managed_file',
'#description' => t('The uploaded image will be displayed on listing pages.'),
'#default_value' => !empty($options['menu_image']['image_fid']) ? $options['menu_image']['image_fid'] : '',
'#upload_location' => 'public://menu_images/',
'#upload_validators' => array(
'file_validate_extensions' => array('gif png jpg jpeg'),
),
);
$form['menu']['link']['image_title'] = array(
'#title' => t('Title'),
'#type' => 'textfield',
'#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'),
'#default_value' => !empty($options['menu_image']['image_title']) ? $options['menu_image']['image_title'] : '',
);
$form['menu']['link']['image_alt'] = array(
'#title' => t('Alternate text'),
'#type' => 'textfield',
'#description' => t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'),
'#default_value' => !empty($options['menu_image']['image_alt']) ? $options['menu_image']['image_alt'] : '',
);
}
}
/**
* Implements hook_node_submit().
*
* @see menu_form_node_form_alter()
*/
function ymbra_menu_images_node_submit($node, $form, $form_state) {
$link = &$node->menu;
// Decompose the selected menu parent option into 'menu_name' and 'plid', if
// the form used the default parent selection widget.
if (!empty($link['parent']) && $link['enabled']) {
// If fid is not 0 we have a valid file.
$fid = $link['image_fid'];
// If fid is not 0 we have a valid file.
if ($fid != 0) {
// The new file's status is set to 0 or temporary and in order to ensure
// that the file is not removed after 6 hours we need to change it's status
// to 1. Save the ID of the uploaded image for later use.
$file = file_load($fid);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
// When a module is managing a file, it must manage the usage count.
// Here we increment the usage count with file_usage_add().
file_usage_add($file, 'ymbra_menu_images', 'image', 1);
}
// If the file was removed we need to remove the module's reference to the
// removed file's fid, and remove the file.
elseif ($fid == 0) {
// Retrieve the old file's id.
$fid = isset($link['options']['menu_image']['image_fid']) ? $link['options']['menu_image']['image_fid'] : FALSE ;
$file = $fid ? file_load($fid) : FALSE;
if ($file) {
file_usage_delete($file, 'ymbra_menu_images', 'image', 1);
file_delete($file);
}
}
}
}
/**
* Implements hook_node_insert().
*/
function ymbra_menu_images_node_insert($node) {
ymbra_menu_images_node_save($node);
}
/**
* Implements hook_node_update().
*/
function ymbra_menu_images_node_update($node) {
ymbra_menu_images_node_save($node);
}
/**
* Helper for hook_node_insert() and hook_node_update().
*/
function ymbra_menu_images_node_save($node) {
if (isset($node->menu)) {
$link = &$node->menu;
if (empty($link['enabled'])) {
if (!empty($link['mlid'])) {
menu_link_delete($link['mlid']);
}
}
else {
//Image File
if ($link['image_fid']) {
$link['options']['menu_image']['image_fid'] = $link['image_fid'];
}
else {
unset($link['options']['menu_image']['image_fid']);
}
//Image title
if (trim($link['image_title'])) {
$link['options']['menu_image']['image_title'] = $link['image_title'];
}
else {
unset($link['options']['menu_image']['image_title']);
}
//Image description
if (trim($link['image_alt'])) {
$link['options']['menu_image']['image_alt'] = $link['image_alt'];
}
else {
unset($link['options']['menu_image']['image_alt']);
}
if (!menu_link_save($link)) {
drupal_set_message(t('There was an error saving the menu link.'), 'error');
}
}
}
}
/**
* Implements hook_form_alter().
*/
function ymbra_menu_images_form_menu_edit_item_alter(&$form, $form_state, $form_id) {
if (isset($form['mlid']['#value'])) {
$options = unserialize(db_query('SELECT options FROM {menu_links} WHERE mlid = :mlid', array(':mlid' => $form['mlid']['#value']))->fetchField());
}
if (!isset($options) || !isset($options['menu_image'])) {
$options = array('menu_image' => array('image_fid' => NULL));
}
$form['image'] = array(
'#type' => 'fieldset',
'#weight' => 5,
'#title' => t('Menu image settings'),
'#attributes' => array('classes' => array('theme-settings-bottom')),
);
if (module_exists('image')) {
$form['image']['image_fid'] = array(
'#title' => t('Image'),
'#type' => 'managed_file',
'#description' => t('The uploaded image will be displayed on listing pages.'),
'#default_value' => !empty($options['menu_image']['image_fid']) ? $options['menu_image']['image_fid'] : '',
'#upload_location' => 'public://menu_images/',
'#upload_validators' => array(
'file_validate_extensions' => array('gif png jpg jpeg'),
),
);
$form['image']['image_title'] = array(
'#title' => t('Title'),
'#type' => 'textfield',
'#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'),
'#default_value' => !empty($options['menu_image']['image_title']) ? $options['menu_image']['image_title'] : '',
);
$form['image']['image_alt'] = array(
'#title' => t('Alternate text'),
'#type' => 'textfield',
'#description' => t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'),
'#default_value' => !empty($options['menu_image']['image_alt']) ? $options['menu_image']['image_alt'] : '',
);
}
$form['submit']['#weight'] = 9;
$form['delete']['#weight'] = 10;
$form['#submit'][] = 'ymbra_menu_images_form_submit';
}
/**
* Process the submitted form
*
*/
function ymbra_menu_images_form_submit($form, &$form_state) {
$options = unserialize(db_query('SELECT options FROM {menu_links} WHERE mlid = :mlid', array(':mlid' => $form_state['values']['mlid']))->fetchField());
// If fid is not 0 we have a valid file.
if ($form_state['values']['image_fid'] != 0) {
// The new file's status is set to 0 or temporary and in order to ensure
// that the file is not removed after 6 hours we need to change it's status
// to 1. Save the ID of the uploaded image for later use.
$file = file_load($form_state['values']['image_fid']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
// When a module is managing a file, it must manage the usage count.
// Here we increment the usage count with file_usage_add().
file_usage_add($file, 'ymbra_menu_images', 'image', 1);
//Save the fid
$options['menu_image']['image_fid'] = $file->fid;
}
// If the file was removed we need to remove the module's reference to the
// removed file's fid, and remove the file.
elseif ($form_state['values']['image_fid'] == 0) {
// Retrieve the old file's id.
$fid = isset($options['menu_image']) ? $options['menu_image']['image_fid'] : NULL;
$file = $fid ? file_load($fid) : FALSE;
if ($file) {
file_usage_delete($file, 'ymbra_menu_images', 'image', 1);
file_delete($file);
unset($options['menu_image']['image_fid']);
}
}
if (trim($form_state['values']['image_title'])) {
$options['menu_image']['image_title'] = $form_state['values']['image_title'];
}
else {
unset($options['menu_image']['image_title']);
}
if (trim($form_state['values']['image_alt'])) {
$options['menu_image']['image_alt'] = $form_state['values']['image_alt'];
}
else {
unset($options['menu_image']['image_alt']);
}
if (!isset($options['attributes'])) {
$options['attributes'] = array();
}
if (!isset($options['attributes']['class'])) {
$options['attributes']['class'] = array();
}
db_update('menu_links')
->fields(array(
'options' => serialize($options),
))
->condition('mlid', $form_state['values']['mlid'])
->execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment