enterprise joomla! and drupal part 3 - coding & customization

(This is part 3 of a comparison of Joomla! and Drupal in the enterprise. See parts 1 and 2 of Joomla! and Drupal in the Enterprise.)

Drupal and Joomla! benefit from sizable and energetic communities with high coding standards, and both platforms have clear and well-documented guidelines for extending the functionality of the core software.

Joomla! has guided those who customize and extend the core product to employ the model-view-controller (MVC) design pattern. MVC is a widely-known object-oriented programming pattern and it provides developers with a framework for separating data access from business logic and data presentation.

Drupal does not employ object-oriented programming in the strict sense. Instead, developers can add their own functionality via a set of hooks that are available via the Drupal API.

This article describes the consequences of these divergent development strategies for those attempting to customize and extend Joomla! and Drupal.

Joomla!’s MVC

Joomla!’s (MVC) framework (described here), provides a template for adding behaviors and functionality to the system in a consistent manner. Using the Joomla! framework, developers can apply their own business logic to Joomla! data, re-arrange the way items appear on a page, and perform other customization tasks.

To implement custom functionality in this framework, a developer essentially extends the three main Joomla! classes that correspond to the three parts of the pattern. These classes are JModel, JView, and JController. In their own sub-classes, developers can add variables and logic as needed.

The advantages of this to developers include:

  • Customizations can happen the same way each time, by creating classes that extend the standard Joomla! MVC classes
  • Presentation logic is separate from data access
  • Team members working on a component can concentrate on discrete classes (objects)
The MVC pattern can also result in the need for writing a lot of code, as we’ll see after briefly discussing Drupal’s customization paths.

Drupal Hooks

As noted above, Drupal is not strictly object-oriented software. It does contain some features of object-oriented programming (read more about object-oriented Drupal here ), but remains event-driven or procedural software.

So instead of extending objects, Drupal provides developers with a variety of hooks on which to hang code and add to or modify Drupal core functionality. Developers can write custom modules that need only contain the customized logic. These custom functions, if named appropriately, will execute when called by the core software.

Dozens of hooks allow for a wide variety of customizations, and Drupal 7 features even more places for developers to hang their hats.

There is a move, in Drupal 7, toward OOP support and given PHP 5’s more mature implementation of OOP, some developers are writing modules as classes. Nevertheless, Drupal’s hook system has a lot of support and provides an effective method for working within the existing framework.

One drawback to the hook system is timing. If two modules operate on a piece of content, the order in which their hooks are called is a function of the physical order of the code. An examination of that code, and testing, are required to ensure that the desired effect is achieved.

One important note is that Drupal’s event-driven software mirrors the way the web works. In the web world, a browser makes an HTTP request, which is really an event, and the computer designated by the domain name responds to that request in a certain way. No bootstrap is required, in Drupal’s case, to organize objects and make it seem as if the HTTP request was an object request or the beginning of an object-oriented set of circumstances.

Simple Head-to-Head Customization

Joomla! provides a highly-structured and object-oriented way to extend the software while Drupal allows customizers to insert custom code via hooks into the core software processes. Which one is easier? Arguments can be made for both, certainly, but it’s worth looking at what it takes to perform a simple extension in each system.

To make Joomla! output the phrase “Hello World!”, a developer needs to produce, at minimum, 5 files: a view, a controller, a template, an “entry” file, and an installer. The procedure for coding these files is detailed in this tutorial. Here’s the code for the entry file, which organizes all the component’s moving parts:


/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * components/com_hello/hello.php
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Pa...
 * @license    GNU/GPL
*/
 
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
 
// Require the base controller
 
require_once( JPATH_COMPONENT.DS.'controller.php' );
 
// Require specific controller if requested
if($controller = JRequest::getWord('controller')) {
    $path = JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php';
    if (file_exists($path)) {
        require_once $path;
    } else {
        $controller = '';
    }
}
 
// Create the controller
$classname    = 'HelloController'.$controller;
$controller   = new $classname( );
 
// Perform the Request task
$controller->execute( JRequest::getWord( 'task' ) );
 
// Redirect if set by the controller
$controller->redirect();

Combining this with the code for the view, the controller, and the template (the code is omitted here but available in the tutorial), reveals that it takes an awful lot to do a little. Adhering to MVC has its price.

Now turn to the Drupal "Hello, World!" implementation, which requires two files. The first is an info file for the module and here it is:


name = HelloWorld
description = "Drupal Demo"
version = 6.x-1.0
core = 6.x  

Next is the module itself:

// $Id: helloworld.module,v 1.0 2010/06/25 08:48:00 AchieveInternet Exp $
 
/**
 * @file
 * Demo a simple Hello World app for Drupal
 */

/**
 * Implementation of hook_menu().
 */
function helloworld_menu() {
  $items = array();
  
  $items['hello/helloworld'] = array(
    'title' => 'Download Stats',
    'page callback' => 'helloworld_callback',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  
  return $items;
}

/**
 * Print Hello World
 * 
 * @param $name
 *  Name of person to greet (if provided)
 */
function helloworld_callback($name = NULL) {
  if (!empty($name)) {
    return check_plain("Hello, $name!");
  }
  else {
    print "Hello, World!";
  }
}

The Drupal module uses hook_menu, which allows a developer to intercept the Drupal responses to URLs. Instead of creating new objects and organizing them, as in Joomla!, the Drupal developer can surgically insert small amount of code to perform the required task.

To be sure, "Hello, World!" isn’t an enterprise-grade component, but the contrast between what’s required to extend each system is significant.

Extending Joomla! and Drupal

For those development teams who prefer to work in object-oriented environments, Joomla! adheres more strictly to OOP standards. However, Drupal’s hook system provides a robust way to work with the core software. The ability to accomplish a lot with so little code converts many Drupal skeptics.

One of the stated goals of Joomla! 1.6 is to fully standardize the way extensions are built in the MVC framework. This will make it easy for developers who are familiar with the framework to build and examine extensions and make customizations.

Drupal 7 will include more classes than Drupal 6 did, and the core product will use classes when it makes sense, especially when it can help separate types of logic (i.e. database access, presentation) into different classes. However, the benefits of the hook system will persist as the software moves forward.

_________________

Achieve Internet, www.achieveinternet.com, specializes in enterprise-class Drupal development with unique capabilities in project architecture and planning for Drupal projects requiring scalability, stability and integration.