0

I am trying to get into module building with Drupal 7.

Why is $tryk = "test" not being taken for the title, but literal strings such as "testmenu" are working?

$tryk = "test";
function menufun_menu() {
    global $tryk;
    $items['menufun'] = array(
        'title' => $tryk,
        'page callback' => 'menufun_greeting',
        'file' => 'menufun_greeting.inc',
        'page arguments' => array('Jane', 'Doe'),
        'access callback' => TRUE,
        'type' => MENU_NORMAL_ITEM,
);
return $items;
} 
  • Have you put global $tryk before $tryk = "test"; ? If not it isn't considered as a global variable. – tostinni Feb 16 '12 at 0:03
  • The question is about a generic PHP feature, not a Drupal feature (which means the question is probably off-topic here). Module files are included files, and your $tryk variable is not global. – kiamlaluno Feb 16 '12 at 0:28
  • @tostinni, kiamlaluno you are right...it was the global...so shame on me...this is my lack of php knowledge ;-) – Jurudocs Feb 16 '12 at 0:38
4

If you want to use a variable for the page title, it is better to use a title callback.

function menufun_menu() {
    $items['menufun'] = array(
        'title callback' => 'menufun_title',
        'page callback' => 'menufun_greeting',
        'file' => 'menufun_greeting.inc',
        'page arguments' => array('Jane', 'Doe'),
        'access callback' => TRUE,
        'type' => MENU_NORMAL_ITEM,
    );
    return $items;
} 
function menufun_title(){
    $tryk = 'test'; // dynamic title set here.
    return $tryk;
}
| improve this answer | |
  • thanks for the answer...do you know why it is better to use the callback? – Jurudocs Feb 16 '12 at 8:33
  • Because that's the only way ;) the information in hook_menu() is only parsed once and then stored. – Berdir Feb 16 '12 at 10:16
  • title in hook_menu is used for static titles and title callback is used to display dynamic titles, such as information from the database. – Sithu Feb 17 '12 at 2:36

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.