3

I have used hook_menu() with a page callback as follows:

function profilecb_menu() {
  $items['profilecb'] = array(
    'title' => 'Profile CB',
    'page callback' => 'pcb_exe',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

  return $items;
}

The callback code is the following:

function pcb_exe($id) {
  $ar = array('arg1'=> 'value1', 'arg2'=> $id);

  return $ar;
}

I have been able to make the callback function return a single string; when I try to return an array, I get an error.

All I need to do is to make the callback function do some tasks, like connecting to the database, and return to a template a complex array of useful information.

How can I do that?

1

In Drupal 6, a menu callback needs to return a string that contains the HTML to output together the output of other modules; alternatively, it can print the output and call exit().

If you need to pass an array to a theme function, then the output returned from the menu callback should be output from a theme function to which the menu callback passes an array.

function pcb_exe($id) {
  $ar = array('arg1'=> 'value1', 'arg2'=> $id);
  return theme('pcb_exe_output', $ar);
}

In Drupal 7 (and higher versions), the output of a menu callback is normally an array that has the format used by drupal_render().
It still possible to pass an array to a theme function, but the parameters passed to a theme function changed.

function pcb_exe($id) {
  $ar = array('arg1'=> 'value1', 'arg2'=> $id);
  return theme('pcb_exe_output', $ar);
}

In this case, there is no noticeable difference, as the parameter passed to the theme function was already an array. The parameter passed to a theme function since Drupal 7 is an array containing all the values required from the theme function. See the code of theme_vertical_tabs() as example:

function theme_vertical_tabs($variables) {
  $element = $variables['element'];
  // Add required JavaScript and Stylesheet.
  drupal_add_library('system', 'drupal.vertical-tabs');

  $output = '<h2 class="element-invisible">' . t('Vertical Tabs') . '</h2>';
  $output .= '<div class="vertical-tabs-panes">' . $element['#children'] . '</div>';
  return $output;
}
  • I tried using your code above for the function pcb_exe($id) but it does not work. the page returned is blank. what could I be doing wrong? – Carlos Muñiz Jun 3 '11 at 22:21
  • I have drupal 7 BTW – Carlos Muñiz Jun 3 '11 at 22:23
  • @Charlie You need to define the theme function theme_pcb_exe_output() too, as Drupal doesn't define it. – kiamlaluno Jun 3 '11 at 22:23
  • And declare it in hook_theme(). I suggest you read api.drupal.org/api/drupal/includes--theme.inc/function/theme/7, that contains a lot of information about the theming API – Berdir Jun 3 '11 at 23:41
  • @Charlie I am glad I could help you. – kiamlaluno Jun 3 '11 at 23:54

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.