[Drupal] Migrating Drupal 7 modules to Drupal 8 using Drupal module upgrader
After the launch of Drupal 8, there is now an incessant need for converting existing Drupal 7 modules to Drupal 8 compatible versions. Due to the substantial changes in Drupal 8 from its previous versions, developers find it hard and time consuming to migrate from D7 to D8. This is mainly because Drupal 8 is object oriented and much closer to an MVC (Model View Controller) architecture, Drupal is no longer a PAC (Presentation Abstraction Control) architecture framework, Drupal follows the PSR-4 folder structure in compatible with symfony component which Drupal 8 uses.
A lot of this difficulty can actually be overcome by a module called Drupal module upgrader. This module automates the migrate process of Drupal 7 modules to Drupal 8. This article elaborates on how this is accomplished and troubleshooting the Drupal module upgrader(dmu) module. DMU is still in its development stages but it does a pretty good job upgrading completely simple modules.
This module requires that you have Drush installed. This itself is quite a confusing process as Drush 7 is the only compatible Drush version with Drupal 8 and to install Drush 7, composer is required. So for the purposes of this article, I assume, Drush 7 is installed in the system. If it isn't please do : Installing drush.
Installing the module
The module can be installed like in Drupal 7 itself. Download and extract into the modules folder. In Drupal 8 the modules folder is not in sites/all/ but in $lt;Drupal root>/modules/ folder.
There is a simpler method to download and enable the module since there is composer involved. Composer is an application-level dependency manager for the PHP programming language that provides a standard format for managing dependencies of PHP software and required libraries. DMU uses composer to install and update the module.
- Go to the command line in your Linux system and navigate to the Drupal installation folder. Type "drush status" to see whether both installation and/or Drush 7 is working. If it is working, type "drush dl drupalmoduleupgrader --select --all". This will list out all the available versions of Drupalmoduleupgrader as you can see in the image. Choose the supported one as the development one will have lots of bugs. This will download the module in the modules folder.
- Before enabling the module, first navigate into the Drupalmoduleupgrader folder in your command line and type "composer install". This will install dmu and download all of its components as you can see in the image. Here, many of you may get a similar error message like in the yellow highlighted area in the image. Not to worry, this may only be a problem later which I will explain then.
- After this, return to the Drupal root on command line and enable the module by typing "drush en drupalmoduleupgrader". You will get a message saying module enabled.
Using the module
- DMU provides some commands to analyze and upgrade the modules. For the purpose of this example, we will take the CCK(Content Construction Kit) module and see if we can upgrade it. If the Drupal 7 module is kept in the module folder then type "drush dmu-analyze cck", If it is kept in a sub folder then type "drush dmu-analyze module --path=<tDrupal/modules/path/to/module>". Type "drush help dmu-analyze" for more details. On successfull execution, a detailed description of the changes to be made with links to their changes will be saved as a html file in the root of the module.
- To upgrade the module, we must run the command "drush dmu-upgrade cck" or "drush dmu-upgrade module --path=<Drupal/modules/path/to/module>". Some of you will get errors like the one shown in the image. This is because of the earlier errors that we ignored. To rectify this, we must navigate once more into the Drupalmoduleupgrader folder on our command line and type "composer update". This will solve the problem. Still if the error persists or the download of filesystem is getting timed out or failed, then you must go to the link here and download zip. Extract the same into the drupalmoduleupgrader/vendor/symfony/filesystem/component/ folder and rename it to Filesystem.
- Run the upgrade commands again. This will show indexing...done. Now if you look into the cck folder, you will see the yml info file and all the Drupal 8 necessary files.
- Run "drush dmu-clean cck" to clean the module of all Drupal 7 files and go to extend in your Drupal site and you will see that the cck module has appeared in the list of modules. Enable and use it.
Workarounds to common errors
CCK is a simple module and therefore indexing it was easy and uncomplicated. There are a few workarounds to make indexing simpler for large modules.
- Use the --only option in the dmu-analyze or dmu-upgrade command. Example, "drush dmu-upgrade cck --only=<HookMenu>"
- Use the --path option to analyze or upgrade only one module in a project. Example, in the coder package there are three modules 'coder_sniffer', 'coder_review', 'coder_upgrade'. Here, we need only scan one of these modules with the command "drush dmu-analyze module --path=<Drupal/modules/coder/coder_review>".
- Even after the dmu-upgrade has executed successfully, enabling your module can still throw off errors and/or give you a white screen death. This is mainly because the program while executing, skips call by references and minute changes like that. If you read your code after dmu-upgrade is done, you will see comment tags @FIXME. This is to indicate where the changes have to manually be done. Almost every change will be mentioned throughout the code and links to the corresponding change records are present in the html file generated by dmu-analyze.
- Only run dmu-clean after the code is bug free and perfectly functional. Otherwise old code will be removed which had to be changed manually.
- You can use the --backup option to create a backup of the module before upgrading or cleaning.
The Drupal Module Upgrader (DMU) module despite being in the development stages, saves developers a lot of time when upgrading modules to be compatible with Drupal 8. This module is introduced by Acquia, a software-as-a-service company providing products, services, and technical support for open-source web content management platform Drupal much like Zyxware technologies, who is a partner to Acquia.