1

This question relates to How do I redirect a taxonomy menu item to another path?

I followed the recommendations of Clive and wrote a hook_menu_alter to provide my callback.

function material_menu_taxonomy_page_callback($term) {
  dsm('Entered material menu callback');
}

function material_menu_menu_alter () {
  $items['taxonomy/term/%taxonomy_term']['page callback'] = 'material_menu_taxonomy_page_callback'; 
}

I placed a code line dsm("Altering"); in the callback to see where in the process the callback is being called, but it never shows up.

I am fairly certain that I have the code correct.

  • The hook has the name of the custom module.
  • The callback name is correct.
  • the callback is in the same file as the hook
  • I have turned the module off and then on so that I can be sure that Drupal knows about mymodule_menu_alter
  • And I have flushed the caches.

My assumption is that mymodule_menu_alter()' should change the callback in the menu item array and that the callback will be called each time the menu item is selected. That suggests that mydsm()` message should display at the top of the page requested by the menu.

While Clive's test indicated in his answer worked, my revisions did not. Following Clive's answer recommendations I revised the code as follows.

material.info file contained in modules subdirectory = sites/all/modules/Custom/material

name = Materials  
description = Redefine the path for materials menu
package = CR-Hub
core = 7.x
; Includes
files[] = material.module

material.module file in same subdirectory

function material_taxonomy_page_callback() {
  drupal_goto('taxonomy/term/1');
  // return 'Entered material menu callback';
}

function material_menu_alter(&$items) {
  // return 'Entered Material Menu Alter';
  $items['taxonomy/term/%taxonomy_term']['page callback'] = 'material_taxonomy_page_callback'; 
  return $items;
}

When I select any menu item neither drupal_goto(), or return 'any text' lines redirect the selected path from the menu item.

4

You haven't passed $items into the function ;)

function material_menu_menu_alter (&$items) {
  $items['taxonomy/term/%taxonomy_term']['page callback'] = 'material_menu_taxonomy_page_callback'; 
}

Without that you're not actually altering anything as $items doesn't exist in a global scope.

You'll also need to clear the caches again once you've made the change.

UPDATE

Working code that I've just verified:

material.info:

name = Materials  
description = Redefine the path for materials menu
package = CR-Hub
core = 7.x
; Includes
files[] = material.module

material.module

function material_taxonomy_page_callback($term) {
  // Calling drupal_goto() here will result in an infinite loop.
  // This is the page callback for pages with a URL like taxonomy/term/1 so
  // there's no need to redirect...you're already where you need to be.
  // This function is used to build up the new page content for the taxonomy page

  // As a test just output the name of the term related to this page.
  return $term->name;
}

function material_menu_alter (&$items) {
  $items['taxonomy/term/%taxonomy_term']['page callback'] = 'material_taxonomy_page_callback'; 

  // You don't need to return anything from this function as $items is passed in by reference...anything you change in that variable will be reflected in the $items var in the calling function.
}

Once installed if I go to any taxonomy page the content region contains just the name of the taxonomy term for that page (using return $term->name). If it's not working for you the only thing I can advise is to clear Drupal's caches, or check what custom/contributed modules you've got installed that might be overriding your code.

16
  • Yeah sure, pick the easy stuff! Other than that, whats wrong with it??? :)
    – Ashlar
    Mar 21 '12 at 21:02
  • I've just popped your code (with the slight change above) into a module called material_menu and it works perfectly...the only difference was I added return 'test'; to the callback function so a page would actually be displayed. It's probably a stupid question but is the module definitely called material_menu, i.e. is the folder the module file is in called material_menu?
    – Clive
    Mar 21 '12 at 21:23
  • The module is called material_menu. This is my first venture in hooks. I will be adding it into a custom module for a website I am attempting to build. The intent is that I have a taxonomy called materials and have generated a taxonomy menu for it. The Material (280 so far) selected defines a category and a series of views in a definition will deliver subcategories (8 total) attached to tabs to deliver different types of articles relating to the material. To do so I must redirect the path from the taxonomy path to invoke the desired views. That's where hook_menu_alter comes in.
    – Ashlar
    Mar 21 '12 at 23:54
  • BTW your assistance is greatly appreciated! Learning Drupal code feels like swallowing an elephant. Its very helpful to have someone who knows how to cut it into bite size chunks.
    – Ashlar
    Mar 21 '12 at 23:57
  • I tried repeating your successful test and cannot reproduce it. I placed my updated code in the question. I rewrote the module and it will not react to drupal_go_to or return 'text'; messages. The menu was defined using taxonomy menu. Could that be changing the required $items [key] needed?
    – Ashlar
    Mar 22 '12 at 16:15
2

I think what you may be missing is that the taxonomy menu item has a file set to look for the callback in the taxonomy.pages.inc

Try this:

$items['taxonomy/term/%taxonomy_term']['page callback'] = 'your_callback';
$items['taxonomy/term/%taxonomy_term']['file'] = '';
1
  • 1
    As long as your_callback() is in a module file, it is fine to leave the value of $items['taxonomy/term/%taxonomy_term']['file'] as it was before, especially if that file still needs to be loaded to use one of the functions it contains.
    – apaderno
    Apr 13 '12 at 16:40
1

I was trying to do exactly the same thing described in Clive's answer, but the override was not working.

Finally I found out that what was preventing it from working was that I had the module Taxonomy Display enabled on the site. Disabling it was the solution to my problem.

So, you should disable any views overriding taxonomy/term/% but you should also check your enabled modules!

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.