• Avail guidance to develop Drupal modules & Drupal themes
  • Share and get review of your code
  • Get Access to free CodeBooks and ThemeBooks
  • Drupal 5, 6 and 7 covered

About bhavinhjoshi

Image menu

Printer-friendly versionSend to a DeveloperPDF version
2
0

This CodeLet will allow you to add an image to a menu by overriding the menu in the template.php file of you theme. Also, the module offer admin side control to select and put the image to a menu item.

<?php
//$ID$

/**
*
* @file
*
* Manage FEI navigation
*
* hook = fei_navigation
*
* @author Drupal Developer
*/


/**
* Implementation of hook_help
  *
  *
*  @author Drupal Developer
  */
function fei_navigation_help($path) {
  switch (
$path) {
    case
'admin/build/fei-navigation':
     
$output = t("Here, you can control the CTA menu display.");
      return
$output;

    case
'admin/build/fei-navigation/add':
     
$output = t("Add CTA to menu");
      return
$output;
  }
}


/**
* Implementation of hook_menu
  *
* @author Bhavin H. Joshi <bhavin@viruteinfo.com>
  */
function fei_navigation_menu() {
 
$item = array();

 
$item['admin/build/fei-navigation'] = array(
   
'title' => 'FEI CTA Menu Configuration',
   
'title callback' => 't',
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('fei_navigation_list'),
   
'access callback' => 'user_access',
   
'access arguments' => array('Administer site configuration'),
   
'weight' => 0,
  );

 
$item['admin/build/fei-navigation/list'] = array(
   
'type' => MENU_DEFAULT_LOCAL_TASK,
   
'title' => 'FEI CTA',
   
'title callback' => 't',
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('fei_navigation_list'),
   
'access callback' => 'user_access',
   
'access arguments' => array('Administer site configuration'),
   
'weight' => -10,
  );

 
$item['admin/build/fei-navigation/add'] = array(
   
'type' => MENU_LOCAL_TASK,
   
'title' => 'Add CTA',
   
'title callback' => 't',
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('fei_navigation_admin'),
   
'access callback' => 'user_access',
   
'access arguments' => array('Administer site configuration'),
   
'weight' => -9,
  );

 
$item['admin/build/fei-navigation/cta/%/%'] = array(
   
'type' => MENU_CALLBACK,
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('fei_navigation_admin', 4, 5),
   
'access callback' => 'user_access',
   
'access arguments' => array('Administer site configuration'),
   
'weight' => -9,
  );

 
$item['get/cta'] = array(
   
'type' => MENU_CALLBACK,
   
'page callback' => 'get_cta',
   
'page arguments' => array(2),
   
'access callback' => 'user_access',
   
'access arguments' => array('Administer site configuration'),
   
'weight' => 0,
  );

 
$item['worldwide/node'] = array(
   
'type' => MENU_CALLBACK,
   
'page callback' => '_get_worldwide_node',
   
'page arguments' => array(2),
   
'access callback' => TRUE,
  );

 
$item['import'] = array(
   
'type' => MENU_CALLBACK,
   
'page callback' => 'import_data',
   
'access callback' => TRUE,
   
'file' => 'import.inc.php',
   
'file_path' => drupal_get_path("module", 'fei_navigation'),
  );

  return
$item;
}


