Coding Standards in Drupal

Coding standards are a set of rules for programmers that lay out best practices, formatting, and various other rules. It tells us how to format what we write, so that everyone is using the same conventions and has the same expectations when they go to look at a new piece of code. This series will:

  • Explain what standards are and why you should use them
  • Show where the official documentation is and how to use it
  • Walk through the most important formatting rules, including for object-oriented code, Twig templating, and many Drupal-specific conventions
  • How to implement Drupal code standards in your tools and projects

This series was originally written by Alanna Burke at Chromatic, and has been generously released under a Creative Commons Attribution-ShareAlike 4.0 International License by them for us to share here, with some modifications and updates. Chromatic is a well-recognized agency in the Drupal space, providing design, development and deployment services to some of the biggest brands in publishing. Chromatic has 10+ years of experience working on large, enterprise Drupal sites. This series is a testament not only to Chromatic's commitment to building Drupal sites the right way, but sharing that information with the open source community.

Chromatic logo

Tutorials in this course
Drupal 7, 8, 9, and 10
More information

Before you dive into using coding standards it makes sense to understand exactly what they are and why they’re important, generally and specifically in the Drupal community. In this tutorial we will look at:

  • What coding standards are
  • Why following coding standards is a good idea
  • Where coding standards come from

By the end of this tutorial you'll have a better understanding of what coding standards are, and why you should be adhering to them when you write code.

Drupal 7, 8, 9, and 10
More information

Formatting standards cover things like the use of whitespace, how to format control structures, and other aspects that affect your code's appearance and format.

In this tutorial we’ll talk specifically about standards regarding formatting. This is by no means an exhaustive list of PHP syntax rules, but rather is focused on formatting standards for Drupal.

By the end of this tutorial you'll know about the most common Drupal code formatting standards as well as where to find more information when questions arise.

Drupal 7, 8, 9, and 10
More information

Standardized documentation is crucial to a project, whether it is just you or an entire team working on it. In this tutorial we're going to look at:

  • Standards for @docblock comments
  • Standards for inline comments
  • Why standards for documentation and comments are as important as standards for the rest of your code.

By the end of this tutorial you'll know how to add inline documentation for all the PHP code that you write for Drupal.

More information

Drupal core uses object-oriented programming (OOP). This method of programming introduces new coding standards to the project. In this tutorial we'll cover the Drupal coding standards that relate to object-oriented PHP code.

By the end of this tutorial you should know how to implement the Drupal coding standards related to OOP, and where to find more information when you've got questions about the standards.

More information

Translations have their own special functions in both Drupal 7 and 8, and there are some rules for standardizing how they are used that make things clearer for everyone.

In this tutorial we'll look at:

  • When to use, and when not to use, translation utilities to output translatable strings
  • How placeholders work in translatable strings
  • Tips for creating links inside of translatable strings

By the end of this tutorial you should know when, and how, to make strings in your code translatable using Drupal's translation utility functions.

Categories
Drupal 8, 9, and 10
More information

Twig is the theme template engine in Drupal as of version 8. Like any code, there are guidelines and standards that dictate both the style and the structure of the code. In this tutorial we will explain how to adhere to the Drupal code standards while implementing Twig templating.

By the end of this tutorial you will be able to adhere to Drupal's coding standards when writing Twig, and know where to find more information about the guidelines when necessary.

Drupal 7, 8, 9, and 10
More information

Once you know what code standards are and why you should use them, you need to learn how to implement Drupal coding standards in your projects. This tutorial will walk through some of the steps you can take to make this as easy as possible. We'll cover:

  • Configuring your editor or IDE to warn you of coding standards violations
  • Setting up the Coder module and phpcs to scan and review your code
  • Performing team code reviews

By the end of this tutorial you should be able to configure your development environment and implement processes in your workflow that help to ensure your code meets Drupal's coding standards guidelines.