Contextual menu question

Events happening in the community are now at Drupal community events on www.drupal.org.
ruess's picture

Hey all,

Relatively new to developing in Drupal and need some help with the basics.

What I'd like to do is simply create a menu item in the main menu that grabs the current page's URL and uses one of the arguments to create the new menu item.

I found a great tutorial example which seems very simple, but something I'm doing is wrong.

In the code below (on a brand new D7 install), the inclusion of "/%" in "$items['coach/%']" suddenly makes the menu item disappear, while leaving it just as "$items['coach']" allows the menu item to remain. Can anyone see if there is anything apparently wrong with this code? What would make the inclusion of the /% disappear???

<?php

/**
* Implements hook_menu().
*/
function coachmenu_menu() {
  $items = array();

    $items['coach/%'] = array(
    'title' => 'Example Page',
    'page callback' => 'coach_content',
    'access arguments' => array('TRUE'),
'page arguments' => array(1),
    'type' => MENU_NORMAL_ITEM,
'menu_name' => 'main-menu',
  );


  return $items;
}


function coach_content($wildcard) {
  $content = array(
    '#type' => 'markup',
    '#markup' => '<p>'. t('The wildcard contains the value "%wildcard".', array('%wildcard' => $wildcard)) . '</p>',
  );
  return $content;
}

Comments

Don't want that trailing %

chellman's picture

You only need to use the % when the argument is somewhere in the middle of your path, like if you had items like this:

$items['coach/%/edit']
$items['coach/%/negotiate']
$items['coach/%/fire']

Anything that appears after the array keys above will get passed as additional arguments. You say "uses one of the arguments to create the new menu item", which makes me wonder if I'm missing something, but anyway, you can leave off the /% and it should work as I'm expecting it to, at least.

Thanks for the response

ruess's picture

Thanks for the response chellman. That makes sense and when I leave out the additional % at the end, the menu item does in fact appear again which is good. However, the menu item does not pick up the argument from the url past the 'coach/', it simply acts as if that does not exist. I am wondering, should my
'page arguments' => array(1),
still be array(1)?

Is there anything else I should consider?

Thanks,

kevin

Remove the page arguments

chellman's picture

I'm sure you've looked at it already, but take another look at the API page for hook_menu(), specifically where it takes about page arguments.

I'm still not totally clear if there's something about what you're looking to do that I'm missing (you said "create menu items", about which I'm not clear), but I think you can remove the %, as you've already done, but also remove that page arguments line. Each slash-delimited token after coach will be a separate argument to your callback function. You only need to specify the page arguments if the default behavior doesn't work the way you need it to (i.e. you need a static argument for some reason, or you need arguments to be passed to your callback in some weird order).

Thanks Chellman, So what I

ruess's picture

Thanks Chellman,

So what I would like to do is to create a contextual menu item in one of my menus that grabs the last argument of the current page's url and uses it to create a similar but different link for the new menu item.

For instance:
if the page I'm on currently is: http://mysite.com/coach/[joesmith]

I would like the menu item to be: http://mysite.com/page/[joesmith]

(coach and page are different content types btw)

The code I'm trying right now based upon what you said is:

       <?php
       function coachmenu_menu() {
          $items = array();
         
       $items['page'] = array(
            'title' => 'Coach Page',
            'page callback' => 'coachpagelink',
         'access arguments' => array('access content'),
          'type' => MENU_NORMAL_ITEM,
           'menu_name'=>"menu-coach-menu",
       );
           return $items;
     }

     function coachpagelink($wildcard) {
            return $wildcard;
      }

. . . but it's still not working. Is my function coachpagelink ok?

Thanks!

kevin