10

I am very curious to know how a Drupal module can be dis-integrated into multiple include files. A number of hook support to link include components, like hook_menu, hook_theme etc.

Once I planned to simplify one of my complex module that have reached to 2.3K of lines with half of its feature set. I have to roll back all those steps due to lack of knowledge about the scope of inclusion.

Help me in this regard if there is some detailed information.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Shoaib Nawaz
  • 2,302
  • 4
  • 29
  • 38

3 Answers3

10

What Nikit said is correct.
I will add there are some hooks that are allowed to define which files need to be loaded. Examples of such hooks are hook_theme(), and hook_menu().

A module should never unconditionally load a file it needs calling module_load_include() from outside a function.

function book_menu() {
  $items['admin/content/book'] = array(
    'title' => 'Books',
    'description' => "Manage your site's book outlines.",
    'page callback' => 'book_admin_overview',
    'access arguments' => array('administer book outlines'),
    'file' => 'book.admin.inc',
  );
  $items['admin/content/book/list'] = array(
    'title' => 'List',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/content/book/settings'] = array(
    'title' => 'Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('book_admin_settings'),
    'access arguments' => array('administer site configuration'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 8,
    'file' => 'book.admin.inc',
  );
  // …
}

function user_theme() {
  return array(
    'user_picture' => array(
      'arguments' => array('account' => NULL),
      'template' => 'user-picture',
    ),
    'user_profile' => array(
      'arguments' => array('account' => NULL),
      'template' => 'user-profile',
      'file' => 'user.pages.inc',
    ),
    // …
    'user_admin_perm' => array(
      'arguments' => array('form' => NULL),
      'file' => 'user.admin.inc',
    ),
    // …
  );
}
Community
  • 1
  • 1
apaderno
  • 28,547
  • 16
  • 75
  • 90
  • But my main intention is about several data access functions those are used in various parts. e.g. comments. `comment_total_count()`, `comment_today_count()` is used in templates, `comment_thread()` is required on admin page as well as on content page. `comment_insert()` on content page while `comment_remove()` and `comment_edit()` on admin page. Do I need to use conventional `require` or `include` syntax? And what about to include multiple files? – Shoaib Nawaz Aug 23 '10 at 22:00
  • 1
    The function used to include PHP files is [`module_load_include()`](http://api.drupal.org/api/function/module_load_include/6). You can include one, or more files. – apaderno Aug 23 '10 at 23:42
7

Using more files, is just a matter of grouping similar things together in the same file, to keep it more managed. Typical files used is

  • .admin.inc for all administration stuff, menu callbacks, forms etc.
  • .pages.inc for frontend menu callbacks.
  • .theme.inc for theme functions, preprocess hooks etc.
  • .forms.inc for non admin forms and their handlers.

This is more a coding style that anything else. So this is just to help yourself maintain the code you have written.

googletorp
  • 33,075
  • 15
  • 67
  • 82
  • 1
    It's for performance as well. All the functions from .module files get loaded at every initialisation. – ram4nd Sep 20 '14 at 03:35
3

It's simple, just review other big modules (like cck, views, etc). Main hooks should be in module, others should be in different files - themers, admin pages, other pages, service functions and so on...

Nikit
  • 5,128
  • 19
  • 31