From b3c4dd21f63250236fa293a76514a11d5ad61cf9 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sat, 20 May 2023 19:57:25 +0100 Subject: [PATCH] daily-email: add 2023-05-17 --- src/content/daily-email/2023-05-17.md | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/content/daily-email/2023-05-17.md diff --git a/src/content/daily-email/2023-05-17.md b/src/content/daily-email/2023-05-17.md new file mode 100644 index 000000000..08b25f590 --- /dev/null +++ b/src/content/daily-email/2023-05-17.md @@ -0,0 +1,34 @@ +--- +title: > + What is deprecated code? +pubDate: 2023-05-17 +permalink: > + archive/2023/05/17/what-is-deprecated-code +tags: + - php + - drupal + - software-development +--- + +Deprecating code is a way of identifying code that will be removed in a future major version. + +For example, the `drupal_set_message()` function was deprecated in Drupal 8.5 and removed in Drupal 9 as the `messenger` service replaced it. + +Once it was deprecated, the function was changed to use the new service to avoid duplicating code and a message was added to notify Developers: + +```php +function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE) { + @trigger_error('drupal_set_message() is deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::addMessage() instead. See https://www.drupal.org/node/2774931', E_USER_DEPRECATED); + + $messenger = \Drupal::messenger(); + if (isset($message)) { + $messenger->addMessage($message, $type, $repeat); + } + + return $messenger->all(); +} +``` + +This approach means that code can be refactored without breaking backwards-compatibility and, to upgrade any custom code to be compatible with Drupal 9, any references to `drupal_set_message()` just needed to be updated to use the new Messenger service. + +No large rewrite needed!