Optional argument in Drupal hook_menu implementation

Asked
Active3 hr before
Viewed126 times

5 Answers

optionalargument
90%

If I call activities_list/76 for example without a third argument I will receive a page not found error. How Can I set the Third parameter as an optional one?, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,You can alter the page title using the following code. (Not tested, kindly let me know if it worked):, What is the Biblical basis for civil disobedience, by not wearing a mask in the midst of COVID-19, despite health department rules?

It's easier than you think :). Don't set any arguments in your path and just pass them to your "page callback" function.

$items['activities_list'] = array(
   'title callback' => 'activities_list_title',
   'description' => 'All the Indicators divided by Coutry',
   'page callback' => 'activities_list',
   'access arguments' => array('access ppi'),
);

And the page callback function would look like this:

function activities_list($arg1, $arg2) {
   // Your code goes here...
   drupal_set_title(activities_list_title($arg1, $arg2));
}

You can alter the page title using the following code. (Not tested, kindly let me know if it worked):

function activities_list_title($arg_1, $arg_2) {
   $title = "";
   // Your code goes here
   return $title;
}
load more v
88%

hook_menu() implementations return an associative array whose keys define paths and whose values are an associative array of properties for each path. (The complete list of properties is in the return value section below.),"page arguments": An array of arguments to pass to the page callback function, with path component substitution as described above.,If you happen to call arg(X) within the callback function, and the argument is not set, a "Page not found" page will be displayed instead. ,"theme arguments": An array of arguments to pass to the theme callback function, with path component substitution as described above.

load more v
72%

The reason is that fast_404_validate_path_sql() checks in the menu_router table for a route matching 'abc/def/3/bar' in the case mentioned above, but the table contains only 'abc/def'.,Looks like this is a duplicate of #2005930: Fast 404 module is returning 404 pages for menu paths having %. Please reopen if it is not.,Top Drupal contributor Acquia would like to thank their partners for their contributions to Drupal.,I noticed that when custom paths are developed and they aren't availble in the DB and the path check is used that the 404 page is fired even if the path exist. For example, I created a custom path "/mycallback/*/*" in hook_menu. When the real path is checked in the DB (like "/mycallback/foo/bar") that this is not found and wrongly a 404 is fired. I created a new configuration entry for fast 404 module that can be added in the settings.php file like this: $conf['fast_404_whitelist_paths'] = array('ajax/*'); In this way all whitelisted paths are just bypassed by DB check.

Actually, this issue is different from #2005930: Fast 404 module is returning 404 pages for menu paths having %. If a hook_menu implementation accepts optional path arguments, like in the following example from the API, abc/def/3/bar triggers a fast 404 but should not. Note, this is the first example of hook_menu on the API page.

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

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

The above code will work correctly only for that specific path, with both parameters. Neither the path /main, nor /main/first will work, only /main/first/second. If you want the parameters to be optional, so /main, /main/first, and /main/first/second are all valid paths, you need to make some changes to the YAML file.,The controller then looks like this (showing the parameters in the function signature)::,This upcasting only happens if you have type-hinted the entity object in your controller parameter. Otherwise it will simply be the value of the parameter.,To wire up the menu_alter there is also a MODULE.services.yml file with an entry that points to the class that does the work:

In Drupal 7 the code might look like this:

function example_menu() {
   $items = array();
   $items['main'] = array(
      'title' => 'Main Page',
      'page callback' => example_main_page ',
      'access arguments' => array('access content'),
      'type' => MENU_NORMAL_ITEM,
      'file' => 'MODULE.pages.inc'
   );
   return $items;
}

function example_main_page() {
   return t(‘Something goes here’);
}
load more v
75%

Sometimes, it will be necessary to auto-load more based on more than one argument. Since only the named argument is passed to the loader by default, one needs to explicitly tell Drupal which extra load arguments should be passed on to the loader. For example, to load a specific revision of a node, it is necessary to pass to node_load() a node ID and a revision ID. That can be accomplished by the following code.,Since the access callback by default is user_access, it can be left out, as in the above code.,If you want to pass more data to the page callback, you can use page arguments to achieve this. Page arguments should be an array of arguments to pass on to the page callback. If an integer is used as an argument it will represent a part of the URL, starting from 0, incremented once for each slash (/). In the following example, this means the 0 will be turned into 'hello'., 1 How could I register a php file to a path using hook_menu? It is a custom written php file which includes drupal bootstrap, uses drupal session variables and accepts an argument which is a user id. – Елин Й. Mar 5 '13 at 11:55

Add the following code to the module file.

/**
 * Implements hook_menu().
 */
function helloworld_menu() {
   $items['hello'] = array(
      'title' => 'Hello world!',
      'page callback' => 'helloworld_page',
      'access callback' => TRUE,
   );

   return $items;
}

/**
 * Page callback for /hello.
 */
function helloworld_page() {
   return 'Hello world!';
}
load more v

Other "optional-argument" queries related to "Optional argument in Drupal hook_menu implementation"