Fixing deprecated php code and services in drupal upgrade.

Upgrading your Drupal site from version 9 to 10 brings exciting new features and security enhancements. However, with every significant update, there's the inevitable encounter with deprecated services and code.Deprecated code represents the single biggest blocker to people updating their sites.The Drupal community provides pretty clear guidelines on what can (and can’t) be deprecated (visit the link here ).

  • In earlier versions of Drupal there was no formal mechanism for updating dependencies or changing core functionality (which made the ecosystem super tricky to manage)
  • Deprecated code is only removed during major version updates (e.g. Drupal 8 => Drupal 9) or (Drupal 9 => Drupal 10).

So, if you’re working on a Drupal 10 upgrade, and you are using anything that is marked @deprecated, then once you’ve completed the Drupal 10 upgrade you will likely have a fatal error on your hands (somewhere in your application) as you attempt to use code that no longer exists.

 

Understanding Deprecation Levels:

Drupal uses different deprecation levels to indicate the severity and time frame for removal:

a. @deprecated:

  • Indicates planned removal in a future major release.
  • These services may still function in Drupal 10, but they are considered outdated and should be addressed in future updates.

b. @trigger_error:

  • Signals immediate deprecation and potential errors in future versions.
  • These services are no longer recommended for use and should be replaced as soon as possible.

c. @deprecated('Drupal 10.0.0'):

  • Specifies the exact Drupal version where the element will be removed.
  • These services will be completely removed in the specified Drupal version and must be replaced before upgrading.
HOW TO FIX DEPRECATED CODE

There are a few steps to take once you find deprecated code:

  1. Review the core documentation on why it was deprecated. Usually you can find a @deprecated tag in the method declaration that either points at one or more new methods and/or a change node on Drupal.org.
  2. Review the change node on Drupal.org (which may or may not be directly linked in the code).
  3. Make your code change(s). Usually it is recommended targeting a particular deprecated instance or a particular deprecation (e.g. EntityManager => EntityTypeManager) and making sure that’s working before moving on to do “all of them.” Depending on your codebase, you could have hundreds or even thousands of deprecations (but frequently there will be multiple instances of the same deprecated code, so you may have a handful of fixes to make in “a lot of places.”)
  4. Thoroughly test / regression test your application with these changes in place
  5. Deploy as necessary.

Example of Deprecated PHP Code in Drupal 10 and its Fix

Deprecated Code:

// Drupal 9 code using deprecated `\Drupal\Core\Render\Renderer` service.

$renderer = \Drupal::service('renderer');
$rendered_content = $renderer->renderRoot($element);

Explanation:

This code snippet uses the \Drupal\Core\Render\Renderer service to render an element. However, this service is deprecated in Drupal 10 and will be removed in future versions.

Fixed Code:

// Drupal 10 code using the recommended `\Drupal\Core\Render\MainContentRenderer` service.

$renderer = \Drupal::service('main_content_renderer');
$rendered_content = $renderer->renderRoot($element);

Explanation:

This code updates the deprecated service with the new recommended replacement, \Drupal\Core\Render\MainContentRenderer. This ensures compatibility with Drupal 10 and future versions.

Another Common Deprecated Code Example in Drupal 10: Using \Drupal\Core\Entity\EntityManager::getStorage()

Deprecated Code:

// Drupal 9 code using the deprecated `\Drupal\Core\Entity\EntityManager::getStorage()` method.

$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$node = $node_storage->load(1);

Explanation:

This code retrieves a node entity using the \Drupal\Core\Entity\EntityManager::getStorage() method. While this method still works in Drupal 10, it is considered deprecated and will be removed in future versions.

Fixed Code:

// Drupal 10 code using the recommended `\Drupal\Core\Entity\EntityTypeManager::getStorage()` method.

$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$node = $node_storage->load(1);

Explanation:

The fixed code replaces the deprecated \Drupal\Core\Entity\EntityManager::getStorage() method with the new recommended approach, \Drupal\Core\Entity\EntityTypeManager::getStorage(). This ensures compatibility with Drupal 10 and future versions.

Additional Resources:

Share on social media

Add new comment