0

Trying to extend Rooms (rooms_booking_manager):

Originally, there is an enquiry-page which takes arguments

"enquiry/[startDate]/[endDate]/[roomNumber]"

I want to write a more specific handler IF arguments persons and children are given as in enquiry/startDate/endDate/roomNumber/numberOfPersons/numberOfChildren; in that case my own handler should take precedence.

This is the code used by the module.

function rooms_booking_manager_menu() {
  $items = array();

  $items['enquiry/%start_date/%end_date'] = array(
    'title' => 'Ask about availability',
    'page callback' => 'rooms_booking_manager_enquiry_page',
    'page arguments' => array(1, 2),
    'access arguments' => array('book units'),
    'type' => MENU_CALLBACK,
  );

  // …

  return $items;
}

This is the code I use in my module.

function netfactory_rooms_menu_alter(&$items) {
  $items['enquiry/%start_date/%end_date/%persons/%children'] = array(
    'title' => 'Ask about availability',
    'page callback' => 'netfactory_rooms_enquiry_page',
    'page arguments' => array(1, 2, 3, 4),
    'access arguments' => array('book units'),
    'type' => MENU_CALLBACK,
  );
}

Nevertheless, if I call enquiry/startDate/endDate/roomNumber/numberOfPersons/numberOfChildren rooms_booking_manager_enquiry_page() is called instead of netfactory_rooms_enquiry_page().

Coming from a .NET MVC background, I was thinking that the order (most specific first) is important; so I also tried setting weight=-1000 on my own route, but that did not help either.

How can I hook into a module to add a route which is MORE SPECIFIC than an existing route?

1

For Drupal, enquiry/%start_date/%end_date/%persons/%children and enquiry/%start_date/%end_date are different; so you aren't overriding an old one, but defining a new one, for which you should use hook_menu(), not hook_menu_alter().

You just need to use code similar to the following one.

function netfactory_rooms_menu_alter(&$items) {
  $items['enquiry/%start_date/%end_date'] = array(
    'title' => 'Ask about availability',
    'page callback' => 'netfactory_rooms_enquiry_page',
    'page arguments' => array(1, 2),
    'access arguments' => array('book units'),
    'type' => MENU_CALLBACK,
  );
}

function netfactory_rooms_enquiry_page($start_date, $end_date, $persons = DEFAULT_VALUE_FOR_PERSONS, $children = DEFAULT_VALUE_FOR_CHILDREN) {
  // …
}

(Replace the constants with the values you wish.)

As you read in the first example on hook_menu(), if the page callback is called for abc/def, its parameters will get their default value, while called for abc/def/123/foo, $ghi will be '123' and $jkl will be 'foo'.

  function mymodule_menu() {
    $items['abc/def'] = array(
      'page callback' => 'mymodule_abc_view',
    );
    return $items;
  }

  function mymodule_abc_view($ghi = 0, $jkl = '') {
    // ...
  }

Keep in mind that wildcard parameters like %start_date are used if there is a function named start_date_load() used to return an array or an object that is then passed to the page callback; if you don't have such function, you should use the generic wildcard %. Then, when the parameter is at the end of the path, and you don't use a load function, then there isn't any need to use wildcard parameters.

  • That was the problem - thought I needed named args, but simple '/%' did the job! – reinhardS Oct 26 '15 at 12:40
1

I would try netfactory_rooms_menu() (i.e. hook_menu() instead of hook_menu_alter()). You are effectively creating a new menu callback, not extending an existing one.

Also, CLEAR THE CACHE before trying the new path.

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.