Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional argument in Drupal hook_menu implementation

How can I set one of the page arguments in my drupal menu item as an optional page argument?

I have

$items['activities_list/%/%']=array(
'title callback' => 'activities_list_title',
'title arguments' =>array(1),
'description' =>'All the Indicators divided by Coutry',
'page callback' => 'activities_list',
'access arguments' => array('access ppi'),
'page arguments' => array(1,2)
); 

If I call activities_list/76 for example without a third argument I will receive a page not found error. How Can I set the Third parameter as an optional one?

Thanks!

like image 724
perpetual_dream Avatar asked Jun 19 '12 08:06

perpetual_dream


1 Answers

It's easier than you think :). Don't set any arguments in your path and just pass them to your "page callback" function.

$items['activities_list']=array(
  'title callback' => 'activities_list_title',
  'description' =>'All the Indicators divided by Coutry',
  'page callback' => 'activities_list',
  'access arguments' => array('access ppi'),
);

And the page callback function would look like this:

function activities_list($arg1, $arg2)
{
    // Your code goes here...
    drupal_set_title(activities_list_title($arg1, $arg2));
}

You can alter the page title using the following code. (Not tested, kindly let me know if it worked):

function activities_list_title($arg_1, $arg_2)
{
    $title = "";
    // Your code goes here
    return $title;
}

Hope this helps... Muhammad.

like image 54
Muhammad Reda Avatar answered Oct 18 '22 10:10

Muhammad Reda