Custom Module Development

What are custom modules?

Custom modules are created by developers to accomplish a specific use case that is required for a specific site. Drupal is flexible enough that you, as a developer, could easily extend it.

Hooks

Hooks are the main components that allow developers to change the way Drupal codes behave. Upon execution, Drupal searches for enabled modules (contributed, core, and custom) which implements a specific hook and calls that hook.

Example hook:


function EXAMPLE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_login') {
    $form['username']['#description'] = t('Enter your username.');
    $form['actions']['submit']['#title'] = t('Submit');
    $form['#submit'][] = 'EXAMPLE_user_login_form';
  }
}
        

Hooks Usage:

Implement a hook (in this case hook_form_alter()) and name the function as MODULENAME_form_alter(). Copy the arguments used in the hook.

Module Files:

  • module.info
  • module.module
  • module.install (optional)

.info File:


name = Media
description = Provides the core Media API
package = Media
core = 7.x

dependencies[] = file_entity
dependencies[] = image
dependencies[] = views

test_dependencies[] = token

files[] = includes/MediaReadOnlyStreamWrapper.inc
files[] = includes/MediaBrowserPluginInterface.inc
files[] = includes/MediaBrowserPlugin.inc
files[] = includes/MediaBrowserUpload.inc
files[] = includes/MediaBrowserView.inc
files[] = includes/MediaEntityTranslationHandler.inc
files[] = includes/media_views_plugin_display_media_browser.inc
files[] = includes/media_views_plugin_style_media_browser.inc
files[] = tests/media.test
files[] = tests/media.entity.test
files[] = tests/media.file.usage.test

configure = admin/config/media/browser
        

.module File:


/**
 * @file
 * Render an administrative menu as a dropdown menu at the top of the window.
 */

/**
 * Implements hook_help().
 */
function admin_menu_help($path, $arg) {
  switch ($path) {
    case 'admin/config/administration/admin_menu':
      return '

' . t('The administration menu module provides a dropdown menu arranged for one- or two-click access to most administrative tasks and other common destinations (to users with the proper permissions). Use the settings below to customize the appearance of the menu.') . '

'; case 'admin/help#admin_menu': $output = ''; $output .= '

' . t('The administration menu module provides a dropdown menu arranged for one- or two-click access to most administrative tasks and other common destinations (to users with the proper permissions). Administration menu also displays the number of anonymous and authenticated users, and allows modules to add their own custom menu items. Integration with the menu varies from module to module; the contributed module Devel, for instance, makes strong use of the administration menu module to provide quick access to development tools.', array('@drupal' => 'http://drupal.org/project/devel')) . '

'; $output .= '

' . t('The administration menu settings page allows you to modify some elements of the menu\'s behavior and appearance. Since the appearance of the menu is dependent on your site theme, substantial customizations require modifications to your site\'s theme and CSS files. See the advanced module README.txt file for more information on theme and CSS customizations.', array('@settings' => url('admin/config/administration/admin_menu'))) . '

'; $output .= '

' . t('The menu items displayed in the administration menu depend upon the actual permissions of the viewer. First, the administration menu is only displayed to users in roles with the Access administration menu (admin_menu module) permission. Second, a user must be a member of a role with the Access administration pages (system module) permission to view administrative links. And, third, only currently permitted links are displayed; for example, if a user is not a member of a role with the permissions Administer permissions (user module) and Administer users (user module), the User management menu item is not displayed.') . '

'; return $output; } }

.install File:


/**
 * @file
 * Install, update, and uninstall functions for the admin menu module.
 */

/**
 * Implements hook_schema().
 */
function admin_menu_schema() {
  $schema['cache_admin_menu'] = drupal_get_schema_unprocessed('system', 'cache');
  $schema['cache_admin_menu']['description'] = 'Cache table for Administration menu to store client-side caching hashes.';
  return $schema;
}

/**
 * Implements hook_install().
 */
function admin_menu_install() {
  // Increase the module weight, so admin_menu catches any alterations made by
  // other modules in hook_menu_alter().
  db_update('system')
    ->fields(array('weight' => 100))
    ->condition('type', 'module')
    ->condition('name', 'admin_menu')
    ->execute();
}
        

Create an Example Module:

  1. Implement hook_schema() which installs a simple database.
  2. Implement hook_menu() which displays two pages: a form and a page that displays the records.
  3. Implement hook_block_info() and hook_block_view() to display the a certain number of recrods.

Helpful Modules:

Resources: