Image

Drupal Coding Standards

Drupal, the renowned open-source web content management framework, promotes a culture of adhering to specific coding standards. These standards help in maintaining a unified and more readable code base which eventually aids in efficient website project management. So, having a clear understanding of Drupal coding standards is indispensable for any Drupal developer.

To start with, Drupal primarily uses PHP, so one should follow PHP coding standards. According to Drupal, PHP code needs to be written in a way that it meets the standards of the PHP Framework Interoperability Group. Developers are supposed to use an indent of 2 spaces, and not use tab. Have a look at the following PHP code which follows Drupal’s coding standards:

/**
* Implements hook_help().
* 
* @param string $route_name
*   The route the help is for, or 'admin_help' if on the central help page.
* @param RouteMatchInterface $route_match
*   The current route match.
* @return 'help'
*   The local task operation.
* @return Response
*   The HTTP response object.
*/
function hook_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the module overview page
  	case 'help.page.module_overview':
  	  $output = '';
  	  $output .= '<h3>' . t('About') . '</h3>';
  	  $output .= '<p>' . t('Drupal is awesome!') . '</p>';
  	  return $output;
  	default:
  }
}

Following Drupal’s naming conventions is crucial as well. It consists of naming variables, functions, methods, classes, interfaces, and files. For example, use lower-case letters for all variable names, and underscore for the spaces between words. Here’s an example:

$module_name;

Besides, Drupal has its own unique HTML coding standards. For example, every drupal-specific CSS file should start with a brief description of that file. Moreover, all attributes must be enclosed in quotation marks and elements, blocks and modifiers should be hyphen-separated.

The other important part of Drupal’s coding standards is to comply with JavaScript & jQuery coding standards. For example, variables and function names should be camelCased. JavaScript often used in coordination with PHP, should also be indented with two spaces and curly braces are always required for control structures.

function myDrupalFunction() {
  var myVariable = 'Hello, Drupal!';
  alert(myVariable);
}

Be it a seasoned Drupal developer or a newbie, remembering and applying these coding standards is vital for readability, maintainability and scalability of your Drupal project. It might be hard to stick to these rules, but the long-term benefits for the project and the development team are worthwhile. To ensure that these standards are adhered to, using a code sniffer or a linting tool can be quite effective. Happy coding with Drupal!

Adhere, maintain, prosper!

Latest Posts
↑ Top