Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split the code of a module into separated files

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.

like image 536
Shoaib Nawaz Avatar asked Aug 19 '10 10:08

Shoaib Nawaz


People also ask

How do you divide a module?

In integer division and modulus, the dividend is divided by the divisor into an integer quotient and a remainder. The integer quotient operation is referred to as integer division, and the integer remainder operation is the modulus.

How do you split code in Python?

Python String split() MethodThe split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

Can Webpack split code into separate files?

Code splitting is one of the most compelling features of webpack. This feature allows you to split your code into various bundles which can then be loaded on demand or in parallel.

Why do people split code across Python modules?

Using multiple files to create sub-modules helps keep the code organized and makes reusing code between projects/functions much easier. Functions and variables defined within a module importable into other modules and allows you to scope your function and variable names without worrying about conflicts.


2 Answers

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',
    ),
    // …
  );
}
like image 114
apaderno Avatar answered Nov 14 '22 05:11

apaderno


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.

like image 32
googletorp Avatar answered Nov 14 '22 05:11

googletorp