Drupal 7

Drupal 7 global variables vs. Drupal 8 services

Several Drupal 7 global values like global $language and global $user are also now accessed via services in Drupal 8 (and not global variables).

Drupal::languageManager()->getCurrentLanguage() and Drupal::currentUser()

Two ways to get the currentUser data:

1) Procedural code
$user = \Drupal::currentUser();


2) OO code access the @current_user service, via dependency injection (DI)
$service = \Drupal::service('thirstysix.ex');

block.api.php lists in Drupal7?

  • hook_block_configure- Define a configuration form for a block.
  • hook_block_info-  Define all blocks provided by the module.
  • hook_block_info_alter - Change block definition before saving to the database.
  • hook_block_list_alter - Act on blocks prior to rendering.
  • hook_block_save - Save the configuration options from hook_block_configure().
  • hook_block_view - Return a rendered or renderable view of a block.
  • hook_block_view_alter -Perform alterations to the content of a block.

Writing Module.info files Drupal 7?

name = Simple Login
description = Desc Text
core = 7.x

stylesheets[] = style.css
scripts[] = script.js
files[] = tests/example.test
dependencies[] = node
package = Others
php = 5.3
version = "7.x-1.0"     /////- discouraged
configure = admin/config/example
required = TRUE
hidden = TRUE

project - Discouraged, packaging use only
project status - url Only used for custom modules not submitted to drupal.org

Theme.info file structure in Drupal7?

name = Bootstrap
description = Desc Text
core = 7.x

regions[header] = header
regions[content] = content
regions[footer] = footer

stylesheets[all][] = css/style.css
scripts[] = js/common.js

; Media queries for stylesheets[]
stylesheets[(min-width:480px)][] = css/480.css
stylesheets[(min-width:768px)][] = css/768.css
stylesheets[(min-width:1024px)][] = css/1024.css
stylesheets[(min-width:1280px)][] = css/1280.css

How to Update a module's weight?

In drupal, the order in which a module's hooks get called is dependent on the weight of your module in the system table.

1)  we can change the weight with a query
     UPDATE system SET weight = 999 WHERE name = 'Module name'

2) hook_module_implements_alter()

3) code to update
    function modulename_install() {
     db_update('system')->fields(array('weight'=>999))->condition('name', 'modulename', '=')->execute();

4) Changing moduleweight via UI
   => modules_weight & Util - Contrib module

Hook_menu in Drupal7?

Define menu items and page callbacks

function hook_menu() {
  $items['example'] = array(
    'title' => 'Example Page',
    'page callback' => 'example_page',
    'page arguments' => array(1, foo),
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
    'weight' => 1,
  return $items;


Drupal7 Menu Item Types?

MENU_CALLBACK  -- A hidden, internal callback, typically used for API calls.
MENU_NORMAL_ITEM -- A "normal" menu item that's shown in menu and breadcrumbs.

MENU_DEFAULT_LOCAL_TASK -- The "default" local task, which is initially active.
MENU_LOCAL_TASK -- A task specific to the parent item, usually rendered as a tab.

MENU_LOCAL_ACTION -- An action specific to the parent, usually rendered as a link.
MENU_SUGGESTED_ITEM -- A normal menu item, hidden until enabled by an administrator.

Basic Hooks: Frequently, Hooks uses in Drupal7?

1) hook_form_alter(&$form, &$form_state, $form_id)
    - Perform alterations before a form is rendered.

2) hook_form_FORM_ID_alter(&$form, &$form_state, $form_id)
    -  Provide a form-specific alteration instead of the global hook_form_alter().
3) hook_form_BASE_FORM_ID_alter(&$form, &$form_state, $form_id)
    -  Provide a form-specific alteration for shared ('base') forms.
4) hook_node_insert($node)  
    - Respond to creation of a new node.

Adds a new field to a table in Drupal7

db_add_field($table, $field, $spec, $keys_new = array())

function my_module_schema_alter(&$schema) {
  $schema['existing_table']['fields']['new_field'] = array(
    'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'default' => 0,
    'description' => 'Field added by my_module',

function my_module_install() {
  $schema = drupal_get_schema('existing_table');
  db_add_field('existing_table', 'new_field', $schema['fields']['new_field']);