Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Prepare your modules for Drupal 9

Prepare your modules for Drupal 9

https://cluj2019.drupaldays.org/prepare-your-modules-for-drupal-9

In this short talk, I will discuss the best practices on developing modules for Drupal 8 with an eye on having them compatible with Drupal 9.

For those who don't know yet, Drupal 9 is just around the corner. After a brief discussion of what will change in Drupal when that new version is released, I will show some tools can already perform a health check in your current code.

Finally, we will conclude by discussing the trends in Drupal 8 development that indicate already which type of functions will eventually be deprecated, and how that can be leveraged to minimize future workloads.

https://www.youtube.com/watch?v=22V4Kz_IYsU&ab_channel=DrupalDevDays

João Ventura

June 12, 2019
Tweet

More Decks by João Ventura

Other Decks in Programming

Transcript

  1. João Ventura Account in d.o for 12 years and 3

    months. Maintainer of some contrib modules (and themes) Co-organizer of: Drupal Dev Days Lisbon 2018 Drupal Europe 2018 Senior Developer at 1xINTERNET in Frankfurt (mostly remote from nearby Darmstadt) And yes, we’re hiring :)
  2. “The big deal about Drupal 9 is … that it

    should not be a big deal.” Dries Buytaert, https://dri.es/plan-for-drupal-9
  3. Drupal 9 What is it? Simply put, it will be

    Drupal 8.9: - all the deprecated APIs. + new versions of 3rd party libraries, like Symfony 4 or 5. Will be released in June 2020. Go see Gábor Hojtsy’s “State of Drupal 9” session yesterday.
  4. Drupal 7 end of life You have 2 years, 4

    months and 20 days Image adapted from Dries blog post at https://dri.es/drupal-7-8-and-9
  5. “A journey of a thousand miles begins with a single

    step.” Laozi, Chapter 64 of the Tao Te Ching
  6. Drupal 7 to 8 migrations D7 core (and friends) to

    D8 core It works! But... Ongoing work in meta issues: https://dgo.to/2456259 (non-i18n) https://dgo.to/2208401 (i18n) Multilingual entity_translation since 8/2018 Views are NOT migrated. They must be re-created in D8.
  7. https://www.drupal.org/project/drupalmoduleupgrader DMU tries to convert your D7 code to D8,

    based on common code conversion patterns. Creates routing.yml out of hook_menu(), module.info.yml out of module.info, src/Form/FormName.php out of configuration forms, etc. Run it: drush dmu-upgrade old_drupal7_module Not a magic wand! You’ll still have to do most of the work. Drupal 7 to 8 migrations Module not (yet) ported? Drupal module upgrader (DMU)
  8. Search https://git.drupalcode.org/project/MODULE/ for migrations/d7_* files (or migration_templates) If files, are

    there, the module will provide some level of migration. If not, that functionality from your old site could in principle, be in the new site, but all your related current content/config may not go over to the new site, and will have to be re-created. Famous examples: metatag, webform, rules, captcha… (yes, and views) Solution? 1. Look in the issue queue. If not existing, create issue. 2. Work on it until RTBC. Drupal 7 to 8 migrations Module ported, but no d7->d8 migration Not even in the issue queue?
  9. Drupal 7 to 8 migrations Pathauto example id: d7_pathauto_settings label:

    Pathauto configuration migration_tags: - Drupal 7 - Configuration source: plugin: variable variables: - pathauto_punctuation_ampersand - pathauto_punctuation_asterisk - pathauto_punctuation_at ... source_module: pathauto process: punctuation/ampersand: pathauto_punctuation_ampersand punctuation/asterisk: pathauto_punctuation_asterisk punctuation/at: pathauto_punctuation_at ... destination: plugin: config config_name: pathauto.settings id: d7_pathauto_patterns label: Pathauto patterns migration_tags: - Drupal 7 - Configuration source: plugin: pathauto_pattern constants: status: true ... process: status: constants/status id: id ... destination: plugin: 'entity:pathauto_pattern' migration_dependencies: optional: - d7_node_type
  10. Drupal 7 to 8 migrations Useful modules In contrib: Migrate

    - base ELT system Migrate Drupal - base D6/7->D8 system Migrate Drupal UI - If wanted Migrate Drupal Multilingual - i18n support Migrate Plus - extra useful plugins Migrate Tools - migrate drush Migrate Upgrade - migrate drupal drush Migrate Devel - debug tools In core: drush si minimal drush pm:enable block_content config bootstrap content_translation migrate_drupal_ui migrate_upgrade ... drush then seven,bootstrap; drush cset system.theme default bootstrap; drush cset system.theme admin seven drush -y migrate:upgrade --legacy-db-key=drupal_7 --legacy-root=/var/www/drupal_7
  11. The best way to identify code deprecation at the moment

    is to use Upgrade Status if you want a UI, or drupal-check if using the command line. Get drupal-check here: https://github.com/mglaman/drupal-check Run it: drupal-check web/modules/contrib/MODULE It is a static analyser, which means you don’t need a running Drupal system to use it. Caveat: unlike PHP, we haven’t annotated Drupal core code with start of availability of APIs, so check if used APIs are available in latest supported D8 branch (currently 8.6) if creating contrib. Drupal 8 deprecated code (and how to find it)
  12. We’re mostly getting rid of code that has been replaced

    by a better implementation. A lot of it is functional code. See deprecate meta issues for some examples of ongoing work: https://dgo.to/2999721 - Deprecate the legacy include files You should avoid calls to functions like file_managed_copy (already deprecated) or drupal_get_path (not yet). In alternative, try to open their implementation and use the replacement methods. Drupal 8 deprecated code (and how to guess what it will be)
  13. A call to file_managed_copy('public://example.png', 'public://example1.png') can easily be replaced by

    \Drupal::service('file_system')->copy('public://example.png','public://example1.png') Why: function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) { $file_system = \Drupal::service('file_system'); if (!isset($destination)) { … } return $file_system->copy($source, $destination, $replace); } catch (FileException $e) { return FALSE; } } Drupal 8 deprecated code (and how to get rid of it)
  14. A call to drupal_get_path('module', 'token') can easily be replaced by

    dirname(\Drupal::service("extension.list.module")->getPathname('token')) Why: function drupal_get_path($type, $name) { return dirname(drupal_get_filename($type, $name)); } function drupal_get_filename($type, $name, $filename = NULL) { if ($type === 'core') {return 'core/core.info.yml';} try { $extension_list = \Drupal::service("extension.list.{$type}"); if (isset($filename)) { $extension_list->setPathname($name, $filename); } return $extension_list->getPathname($name); } catch (ServiceNotFoundException $e) { … } catch (\InvalidArgumentException $e) { … } } Drupal 8 deprecated code (and how to get rid of it)
  15. Drupal 9 will ship with at least Symfony 4, but

    since Symfony 5.0 will be released in November 2019, we may jump from 3 to 5 directly. There is already some support for running Drupal 8 with Symfony 4 (and 5). See: https://dgo.to/2976394 - Allow Symfony 4 to be installed in Drupal 8 https://dgo.to/3055180 - Make Drupal 8 work with Symfony 5 (along with Symfony 3 and 4) You can have an early preview of how your site will behave in Drupal 9 by also applying the above patches. Or just wait until the Drupal 9.x branch starts to be used. This will probably happen late this year. Symfony 4 (and how to play with it)
  16. JOIN US FOR CONTRIBUTION Drupal Dev Days Cluj 2019 Drupal

    9 @drupaldevdays Migration D7 - D8 Contrib Migration
  17. WHAT DID YOU THINK? Locate this session at the Drupal

    Developer Days Transylvania 2019 website: https://cluj2019.drupaldays.org/schedule Take the survey! https://www.surveymonkey.com/r/drupaltransylvania THANK YOU!