For example, if I have a module1 that has implemented the following hook_menu:

<?php
/**
 * Implements hook_menu().
 */
function mymodule1_menu () {
  
$items['admin/my-module/view-all'] = array(
       ....
     
'page callback' => 'mymodule1_callback'
  
);

  return

$items;
}

function

mymodule1_callback () {
...
}
?>

And I have a module2 that extends the hook_menu of module1. I dont want to reset the 'page callback' of the module1 and set only my custom callback. I want that the two pages callbacks (the callback of the module1 and the callback of the module2) are executed when user access this page.

How do I do that? Is it something like this below?

<?php
function mymodule2_menu () {

 

$items['admin/my-module/view-all'] = array(
       ....
     
'page callback' => array('mymodule1_callback', 'mymodule2_callback')
   );

  return

$items;
}

function

mymodule2_callback () {
...
}
?>

I'm using Drupal 7.

Comments

nevets’s picture

You can't. What is your actual goal? Are you trying to add to (but not change) the page generated by the original callback? If so, your module could implement a block, then you can place the block in the main content area and limit it to just the page for the original callback.

Jaypan’s picture

Module 1:

<?php
function mymodule1_menu () {
  
$items['admin/my-module/view-all'] = array(
       ....
     
'page callback' => 'mymodule1_callback'
  
);
  return
$items;
}
?>

Module 2:

<?php
function mymodule2_menu_alter(&$menu)
{
  if(isset(
$menu['admin/my-module/view-all']))
  {
   
$menu['admin/my-module/view-all']['page callback'] = 'mymodule2_combined_callback';
  }
}

function
mymodule2_combined_callback()
{
 
mymodule1_callback();
 
mymodule2_callback();
}
?>

I will soon be leaving the Drupal forums permanently. To understand why, please see this thread.

spyout98’s picture

Thanks Jaypan! That's a way to do what I want. My only one doubt:

Is this the best way to do it?

If so, I will implement what you answered.
If not, what is the best way to add a page callback for a path already defined in hook_menu from another module? (PS: I want to add a page callback, and not reset the existing and set only my function).

Thanks a lot!

Jaypan’s picture

Is this the best way to do it?

It's a way to do it. I don't know of any other. Is it the best? I'd have to see something else to compare it to in order to answer that question. Let me know if you have any ideas.

I will soon be leaving the Drupal forums permanently. To understand why, please see this thread.

spyout98’s picture

No problems, Jaypan.

I just would like to know if there is other ways to do it. It's great to know how to solve different situations with many possibilities.

But your answer and code attend completely what I need.

Thanks a lot, guy! You saved my module!

I hope that this code can help other developers around the world that have the same doubt.

Thanks again Jaypan!