0

I have written a custom module and have defined a menu inside the module as test. This menu uses a function named mymodule_test() to render contents. The code written for this menu is working perfectly OK and I am getting expected output on /test page.

Now, I want to define some more menus that use the same mymodule_test(). I understand that I can define more menu items in the mymodule_menu() as below.

$items['test-1'] = array(
  'title' => 'Test',
  'page callback' => 'drupal_get_form',
  'page arguments' => array('mymodule_test'),
  'access arguments' => array('administer mymodule'),
  'type' => MENU_NORMAL_ITEM,
);

However, I am planing to use more than 100 such menu and believe this is not the best way to achieve this. Please guide me in the right direction to achieve the same.

This is to note that these menus will be dynamic in nature so I cannot hard code it in the module file.

I am using Drupal 6.

2
  • Hello Nitesh, Did you got any solution for the dynamic URL creation without using array. Right now I'm having same requirement for a website which I'm developing. – James Jan 23 '15 at 12:05
  • Nothing James and had to use arrays :( – Nitesh Jan 23 '15 at 16:44
2

Firstly, you should not define a callback as ['/test-1'], instead just use ['test-1'], without the slash - '/'. Check here - http://api.drupal.org/api/drupal/developer!hooks!core.php/function/hook_menu/6

If you want to create 100 menus, go ahead, this is the way you can do this. If just the path is changing and nothing else, I would recommend to create an array, and then loop it, defining the value of the array item in the menu-path. something like this -

$arr = array('test-1', 'test-2', 'test-3', 'test-4', 'test-5');
foreach ($arr as $a) {
  $items[$a] = array(
  'title' => 'Test',
  'page callback' => 'drupal_get_form',
  'page arguments' => array('mymodule_test'),
  'access arguments' => array('administer mymodule'),
  'type' => MENU_NORMAL_ITEM,
  );
}

This will be less code and quick.

1
  • Thanks for making the point. I edited the question. Secondly, I understand this can be done via arrays and this is the easiest solution. However, these menu items are going to be dynamic in nature and end users are going to define these URL keywords. Hope you understand the situation better now. – Nitesh Feb 7 '13 at 14:01
2

If you do something like this inside your module_menu() hook/function...

$items['tests/%'] = array(
  'title' => 'Test',
  'page callback' => 'drupal_get_form',
  'page arguments' => array('mymodule_test'),
  'access arguments' => array('administer mymodule'),
  'type' => MENU_NORMAL_ITEM,
);

...paths such as tests/foo, tests/bar, tests/baz will all work and should call your form identically.

In fact, in your current implementation, calling test-1/foo will work, as will test-1/bar and test-/baz because drupal will try to be smart and call the best menu callback it can when it finds a partial match (for no better explanation).

Check out http://api.drupal.org/api/drupal/includes!menu.inc/group/menu/6 for all the nitty gritty.

But do note that in these simple implementations, any text will work after tests/ or test-1 so if you need to be more restrictive, that will require a lot more programming effort and without knowing where your users are getting these keywords and how they are stored in your module, that's a whole 'nother set of questions and answers.

4
  • Thanks for detailed answer. You're absolutely correct. The problem is I want to define the 1st word after the slash - mysite.com/test-1. In this example, I want test-1 to be dynamic. Any ideas of how can I achieve this? – Nitesh Feb 11 '13 at 17:51
  • 2
    there really isn't a way for the first and potentially only term to be dynamic for all sorts of reasons. – Jimajamma Feb 11 '13 at 18:46
  • I agree and hence working to find an alternative solution :) Anyways Thanks for your response.. – Nitesh Feb 12 '13 at 1:57
  • This is the approach I would use. There is a reason why Drupal uses node/1 and user/1. Also, in this way the code is more maintainable. – kiamlaluno Jul 11 '16 at 14:09

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.