I'm working on learning more module development, and i'm stumped on a really simple example here.

/**
 * Implementation of hook_menu().
 */
function example_Menu() {
  $items['maps'] = array(
    'title' => 'Find Us',
    'description' => 'Use the map to locate our facilities.',
    'page callback' => 'example_map',
    'page arguments' => array('all'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  
  $items['maps/%'] = array(
    'title' => 'Find Us',
    'description' => 'Use the map to locate our facilities.',
    'page callback' => 'example_map',
    'page arguments' => array(1),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  
  return $items;
}

/* I want this function to take either a default argument of all for the maps/ url or pass in a var if there is an arg past maps/*/
function example_map($args) {
  
  /* take the value of what is passed into the function and output it in a simple string. */
  $output = t('This is the output of my argument... @args', $args);
  
  return $output;
}

Comments

nevets’s picture

Two changes

    'page arguments' => array('all'),

should be

    'page arguments' => array(1),

and change

function example_map($args) {

to

function example_map($args = 'all') {

You will need to clear the menu cache after changing example_menu().

rockitdev’s picture

thanks. my function just outputs

This is the output of my argument... @args'

Should in place of @args, it say all?

Drave Robber’s picture

$output = t('This is the output of my argument... @args', array('@args' => $args));

rockitdev’s picture

awesome. that fixes it if i hit a url of /maps/1 i see 1 output. but if i just hit maps/ shouldn't i get an output of all?

nevets’s picture

Try removing 'page arguments' => array(1),, if I recall correctly you only want to specify required arguments.

rockitdev’s picture

/**
 * Implementation of hook_menu().
 */
function example_Menu() {
  $items['maps'] = array(
    'title' => 'Find Us',
    'description' => 'Use the map to locate our facilities.',
    'page callback' => 'example_map',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
    
  return $items;
}

/* I want this function to take either a default argument of all for the maps/ url or pass in a var if there is an arg past maps/*/
function example_map($args = 'all') {
  
  /* take the value of what is passed into the function and output it in a simple string. */
  $output = t('This is the output of my argument... @args', array('@args' => $args));
  
  return $output;
}