/**
* fei_navigation_admin
*
* Manage & configure FEI CTA in menus
*
* @author Drupal Developer
*/
function fei_navigation_admin($form, $fei_id = NULL, $op = NULL) {
 
$form = array();

  if (
$op) {
    switch (
$op) {
      case
'edit':
       
$r__menu = db_query("SELECT * FROM {fei_navigation} WHERE fei_id = %d",
         
$fei_id);
        while (
$om__menu = db_fetch_object($r__menu)) {
         
$ss__node_title = fei_get_node_title($om__menu->nid) ." - ". t("node")
            .
": ". $om__menu->nid;
         
$ss__mlid = $om__menu->mlid;
          list(
$width, $height) = preg_split("/(x|X)/", $om__menu->image_size);
          if (!
$height) {
           
$ss__imagecase_size = $width;
          }
          else {
           
$ss__size = $om__menu->image_size;
          }
         
$sn_weight = $om__menu->weight;
        }
        break;
      case
'status':
       
db_query("UPDATE {fei_navigation} SET status = !status WHERE fei_id = %d",
         
$fei_id);
       
drupal_set_message(t("CTA status has been changed."));
       
drupal_goto($_GET['destination']);
        break;
      case
'delete':
       
db_query("DELETE FROM {fei_navigation} WHERE fei_id = %d", $fei_id);
       
drupal_set_message(t("CTA has been deleted."));
       
drupal_goto($_GET['destination']);
        break;
    }

   
$form['fei_id'] = array(
     
'#type' => 'hidden',
     
'#value' => $fei_id,
    );

   
$form['cta_update'] = array(
     
'#type' => 'submit',
     
'#value' => t("Update CTA menu"),
     
'#submit' => array('fei_navigation_cta_update'),
     
'#weight' => 50,
    );
  }
  else {
   
$form['cta_submit'] = array(
     
'#type' => 'submit',
     
'#value' => t("Add CTA to menu"),
     
'#submit' => array('fei_navigation_cta_submit'),
     
'#weight' => 50,
    );

  }
 
$form['nodes'] = array(
   
'#type' => 'textfield',
   
'#title' => t("CTA node"),
   
'#description' => t("Search the CTA node"),
   
'#autocomplete_path' => ($op != 'edit') ? 'get/cta' : NULL,
   
'#default_value' => $ss__node_title,
   
'#disabled' => ($op == 'edit') ? TRUE : FALSE,
   
'#required' => ($op != 'edit') ? TRUE : FALSE,
  );

 
$am__parent_menu = menu_get_menus(FALSE);
  foreach (
$am__parent_menu as $key => $value) {
   
$am__menu_tree = array_values(menu_tree_page_data($key));
    foreach (
$am__menu_tree as $k => $v) {
     
$am__menu[$value][$v['link']['mlid']] = $v['link']['title'];
    }
  }

 
$am__menu[0] = "-- ". t("Select menu") ." --";
 
$form['menus'] = array(
   
'#type' => 'select',
   
'#title' => t("Menu"),
   
'#options' => $am__menu,
   
'#description' => t("Select the menu, the CTA should appear in"),
   
'#default_value' => ($ss__mlid) ? $ss__mlid : 0,
   
'#required' => TRUE,
  );

  if (
module_exists('imagecache')) {
   
$presets = imagecache_presets();
    foreach (
$presets as $preset) {
     
$am__preset[$preset['presetid']] = $preset['presetname'];
    }
   
$am__preset[0] = "-- ". t("Select preset") ." --";
   
$form['cta_size'] = array(
     
'#type' => 'select',
     
'#title' => t("Imagecache presets"),
     
'#options' => $am__preset,
     
'#default_value' => ($ss__imagecase_size) ? $ss__imagecase_size : 0,
     
'#description' => t("Select the preset to set the size of the CTA image.
        Alternatively, you can set custom image size below and leave this field."
),
    );
   
$ss__custom_size = t("This value will be ignored if a preset is selected above.");
  }

 
$form['custome_cta_size'] = array(
     
'#type' => 'textfield',
     
'#title' => t("image size"),
     
'#description' => t("Define your custom size of the CTA image.
        The size should be in wxh or wXh format."
) ." ". $ss__custom_size,
     
'#default_value' => ($ss__size) ? $ss__size : '50X50',
     
'#size' => 20,
     
'#maxlength' => 8,
    );

 
$form['cta_weight'] = array(
   
'#type' => 'textfield',
   
'#title' => t("Weight"),
   
'#size' => 10,
   
'#attributes' => array('class' => 'cta-weight'),
   
'#required' => TRUE,
   
'#default_value' => ($sn_weight) ? $sn_weight : 0,
   
'#maxlength' => 4,
  );

 
$form['#validate'] = array('fei_navigation_cta_validate');
  return
$form;
}


