3

i was wondering is it possible to create custom menu named arguments in D7. I can do this

function MY_MODULE_menu(){
  $items['my/callback/%user'] = array(
    ...
    'page callback' => 'MY_MODULE_callback',
    'page arguments' => array(2),
    ....
  );
}

and it will load the user entity. So instead i want to do this:

function MY_MODULE_menu(){
  $items['my/callback/%my_entity'] = array(
    ...
    'page callback' => 'MY_MODULE_callback',
    'page arguments' => array(2),
    ....
  );
}

and my custom entity to be loaded.

1 Answer 1

1

Yes, name your function my_entity_load and it will be passed the id.

From the Drupal hook_menu documentation (emphasis mine).

Auto-Loader Wildcards Registered paths may also contain special "auto-loader" wildcard components in the form of '%mymodule_abc', where the '%' part means that this path component is a wildcard, and the 'mymodule_abc' part defines the prefix for a load function, which here would be named mymodule_abc_load(). When a matching path is requested, your load function will receive as its first argument the path component in the position of the wildcard; load functions may also be passed additional arguments (see "load arguments" in the return value section below). For example, your module could register path 'my-module/%mymodule_abc/edit':

  $items['my-module/%mymodule_abc/edit'] = array(
    'page callback' => 'mymodule_abc_edit',
    'page arguments' => array(1),
  );

When path 'my-module/123/edit' is requested, your load function mymodule_abc_load() will be invoked with the argument '123', and should load and return an "abc" object with internal id 123:

  function mymodule_abc_load($abc_id) {
    return db_query("SELECT * FROM {mymodule_abc} WHERE abc_id = :abc_id", array(':abc_id' => $abc_id))->fetchObject();
  }
1
  • I have no idea how i missed that, the answer was RTFM :) Thanks Thomas for the help and pointing the solution out. Dec 13, 2013 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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