1

in short I want the same functionality as the taxonomy module where you can have an URL like "localhost/drupal/admin/structure/taxonomy/occupational_fields/edit" where occupational_fields was the aliased part.

The menu entry in taxonomy for the page callback looks like this:

$items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name/edit'] = array(
  'title' => 'Edit',
  'page callback' => 'drupal_get_form',
  'page arguments' => array('taxonomy_form_vocabulary', 3),
  'access arguments' => array('administer taxonomy'),
  'type' => MENU_LOCAL_TASK,
  'weight' => -10,
  'file' => 'taxonomy.admin.inc',
);

The function declaration looks like this:

function taxonomy_form_vocabulary($form, &$form_state, $edit = array())

Now my implementation looks like this:

$items['admin/config/mapper/%tid/edit'] = array(
  'title'            => 'Mapper edit mapping',
  'description'      => 'Add or edit a mapping',
  'page callback'    => 'drupal_get_form',
  'page arguments'   => array('mapper_form_mappings', 3),
  'access arguments' => array('administer site configuration'),
  'type'             => MENU_LOCAL_TASK,
  'file'             => 'mapper.admin.inc',
);

And respectively:

function mapper_form_mappings($form, &$form_state, $edit = array())

But when an URL like "localhost/drupal/admin/config/mapper/11/edit" is opened the function above is not called.

I tried googling a good URL alias tutorial for module programming, but couldn't find one. Maybe I searched for the wrong thing?

edit: I can imagine that I need some type of handler, but I couldn't find one in the taxonomy files and the tutorials didn't mention them either.

  • Your code worked fine for me, cleared the caches, checked the menu_router for the new path, all was there. Can you clear all caches and check for the entry in that table (menu_router) ? – Stefanos Petrakis Sep 4 '16 at 18:09
  • Thank you for your comment. I'll do some more tests then, just for clarification, does the placeholder ( in my case %tid ) have any significance? Or could I also write %something instead? – Splitframe Sep 5 '16 at 0:44
0

Okay, here is a proper answer to a tricky question.

Documentation

First of all, you should read the following two documents, if you haven't already:

Simple Wildcards

By using % as a path wildcard you are instructing the menu_router matching algorithm to use the path argument as it is (used mostly for numerical arguments). So, following from your comment, yes, it does have a significance. If you write your code like this:

$items['admin/config/mapper/%/edit'] = array(
  'title'            => 'Mapper edit mapping',
  'description'      => 'Add or edit a mapping',
  'page callback'    => 'drupal_get_form',
  'page arguments'   => array('mapper_form_mappings', 3),
  'access arguments' => array('administer site configuration'),
  'type'             => MENU_LOCAL_TASK,
  'file'             => 'mapper.admin.inc',
);

you are going to get the 3rd part of your path as an argument to your mapper_form_mappings function. Exactly as it occurs in the path.

Auto-Loader Wildcards

Now, if you would use a %NAME type of wildcard, like taxonomy does, you would be triggering the 'Autoloader Wildcards' (a.k.a. 'Wildcard Loader Arguments') functionality. Drupal will try to load an object through a function called NAME_load. In the case of the %taxonomy_vocabulary_machine_name Drupal will try to load a vocabulary using the specified machine name as an argument to the taxonomy_vocabulary_machine_name_load function. The loaded object will then be passed as an argument to drupal_get_form (or any other function defined in your path definition).

Current problem

In your case, your original code:

$items['admin/config/mapper/%tid/edit'] = array(
  'title'            => 'Mapper edit mapping',
  'description'      => 'Add or edit a mapping',
  'page callback'    => 'drupal_get_form',
  'page arguments'   => array('mapper_form_mappings', 3),
  'access arguments' => array('administer site configuration'),
  'type'             => MENU_LOCAL_TASK,
  'file'             => 'mapper.admin.inc',
);

implies there is a tid_load function that will receive the %tid argument from the URL and should return some object to drupal_get_form. This is however not working, since such a function does not exist.

Alternatives using wildcard loaders

If you want to use the 'Auto-Loader Wildcards' functionality, you should do one of the following:

  • Use an existing _load function, e.g. taxonomy_term_load. Your wildcard should be called %taxonomy_term in this case and the final argument passed to drupal_get_form would be an taxonomy term object.
  • Use a custom _load function, e.g. mapper_load. Your wildcard should be called %mapper in this case and the final argument passed to drupal_get_form would be whatever your mapper_load function returns.

Best of luck!

| improve this answer | |
  • Wow, huge thanks for this awesome little tutorial! I wish there were more like these for core drupal features. I really struggle to find on-point instructions most of the time or don't know what exact to search for. The first link had most if this I see, I just haven't found it :( – Splitframe Sep 5 '16 at 12:50
  • Very welcome, a pleasure to run this through my head too. Practice makes perfect, see you around! – Stefanos Petrakis Sep 5 '16 at 13:32

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.