/**
*
* @param $ss__name
* @return unknown_type
*
* @author Drupal Developer
*/
function get_cta($ss__name) {
  if (
$ss__name) {
   
$r__result = db_query("SELECT title, nid FROM {node} WHERE type = 'feicta'
      AND LOWER(title) LIKE LOWER('%s%%') AND status = 1"
, $ss__name);
    while (
$om__result = db_fetch_object($r__result)) {
     
$ss__value = $om__result->title ." - ". t("node") .": ". $om__result->nid;
     
$am__data[$ss__value] = check_plain($om__result->title) ." - ". t("node")
        .
": ". $om__result->nid;
    }
    if (empty(
$am__data)) {
      return
drupal_json("");
    }
    else {
      return
drupal_json($am__data);
    }
  }
}

/**
*
* @param $form
* @param $form_state
* @return unknown_type
*/
function fei_navigation_cta_submit($form, &$form_state) {
 
$an__nid = preg_split("/(node:)/", $form_state['values']['nodes']);
 
$ss__size = $form_state['values']['cta_size'] ?
   
$form_state['values']['cta_size'] : $form_state['values']['custome_cta_size'];

 
db_query("INSERT INTO {fei_navigation} VALUES(fei_id, %d, %d, '%s', '%s',
    '%s')"
, $an__nid[1], $form_state['values']['menus'], $ss__size,
   
$form_state['values']['cta_weight'], 1);

 
drupal_set_message(t("CTA has been saved."));
}



/**
  *
* Enter description here...
* @param $form
* @param $form_state
* @return unknown_type
  */
function fei_navigation_cta_update($form, &$form_state) {
 
$sn__fei_id = $form_state['values']['fei_id'];
 
$ss__size = $form_state['values']['cta_size'] ?
   
$form_state['values']['cta_size'] : $form_state['values']['custome_cta_size'];

 
db_query("UPDATE {fei_navigation} SET mlid = %d, image_size = '%s',
    weight = '%s' WHERE fei_id = %d"
, $form_state['values']['menus'],
   
$ss__size, $form_state['values']['cta_weight'], $sn__fei_id);
 
drupal_set_message(t("CTA has been update."));
}



/**
  *
* @param $form
* @param $form_state
* @return unknown_type
  */
function fei_navigation_cta_validate($form, &$form_state) {
 
$an__nid = preg_split("/(node:)/", $form_state['values']['nodes']);
  if (!
$form_state['values']['menus']) {
        
form_set_error('menus', t("Menu is required"));
    }
 
$sn__menu_id = db_result(db_query("SELECT mlid FROM {fei_navigation} WHERE nid = %d",
   
$an__nid[1]));
  if (
$sn__menu_id == $form_state['values']['menus'] &&
   
$form_state['values']['menus'] != 0) {
   
form_set_error('nodes', t("This node is already assigned to the menu.
      Selecte a different menu or node."
));
  }
  if (
$form_state['values']['cta_size'] == 0 &&
   
$form_state['values']['custome_cta_size'] == NULL) {
   
form_set_error('custome_cta_size', t("Image size is required"));
  }
}


