API Connect

 View Only

IBM API Connect: Upgrading your Custom Modules to be Drupal 10 and PHP 8.1 compliant.

By Reece Oliver posted Tue March 07, 2023 12:00 PM

  

Introduction

As announced in two previous blog posts, you should know that the upcoming v10.0.5 release is moving to Drupal 10 and PHP 8.1. If you haven’t read those blogs yet, it is highly recommend in doing so before continuing with this one.

IBM API Connect Developer Portal and Drupal 10: Get Set

IBM API Connect: Prepare your Developer Portal Upgrade to Drupal 10

Disclaimer: Although I will be showing you certain aspects that need updating, this does not mean this encompasses everything. Between Drupal 10 and PHP 8.1 there are a few deprecations so you will need to fully read the lists to see if they apply to your modules. With that being said, what the blog post will cover are the common areas we found that we needed to update for our modules in the Portal subsystem.

PHP 8.1 Deprecations

  • Passing null to non-nullable internal function parameters is deprecated. We had a few scenarios that we had to fix up where we could potentially end up passing null to some php functions, an example of this can be seen below. This is the one we had the most hits with when it came to the PHP deprecations so it may be the same for you.

// Before
$redacted_url = $url;
if (strlen($redacted_url) > 0 && strpos($redacted_url, '/') !== 0) {

// After
$redacted_url = $url ?? '';
if (strlen($redacted_url) > 0 && strpos($redacted_url, '/') !== 0) {

  • Optional parameters specified before required parameters. There were some functions in code that had optional parameters before required ones. In PHP 8.1 these are now required to come after.

  • Improved type safety. In PHP 8.1 there is improved type safety so we found we had a lot of warnings that we needed to correct in our functions and declarations of variables.

Both of these can be addressed as seen in the example below

// Before
protected $id;

public function oldFunction($url = '', $data) {
  return $id;
}

// After

/**
 * Drupal entity ID
 *
 * @var string
 */
protected $id;

public function newFunction(someObject $data, string $url = ''): string {
  return $id;
}

Drupal 10 Deprecations

  • Entity queries require an accessCheck before being executed. This is one of the most prolific deprecations we had to update with our Drupal modules and you will more than likely need too if you have custom modules dealing with entities - https://www.drupal.org/node/3201242

// Before
$query = \Drupal::entityQuery('node');
$query->condition('type', 'api');
$query->condition('status', 1);
$nids = $query->execute();

// After
$query = \Drupal::entityQuery('node');
$query->condition('type', 'api');
$query->condition('status', 1);
$nids = $query->accessCheck()->execute();

// Before

// ibm_apim.libraries.yml 
validate_password:
  version: 1.x

// validate_password.js
(function($, Drupal, drupalSettings) {
  var $passwordInput = $(context).find('input.js-password-field').once('ibmApimValidatePassword');
})(jQuery, Drupal, drupalSettings);


// After

// ibm_apim.libraries.yml 
validate_password:
  version: 1.x
  dependencies:
    - core/once

// validate_password.js
(function($, Drupal, drupalSettings, once) {
  var $passwordInput = $(once('ibmApimValidatePassword', context)).find('input.js-password-field');
})(jQuery, Drupal, drupalSettings, once);

  • app.root and site.path services have been converted to container parameters. This will need to be converted to the new way if you make use of them in your custom modules.

// Before
  \Drupal::service('config.factory')
    ->getEditable('locale.settings')
    ->set('translation.use_source', 'local')
    ->set('translation.path', \Drupal::service('site.path') . '/translations')
    ->save();

// After
  \Drupal::service('config.factory')
    ->getEditable('locale.settings')
    ->set('translation.use_source', 'local')
    ->set('translation.path', \Drupal::getContainer()->getParameter('site.path') . '/translations')
    ->save();

Notable Changes this upcoming update will bring

  • CKEditor4 has been removed and CKEditor5 is now the default text editor.

  • Claro is now the default theme for the admin UI giving it a modern and refreshing look.

  • These modules will be uninstalled due to them not supporting Drupal 10. Please make sure none of your custom modules depend on them as they won’t be there after the upgrade.

    • ckeditor_media_embed

    • console

    • flood_unblock

    • message

    • message_notify

    • message_subscribe

    • video_embed_field

    • video_embed_html5

    • content_browser

    • entity_browser

    • entity_browser_enhanced

    • entity_embed

    • file_browser

    • editor_file

    • color

    • Unlimited_number

    • embed

    • ckeditor

    • quickedit

  • Please also make sure that none of your custom Roles depend on any permissions that these modules create as they will be removed in upgrade and if your Roles depend on them it will break the upgrade process.

  • The Drupal 10 Upgrade also come with a major Symfony upgrade version. Drupal 9 used Symfony 4.4 and Drupal 10 now uses Symfony 6 so if you are using any Symfony APIs please make sure that they have not been deprecated. Symfony provide a guide at https://symfony.com/doc/current/setup/upgrade_major.html

  • The default Drush version is now Drush 11, so if you are writing Drush commands in your custom modules then you must write them the new way and the old Drush 8 way will longer work. Here are a couple of examples on how you can achieve that https://www.specbee.com/blogs/writing-your-own-custom-drush-9-and-10-commands and https://www.drush.org/11.x/commands/


Conclusion

As I mentioned at the start of this blog post this isn’t everything that could possibly need changing, this is just an example of our experience in updating our modules to get it working on Drupal 10. Your experience will more that likely differ to ours but we hope you can take the examples and information that has been provided and appropriately prepare for the upcoming upgrade.

#APIConnect #developerportal #portal #drupal

0 comments
101 views

Permalink