Theming
Topic

Update Custom Code for Drupal for Drupal 8, 9, and 10

Starting with Drupal 8, Drupal was been refactored to use an object-oriented architecture and has adopted Symfony components as 3rd-party dependencies, along with many other changes.

Drupal 7 module and theme developers setting out to update their code to the latest version of Drupal can benefit a lot from mapping what they already know about Drupal 7 to the way that they accomplish the same thing in the latest version of Drupal. This generally involves picking a Drupal 7 pattern and then browsing through change records, and using tools like Drupal Module Upgrader to help identify the modern Drupal equivalent.

We also recommend taking the time to familiarize yourself with Drupal features like events, plugins, and services — at least enough to recognize which pattern is appropriate for different types of problems. This will help you spot instances where there either isn’t a direct upgrade path, or where Drupal might provide an updated way of accomplishing something.

Example tasks

  • Update a custom Drupal 7 module so that it works with the latest version of Drupal.
  • Convert a PHPTemplate (.tpl.php) file to Twig (.html.twig).
  • Test your site's modules for readiness with the current version of Drupal.

Confidence

Beware of any tutorials or documentation showing before/after comparisons with Drupal 7 code from before Drupal 8 was released. Drupal APIs continued to change up until it was released, though many of these older tutorials are still accurate. We recommend spending some time learning current best practices so that when you encounter specific solutions you’ll have a better chance to correctly gauge their accuracy. Some resources for upgrading to Drupal 8 may not be complete for Drupal 9. Check the respective issue queues of any tool to see its status.

Drupalize.Me resources

Posted on Monday, May 4, 2020 - 12:47 by joe

Upgrade status generates a report that serves as a sort of checklist to help you determine whether or not your site is ready to be upgraded to Drupal 9. It packs a lot of useful information into a single report. It's worth taking the time to install it on a copy of your Drupal 8 site and seeing what it has to say.

Posted on Monday, April 20, 2020 - 13:36 by joe

Drupal check, and Drupal rector, are two useful command line tools you can use to help jump start the process of updating your Drupal 8 code to ensure it's compatible with Drupal 9. This post includes some notes about the process I went through while testing them out on some of the Drupalize.Me code base.

More information

Upgrading is the process of moving your site from a previous major version of Drupal to a newer version, for example from Drupal 7 to Drupal 11.

Categories
Drupal 8, 9, 10, and 11
More information

Deprecated code is any code flagged for future removal, but which has not yet been removed to ensure that existing code which relies on the to-be-removed code will continue to work. When an API, or feature, becomes deprecated it's a warning to all developers that at some point in the future this thing they are relying on will not exist, and they'll need to update their custom code to a new approach. Preferably before the existing one gets removed.

In this tutorial well:

  • Learn about why, and when, code is deprecated in Drupal
  • How to identify custom code that is using deprecated features
  • How to determine an appropriate upgrade path to the new API

By the end of this tutorial you should be able to explain what code deprecation is, and why it's important for Drupal's continue innovation, as well as how to update your own code when an API you rely on becomes deprecated.

Drupal 8, 9, 10, and 11
More information

Object-oriented PHP utilizes classes and objects to organize code into reusable chunks. This approach helps us organize complex applications, such as Drupal, into modular code called classes that can be reused across the entire system.

Categories
Module Development, Backend and Infrastructure
Drupal 8, 9, 10, and 11
Categories
Theming
Drupal 8, 9, and 10
Drupal 8, 9, and 10
More information

Twig is a template engine for PHP, which is the language used in Drupal. Twig can be used to design templates in generic PHP applications as well as Drupal themes.

Drupal 7, 8, 9, and 10
More information

Coding standards provide a set of rules for how your code should be formatted, and best practice guidelines for naming conventions and the location of files.

Guides

Not sure where to start? Our guides provide useful learning tracks for all skill levels.

Navigate guides

External resources

  • Change Records (Drupal.org)
    • If you’ve got questions about what happened to a specific hook, function, or way of doing things search through the change records for an answer. Most change records show before and after examples of specific code snippets. Make sure to also review those that are still in a draft state.
  • Drupal Module Upgrader (drupal.org)
    • This module works in combination with Drush, and Composer in order to scan your Drupal 7 code and make suggestions on how to upgrade it.
  • Drupal Twig Conversion Instructions (.tpl.php to .html.twig) (Drupal.org)
    • Step-by-step instructions that were used for converting core functions, which can also be used by anyone who needs to covert to Twig.
  • Upgrading and converting Drupal 7 modules (Drupal.org)
    • This guide contains a number of different walk-throughs showing how to convert various elements in a Drupal 7 module. While you can’t always follow it step-by-step and be successful we’ve found it to be a useful place to start tracking down what is necessary to incorporate some of the bigger and more conceptual changes.
  • Upgrading Drupal (Drupal.org)
    • Learn how to upgrade a site to the next major version.