2

I have two pages, the first page 'test' has links to test/%. On test/% I want to have different titles but some how this wildcard and the title callback aren't working properly.

/**
 * Implements hook_menu().
 */
function cjgimport_menu() {
  $items['test'] = array(
    'title' => t('Test title'),
    'title callback' => 'function_title_callback',
    'title arguments' => array(0),
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array('access content'),
    'page callback' => 'function_content_callback',
  );

  $items['test/%'] = array(
    'title' => t('Test title'),
    'title callback' => 'function_title_callback',
    'title arguments' => array(1),
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array('access content'),
    'page callback' => 'function_content_callback',
  );

  return $items;
);

/**
 * Custom callback function for title
 */
function function_title_callback($arg) {
  if($arg == 0) {
    $title = ucfirst(arg(0));
  } else {
    $title = ucfirst(arg(1));
  }
  return $title;
}

Can anyone tell me why the title doesn't work on the test/% pages?

1
  • If you dpm($arg), do you see what you expect to see?
    – Mołot
    Jan 2, 2014 at 10:47

2 Answers 2

5

You need to provide the wildcard as title arguments:

$items['test/%'] = array(
  'title' => t('Test title'),
  'title callback' => 'function_title_callback',
  'title arguments' => array(1),
  'type' => MENU_NORMAL_ITEM,
  'access arguments' => array('access content'),
  'page callback' => 'function_content_callback',
);

And obviously make sure that your function is actually called function_title_callback() (which you've listed in hook_menu()), and not cjgimport_title_callback(), which you have listed in the question.

You should also be using the $arg variable, not the arg() function in your callback...but I guess that might be the result of debugging.

6
  • Yes sorry, was making that as 'test' code. Forgot to change the function. I added the title arguments as array(1). The function_title_callback($arg) does not seem to work though. Jan 2, 2014 at 10:26
  • Did you clear the cache after changing hook_menu()? It needs to be done every time as routes are cached
    – Clive
    Jan 2, 2014 at 10:29
  • @Erikdekamps can you make your question match your comment, then? Or ask another question? And "does not seem to work" is very, very vague. Expected and encountered behaviour would be better ;) And if it's not, in fact, cache, can you simplify your callback, get rid of ucfirst and possibly switch - so we could know if the callback is called at all, and what's in it's $arg?
    – Mołot
    Jan 2, 2014 at 10:32
  • I'm sorry, I will change the part where it says $arg. The 'title arguments' => array(1) does pass through the $arg and works but in the $items['test/%/'] I would expect it to pass through the second argument. Jan 2, 2014 at 10:39
  • Oh. You need to return $items from hook_menu() obviously ;)
    – Clive
    Jan 2, 2014 at 11:05
0

Please remember to put the loader function in the same file where your menu function is in, it usually is the .module file. In my case, my loader function wouldnt fire when i put it in the .inc file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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