Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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.

EDIT

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.

share|improve this question

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
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 at 14:01

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.

share|improve this answer
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 at 17:51
1  
there really isn't a way for the first and potentially only term to be dynamic for all sorts of reasons. – Jimajamma Feb 11 at 18:46
I agree and hence working to find an alternative solution :) Anyways Thanks for your response.. – Nitesh Feb 12 at 1:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.