1

In my template file I have 2 links like

if(mytest){
  print l(t('Action 1'), 'my_first_link/'.$id);
}else{
  print l(t('Action 2'), 'my_second_link/'.$id);
}

In my custom module I have

function myModule_menu() {
  $items = array();
  $items['my_first_link'] = array(
    'title' => 'First function',
    'page callback' => array('myModule_first_function'),
    'page arguments' => array(1,),
  );
  $items['disableproject'] = array(
    'title' => 'Second function',
    'page callback' => array('myModule_second_function'),
    'page arguments' => array(1,),
  );
  return $items;
}
function myModule_first_function($url_argument){
  print('url_argument = '.$url_argument);
}
function myModule_second_function($url_argument){ 
 print('url_argument = '.$url_argument);
}

My first link works fine but my second doesn't!

It gives me a 404 error!

How can I solve it?

Thank you

3

You need to add 'access arguments' to your call:

$items['disableproject'] = array(
  'title' => 'Second function',
  'page callback' => array('myModule_second_function'),
  'page arguments' => array(1),
  'access arguments' => array('some permission'),
);

Or you can add a custom access callback:

$items['disableproject'] = array(
  'title' => 'Second function',
  'page callback' => array('myModule_second_function'),
  'page arguments' => array(1),
  'access callback' => 'some_access_callback_function',
);
  • thanks for your quick reply. I used 'access callback' => 'user_access', and it works fine – A.Sana Mar 23 '18 at 9:21
  • You don't need 'access callback' => 'user_access', that's the default. If you leave it out, that will do the same thing. It's access arguments that matter in that scenario. – Clive Mar 23 '18 at 9:34
1

You can also define the access arguments true, use this if you want this link to be accessible by anyone else use what Jaypan has suggested. BTW I believe that you have flushed your drupal cache after making changes to hook_menu

$items['disableproject'] = array(
  'title' => 'Second function',
  'page callback' => array('myModule_second_function'),
  'page arguments' => array(1),
  'access arguments' => TRUE,
);
  • Thank you Sir.. It's just accessible for admin. I keep your solution for next tasks :) Thanks again – A.Sana Mar 23 '18 at 9:25
  • This will result in an error - access arguments must be an array. You probably meant access callback. – Clive Mar 23 '18 at 9:33
  • I tested with 'access arguments' => TRUE, and it's ok – A.Sana Mar 23 '18 at 10:11
  • It's not @A.Sana, clear the cache and you'll see the error message. It's very well documented: api.drupal.org/api/drupal/modules%21system%21system.api.php/…. "access arguments": An array of arguments to pass to the access callback function. Emphasis mine – Clive Mar 23 '18 at 10:59

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.