/**
  *
* Enter description here...
* @param $ss__menu_id
* @return unknown_type
*/
function get_fei_cta_menu($ss__menu_id) {
  if (
$ss__menu_id) {
   
$r__result = db_query_range("SELECT cta.nid, cta.weight, cta.image_size,
      fei.field_feicta_image_fid, fei.field_feicta_image_data,
      fei.field_feicta_url_value url, n.title, nr.body, f.filepath FROM {fei_navigation} cta
      INNER JOIN {content_type_feicta} fei USING ( nid ) INNER JOIN {files} f
      ON fei.field_feicta_image_fid = f.fid INNER JOIN {node} n USING ( nid )
      INNER JOIN {node_revisions} nr USING ( nid ) WHERE cta.mlid = %d AND
      cta.status = 1 AND n.status = 1 ORDER BY cta.weight"
, $ss__menu_id, 0, 3);
     
$sn__vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE name = 'fei-vocabulary'"));
    while (
$om__result = db_fetch_object($r__result)) {

      
$sm__term = db_result(db_query("SELECT t.name FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid
         WHERE t.vid = %d AND r.vid = %d"
, $sn__vid, $om__result->nid));


     
$pattern = array('x', 'X');
     
$am__image_data = unserialize($om__result->field_feicta_image_data);
      list(
$width, $height) = preg_split("/(x|X)/", $om__result->image_size);

     
$option = array('html' => TRUE);
     
$ss__body_link = l("<strong>". $sm__term ."</strong><span>".strip_tags($om__result->body) ."</span>", $om__result->url, $option);
     
$am__ctal_link['title'][] = $om__result->title;
     
$am__ctal_link['body'][] = $ss__body_link;
     
$am__ctal_link['image'][] = $om__result->filepath;;
     
/*if (!$height) {
        $ss__preset = db_result(db_query("SELECT presetname FROM
          {imagecache_preset} WHERE presetid= %d", $width));
        $ss__image = theme('imagecache', $ss__preset, $om__result->filepath,
          $am__image_data['alt'], $am__image_data['title']);
      }
      else {
        $am__size = array('height' => $height, 'width' => $width);
        $ss__image = theme('image', $om__result->filepath,
          $am__image_data['alt'], $am__image_data['title'], $am__size, FALSE);
      }
      $ss__body_link = l(strip_tags($om__result->body), $om__result->url);
//      $ss__cta_link = l($ss__image, $om__result->url, array('html' => TRUE));
      $am__ctal_link['title'][] = $om__result->title;
      $am__ctal_link['body'][] = $ss__body_link;
      $am__ctal_link['image'][] = $ss__image;*/
   
}
    return
$am__ctal_link;
  }
}



/**
  *
* Enter description here...
* @return unknown_type
  */
function get_stored_cta() {
 
$header = array(
   
t("Node"),
   
t("Menu"),
   
t("Image size"),
   
t("Status"),
   
t("Weight"),
    array(
'data' => t("Operation"), 'colspan' => 2),
  );

 
$ss__destination = drupal_get_destination();
 
$r__result = db_query("SELECT * FROM {fei_navigation} ORDER BY weight asc");
  while (
$om__result = db_fetch_object($r__result)) {
   
$ss__node_title = db_result(db_query("SELECT title FROM {node} WHERE
      nid = %d"
, $om__result->nid));
   
$ss__menu_name = db_result(db_query("SELECT link_title FROM {menu_links} WHERE
      mlid = %d"
, $om__result->mlid));
    list(
$width, $height) = preg_split("/(x|X)/", $om__result->image_size);
    if (!
$height) {
     
$ss__preset = db_result(db_query("SELECT presetname FROM
        {imagecache_preset} WHERE presetid= %d"
, $width));
     
$ss__size = l($ss__preset, 'admin/build/imagecache/'. $width);
    }
    else {
     
$ss__size = $om__result->image_size;
    }

   
$am__weight['cta_weight_'. $om__result->fei_id] = array(
     
'#type' => 'textfield',
     
'#size' => 5,
     
'#default_value' => $om__result->weight,
     
'#attributes' => array('class' => 'cta-weight'),
    );

   
$ss__status = ($om__result->status) ? t("Enabled") : t("Disabled");
   
$ss__disabled = (!$om__result->status) ? "menu-disabled" : NULL;
   
$row[] = array('data' => array (
     
l($ss__node_title, "node/". $om__result->nid),
     
$ss__menu_name,
     
$ss__size,
     
l($ss__status, 'admin/build/fei-navigation/cta/'. $om__result->fei_id
       
."/status", array('attributes' => array('title' => t("Click to toggle")),
       
'query' =>  $ss__destination)),
     
drupal_render(form_builder(NULL, $am__weight, $form_state)),
     
l(t("edit"), 'admin/build/fei-navigation/cta/'. $om__result->fei_id ."/edit"
       
, array('query' =>  $ss__destination)),
     
l(t("delete"), 'admin/build/fei-navigation/cta/'. $om__result->fei_id ."/delete"
       
, array('query' =>  $ss__destination)),
        ),
       
'class' => "draggable ". $ss__disabled,
    );
    unset(
$am__weight);
  }

 
$am__attributes = array('id' => 'cta-menu-added');
 
drupal_add_tabledrag('cta-menu-added', 'order', 'sibling', 'cta-weight');
  return (!empty(
$row) ? theme('table', $header, $row, $am__attributes) : NULL);
}


