Less is more: Keeping down the number of modules by extending existing ones

Too often when someone asks for a feature "Can Drupal do xyz"? The answer is "just install the whatever module". While this is the correct answer in 90% of the cases, there are other cases where an additional module is bad thing to have.

We regularly see sites with 120 and more modules, often complaining about poor performance or poor scalability, typical cases of the Open Buffet Binge syndrome. Therefore, we tend to shy away from the temptation to just install the whatever module on those sites and other large sites we manage.

Recently on a client web site that publishes lots of links to media content, the need became obvious to let site visitors alert the site admin of broken links. The site receives over 800,000 page views per day. In these situations, having less modules is better.

Initially, the contact module was used, and a link to it was put on every node of a particular type.

However, people being who they are, the emails that were sent were often devoid of any context and said things like "this link is broken, please fix". What "this" refers too is anyone's guess.

As the email volume kept growing, the problem became so severe as to warrant an alternative solution.

We thought of our flag content module, or even the abuse module, or some variation of that. We decided that these are overkill, and that we wanted to keep the site snappy.

We went for a surprisingly functional yet simple solution: The web site already had the contact module, so we decided to piggy back on that, but adding an argument in the URL that represents the node ID, and then using that to add the info needed to the email message.

Only the following two functions were needed:

The first is to add the node ID to a link that can be displayed in the content, or as a link below the content.

function custom_nodeapi(&$node, $op, $teaser, $page) {
  global $base_url;

  if ($op == 'view' && $node->type == 'book' && $page == TRUE) {
    $node->content['prelude']['#weight'] = -5;
    $node->content['prelude']['#value'] = <<<EOT
<a target="_blank" rel="external nofollow" href="/contact/$node->nid">Report Broken Link</a>

Then, we populate the email body with a link to the said node, and warn the user not to modify that part.

function custom_form_contact_mail_page_alter(&$form, $form_state) {
  global $base_url;

  $nid = arg(1);
  if (!$nid) {
    // The second argument is not specified, do nothing

  if (!is_numeric($nid)) {
    // The node ID is not numeric, do nothing

  // Remove the contact message that the admin sets
  // Remove the "send me a copy" checkbox
  // Standard subject
  $form['subject']['#value'] = 'WebSiteName: Broken link';
  // Include the URL in the message
  $form['message'] = array(
    '#type'  => 'textarea',
    '#title' => 'Message',
    '#default_value' =>
      "\n\n\n" .
      'Do not delete anything below this line:' .
      "\n===========\n" .
      'Broken link for article: ' . "$base_url/node/$nid",

That is it! Since the custom module was already on the site, we did not increase the number of files to be loaded by adding an extra module.

Works like a charm ...



code snippets where?

What an excellent idea - and surely a worthwhile addition to drupal 7 core! I would like to use it but am unsure what files the 2 function code snippets above are added to please advise - thanks


I disagree it should go in core. Core should be extensible and provide building blocks, but not the kitchen sink. It is up to people to extend it any way they wish. If every snippet ends up in core, we will soon be as bloated as anything else out there.

To answer your question, the above two functions go into a file called custom.module in the sites/all/modules/custom directory. In the same directory, create a file called custom.info, and put this in it:

name = Custom
description = Customizations for this site
core = 6.x

That is it.

Normally, every site I manage has a custom module for these type of snippets that don't warrant a module on their own.