Skip to content

Deprecation Error Solutions

Joel Pittet edited this page Apr 11, 2019 · 11 revisions

Listed here are common deprecation errors and their solutions. Before replacing the deprecated call, confirm that the version of Drupal you are using does in fact have the newer approach available.

IMPORTANT! The examples below are proof of concept. Specific resolution may vary and always use dependency injection where possible.

Call to deprecated function drupal_set_message()

Use the Messenger service instead. See core/includes/bootstrap.inc#L485, https://www.drupal.org/node/2774931, and core/lib/Drupal/Core/Messenger/Messenger.php for details.

Before

drupal_set_message($message, $type, $repeat);

After

\Drupal::messenger()->addMessage($message, $type, $repeat);

Call to deprecated function entity_view()

Use the entity type manager instead. See https://www.drupal.org/node/3033656 for details.

Before

$build = entity_view($node, 'teaser')

After

$builder = \Drupal::entityTypeManager()->getViewBuilder('node');
$build = $builder->view($node, 'teaser');

Call to deprecated function entity_get_display()

Use the entity type manager instead. Refer to https://git.drupalcode.org/project/drupal/blob/8.8.x/core/includes/entity.inc#L443 for details.

Before

entity_get_display($entity_type, $bundle, $view_mode);

After

\Drupal::entityTypeManager()
  ->getStorage('entity_view_display')
  ->load($entity_type . '.' . $bundle . '.' . $view_mode);

Call to deprecated function entity_get_form_display()

Use the entity type manager instead. Refer to https://git.drupalcode.org/project/drupal/blob/8.8.x/core/includes/entity.inc#L523 for details.

Before

entity_get_form_display($entity_type, $bundle, $form_mode);

After

\Drupal::entityTypeManager()
  ->getStorage('entity_form_display')
  ->load($entity_type . '.' . $bundle . '.' . $form_mode);

Call to deprecated method [METHOD NAME] of class Drupal\KernelTests\KernelTestBase

Refer to core/tests/Drupal/KernelTests/AssertLegacyTrait.php for the correct method to use. For example, use assertEquals instead of assertEqual.

Call to method [METHOD NAME] of deprecated class Drupal\Tests\BrowserTestBase

Refer to core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php for the correct method to use. For example, use elementExists instead of assertElementPresent.

Other Deprecations

For other messages, if you are using PHPStorm you can right click on the function call that is causing the error and go to its declaration to see information about why it is deprecated and what the resolution is.