/**
  *
* Enter description here...
* @param $nid
* @return unknown_type
  */
function fei_get_node_title($nid) {
  return
db_result(db_query("SELECT title from {node} WHERE nid = %d", $nid));
}



/**
  *
* Enter description here...
* @return unknown_type
  */
function fei_navigation_list() {
 
$form = array();
 
$am__cta = get_stored_cta();
 
$form['cta_added'] = array(
   
'#type' => 'item',
   
'#value' => get_stored_cta(),
   
'#weight' => -10,
  );
  if (!empty(
$am__cta)) {
     
$form['cta_update'] = array(
       
'#type' => 'submit',
       
'#submit' => array('fei_cta_rearrange_update'),
       
'#value' => t("Save configuration"),
       
'#weight' => 50,
      );
  }
  return
$form;
}


/**
*
* @param $form
* @param $form_state
* @return unknown_type
*/
function fei_cta_rearrange_update($form, $form_state) {
 
$am__post_key = array_keys($form_state['clicked_button']['#post']);
 
$am__cta_weight = preg_grep('/^(cta_weight_)/', $am__post_key);
  for (
$i = 0; $i < count($am__cta_weight); $i++) {
   
$am__fei_id = preg_split('/^(cta_weight_)/', $am__cta_weight[$i], -1,
     
PREG_SPLIT_NO_EMPTY);
    if (
$am__fei_id[0]) {
     
db_query("UPDATE {fei_navigation} SET weight = %d WHERE fei_id = %d",
       
$form_state['clicked_button']['#post'][$am__cta_weight[$i]], $am__fei_id[0]);
    }
  }
 
drupal_set_message(t("Configuration has been saved."));
}



function
fei_navigation_block($op = 'list', $delta = 0, $edit = array()){
  switch (
$op) {
    case
'list':
     
$blocks[0] = array('info' => t("WorldWide custom filter block"),
       
'weight' => 0);
      return
$blocks;
    case
'view':
     
$block = array();
      switch (
$delta) {
        case
0:
         
$block = array('subject' => t("World wide"),
           
'content' => drupal_get_form('world_block_filter'));
        break;
      }
      return
$block;
  }
}


/**
*
* @return unknown_type
*/
function world_block_filter() {
 
$form = array();
 
$sn__vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE name = 'Worldwide'"));
 
$am__term_tree = taxonomy_get_tree($sn__vid);
 
$form['#method'] = 'GET';
 
$form['#action'] = url('world-wide');
 
$am__node = array();
  foreach (
$am__term_tree as $om__term) {
   
$am__node = _get_worldwide_node($om__term->tid);
   
$am__node[0] =  $om__term->name;
   
ksort($am__node);

   
$form['worldwide_'. $om__term->tid] = array(
     
'#type' => 'select',
     
'#options' => $am__node,
    );
  }
  return
$form;
}



/**
*
* @param $sn__tid
* @return unknown_type
* @author Drupal Developer
*/
function _get_worldwide_node($sn__tid) {
 
$form_build_id = $_POST['form_build_id'];
 
$form_id = $_POST['form_id'];
 
$form_state =  array('storage' => NULL, 'submitted' => FALSE);

 
$r__node = taxonomy_select_nodes(array($sn__tid));
  while (
$om__node = db_fetch_object($r__node)) {
   
$am__node[$om__node->nid] = $om__node->title;
  }

  return
$am__node;
}


function
fei_navigation_form_world_block_filter_alter(&$form, $form_state) {
  foreach (
$form as $key => $field) {
    if (
$field['#type'] == 'select') {
     
$form[$key]['#attributes'] = array('onselect' => 'this.form.submit()');
    }
  }
}
?>

beautifulmind's picture
Offline
Joined: 02/05/2010
Points: 179
Image menu

To make the image menu work properly, you have to create a function in your tempate.php file to inject images created using the image menu interface. Rest will be taken care by the code itself.

 

Joined: 03/06/2009
Points: 428
Image Menu

Yes, that's true.
Thanks for drawing attention.

Syndicate content

Sitestats

Recent comments

rss-top-right.png
toolbar powered by www.iconcy.com