1

I have a problem that's driving me bananas on Drupal 7.

My need is very simple. I have a site with a bunch of communities with URLs like

http://domain.example/community/communityname

All of these communities have similar structures with pages like "learning", "blog" etc. which are at URLs such as

http://domain.example/community/communityname/learning

I need to create a navigation menu for each of these communities without actually creating a separate menu item for each. How can this be achieved?

I tried doing this with hook_menu and even after 3 hours of relentless googling and fiddling about I just can't seem to get this working.

Any help is much appreciated.

Here's my non-working code:

function mymodule_menu(){
    $items = array();
    $items['community/%mymodule_arg/learning'] = array(
        'title' => 'Learning',
        'page callback' => 'mymodule_view_page',
        'page arguments' => array(1), 
        'menu_name' => 'menu-community-menu',
        'type' => MENU_NORMAL_ITEM,
        'access callback'=>TRUE
    );
    return $items;
}

function mymodule_arg_to_arg($arg) {
    return (empty($arg) || $arg == '%') ? "community-a" : $arg;
}

function mymodule_view_page($context) {
    return drupal_goto("community/" . $context . "/learning");
}

If I replace the wildcard from the items array key and replace it with a static string, the code works. Also if I just return a string from mymodule_arg_to_arg() it works. But I just can't seem to make it dynamic.

1

You seem to me trying to make it more complicated as necessary. You can simply use the arg function http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/arg/7

in your example domain http://domain.example/community/communityname

arg(0) = community;

arg(1) = communityname;

so why not just use arg(0) and arg(1) in your dynamic link url

  • Sorry for being a bit dense, but I don't really understand how the answer maps to my problem. I'm aware of the arg function and I use it for parsing the community from the URL, but how will it help be create the menu? My current placeholder implementation is to hack my way out with a block that evaluates php-code, but I really think that's not a very good solution, even if it works. – Tommi Forsström Dec 8 '11 at 17:26

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.