home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (common_test.module)

This example Drupal source code file (common_test.module) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

access, arg2, arg3, arguments, array, content, drupal, function, if, page, php, theme, title, type

The common_test.module Drupal example source code

<?php
// $Id: common_test.module,v 1.13 2010/08/17 21:31:13 dries Exp $

/**
 * @file
 * Helper module for the Common tests.
 */

/**
 * Implements hook_menu().
 */
function common_test_menu() {
  $items['common-test/drupal_goto'] = array(
    'title' => 'Drupal Goto',
    'page callback' => 'common_test_drupal_goto_land',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['common-test/drupal_goto/fail'] = array(
    'title' => 'Drupal Goto',
    'page callback' => 'common_test_drupal_goto_land_fail',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['common-test/drupal_goto/redirect'] = array(
    'title' => 'Drupal Goto',
    'page callback' => 'common_test_drupal_goto_redirect',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['common-test/drupal_goto/redirect_advanced'] = array(
    'title' => 'Drupal Goto',
    'page callback' => 'common_test_drupal_goto_redirect_advanced',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['common-test/drupal_goto/redirect_fail'] = array(
    'title' => 'Drupal Goto Failure',
    'page callback' => 'drupal_goto',
    'page arguments' => array('common-test/drupal_goto/fail'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['common-test/destination'] = array(
    'title' => 'Drupal Get Destination',
    'page callback' => 'common_test_destination',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  $items['common-test/query-string'] = array(
    'title' => 'Test querystring',
    'page callback' => 'common_test_js_and_css_querystring',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Redirect using drupal_goto().
 */
function common_test_drupal_goto_redirect() {
  drupal_goto('common-test/drupal_goto');
}

/**
 * Redirect using drupal_goto().
 */
function common_test_drupal_goto_redirect_advanced() {
  drupal_goto('common-test/drupal_goto', array('query' => array('foo' => '123')), 301);
}

/**
 * Landing page for drupal_goto().
 */
function common_test_drupal_goto_land() {
  print "drupal_goto";
}

/**
 * Fail landing page for drupal_goto().
 */
function common_test_drupal_goto_land_fail() {
  print "drupal_goto_fail";
}

/**
 * Implements hook_drupal_goto_alter().
 */
function common_test_drupal_goto_alter(&$path, &$options, &$http_response_code) {
  if ($path == 'common-test/drupal_goto/fail') {
    $path = 'common-test/drupal_goto/redirect';
  }
}

/**
 * Print destination query parameter.
 */
function common_test_destination() {
  $destination = drupal_get_destination();
  print "The destination: " . check_plain($destination['destination']);
}

/**
 * Implements hook_TYPE_alter().
 */
function common_test_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  // Alter first argument.
  if (is_array($data)) {
    $data['foo'] = 'Drupal';
  }
  elseif (is_object($data)) {
    $data->foo = 'Drupal';
  }
  // Alter second argument, if present.
  if (isset($arg2)) {
    if (is_array($arg2)) {
      $arg2['foo'] = 'Drupal';
    }
    elseif (is_object($arg2)) {
      $arg2->foo = 'Drupal';
    }
  }
  // Try to alter third argument, if present.
  if (isset($arg3)) {
    if (is_array($arg3)) {
      $arg3['foo'] = 'Drupal';
    }
    elseif (is_object($arg3)) {
      $arg3->foo = 'Drupal';
    }
  }
}

/**
 * Implements hook_TYPE_alter() on behalf of Bartik theme.
 *
 * Same as common_test_drupal_alter_alter(), but here, we verify that themes
 * can also alter and come last.
 */
function bartik_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  // Alter first argument.
  if (is_array($data)) {
    $data['foo'] .= ' theme';
  }
  elseif (is_object($data)) {
    $data->foo .= ' theme';
  }
  // Alter second argument, if present.
  if (isset($arg2)) {
    if (is_array($arg2)) {
      $arg2['foo'] .= ' theme';
    }
    elseif (is_object($arg2)) {
      $arg2->foo .= ' theme';
    }
  }
  // Try to alter third argument, if present.
  if (isset($arg3)) {
    if (is_array($arg3)) {
      $arg3['foo'] .= ' theme';
    }
    elseif (is_object($arg3)) {
      $arg3->foo .= ' theme';
    }
  }
}

/**
 * Implements hook_theme().
 */
function common_test_theme() {
  return array(
    'common_test_foo' => array(
      'variables' => array('foo' => 'foo', 'bar' => 'bar'),
    ),
  );
}

/**
 * Theme function for testing drupal_render() theming.
 */
function theme_common_test_foo($variables) {
  return $variables['foo'] . $variables['bar'];
}

/**
 * Implements hook_library_alter().
 */
function common_test_library_alter(&$libraries, $module) {
  if ($module == 'system' && isset($libraries['farbtastic'])) {
    // Change the title of Farbtastic to "Farbtastic: Altered Library".
    $libraries['farbtastic']['title'] = 'Farbtastic: Altered Library';
    // Make Farbtastic depend on jQuery Form to test library dependencies.
    $libraries['farbtastic']['dependencies'][] = array('system', 'form');
  }
}

/**
 * Implements hook_library().
 *
 * Adds Farbtastic in a different version.
 */
function common_test_library() {
  $libraries['farbtastic'] = array(
    'title' => 'Custom Farbtastic Library',
    'website' => 'http://code.google.com/p/farbtastic/',
    'version' => '5.3',
    'js' => array(
      'misc/farbtastic/farbtastic.js' => array(),
    ),
    'css' => array(
      'misc/farbtastic/farbtastic.css' => array(),
    ),
  );
  return $libraries;
}

/**
 * Adds a JavaScript file and a CSS file with a query string appended.
 */
function common_test_js_and_css_querystring() {
   drupal_add_js(drupal_get_path('module', 'node') . '/node.js');
   drupal_add_css(drupal_get_path('module', 'node') . '/node.css');
   // A relative URI may have a query string.
   drupal_add_css('/' . drupal_get_path('module', 'node') . '/node-fake.css?arg1=value1&arg2=value2');
   return '';
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal common_test.module source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.