1

I just started learning Drupal 7 Module development. In a tutorial I watched, the person said that

"In hook_menu(), if we have a 'file' attribute then the 'file path' is a must and drupal wont work if it is not declared in drupal 7."

But when I read a book, it said that

The 'file_path' is not required in drupal 7 provided that the file is present in the same directory.

As you can see, its a bit confusing. So my questions are

1) Is 'file path' required in Drupal 7 ?

2) When to use 'file path' ?

I've posted a snippet of the hook_menu implementation :

function modulename_menu() {
  $items['...'] = array(
    ....
    ....
    'file' => 'modulename.admin.inc',
    'file path' => drupal_get_path('module','modulename'),
  );
  return $items;
} 

Note : Assume that the file is present in the same modules folder

2

The first sentence you quoted is wrong. "file path" is not a must, if you set "file"; in fact, by default Drupal will take its value is the directory containing the module implementing the menu. If you want to use a different directory, such as drupal_get_path('module', 'mymodule') . '/includes'), then you can set "file path."

_menu_router_build() contains the following code.

// Calculate out the file to be included for each callback, if any.
if ($item['file']) {
  $file_path = $item['file path'] ? $item['file path'] : drupal_get_path('module', $item['module']);
  $item['include file'] = $file_path . '/' . $item['file'];
}

$item['module'] is set by menu_router_build() using the following code.

  foreach (module_implements('menu') as $module) {
    $router_items = call_user_func($module . '_menu');
    if (isset($router_items) && is_array($router_items)) {
      foreach (array_keys($router_items) as $path) {
        $router_items[$path]['module'] = $module;
      }
      $callbacks = array_merge($callbacks, $router_items);
    }
  }

A module could implement hook_menu_alter() to alter $item['module'].

  • Note for future self: clear caches in-between altering file path. – Dominic Woodman Jan 5 '15 at 12:12
1

1) Is 'file path' required in Drupal 7 ?

Required if file being included is not in module path. Its not mandatory if the file is included in the module itself.
file path: The path to the directory containing the file specified in "file". This defaults to the path to the module implementing the hook.

2) When to use 'file path' ?

When you want to include a file outside your module.

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.