Skip to main content

How to Upgrade Drupal7 Module to Drupal 8/9

Since Drupal8/9 works very much differently than Drupal7, the module structure has changed, the files type supported changed and Drupal API also changed. so with many modules which are available for download, you can find separate versions for Drupal7 and Drupal8/9.
But in the case of a custom module, you would like to upgrade your own module.

Drupal org suggests a guide that provides information and specific steps for upgrading contributed and custom modules from Drupal 7 to the latest version of Drupal. 

Drupal Module Upgrader can do some of this automatically for you, covering many API hooks.

The Drupal Module Upgrader is a Drush command that helps you update your modules from Drupal 7 to Drupal 8.

* didn't work for me, so here are the steps to do it manually.

 

The Module/Theme Upgrade Steps

I decided to follow this guide and add my own remarks. here are the links and my remarks for upgrading the module written for Drupal 7 to fit Drupal 8:

Step 1:Convert mymodule.info to mymodule.info.yml 

Convert the file name and its syntax to YAML syntax.
All of our old .info files have been converted to YAML and the old .info parser has been removed. The new parser is the Symfony YAML component. The new file extension is .info.yml. This applies to modules, themes, and profiles.
I decided not to use the module: drupalmoduleupgrader, and found few issues and errors with it.

  • in case the info file includes the command: configure
    1. create routing file: mymodule.routing.yml
    2. set in it all paths define in mymodule_menu,
      TIP1: since I might create a few custom modules, I'm creating a path to the config group for all my custom modules, like this:
       

      drupalvip.admin_config:
          path: '/admin/config/mygroup'
          defaults:
              _controller: '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage'
              _title: 'DrupalVIP'
          requirements:
              _permission: 'access administration pages'  

      and my custom module settings path, I will put under it, like this:
       

      drupalvip_mail.settings:
          path: '/admin/config/mygroup/mymodule'
          defaults:
              _form: '\Drupal\mymodule\Form\SettingsForm'
              _title: 'DrupalVIP Mail settings'
          requirements:
              _permission: 'access administration pages'
          options:
              _admin_route: TRUE   



       

  1. if file info includes a 'stylesheets' or 'scripts' array you should create a 'libraries' file to define them.
    create file: mymodule.libraries.yml, and put in the module rout
     

    stylesheets:
        css:
            theme:
                css/mail.css: {}
                
    scripts:        
        js:
            js/mail.js: {}

    in the info.yml file add the following lines:
     

    libraries:
        - mymodule/stylesheets
        - mymodule/scripts

     

    Very Important TIP:
    Before starting the installation and debugging the upgraded module, make sure Drush is operational
    you will strongly need it !!
     

Context syntax example between file types: info  &  yml

------------------------------
Drupal 7: mymodule.info
------------------------------
name = My D7 Module
description = This module needs to be ported to D8.
core = 7.x
version = VERSION
package = Custom Modules

configure = admin/config/user-interface/your-module

dependencies[] = ctools
dependencies[] = panels
dependencies[] = views

files[] = lib/FozzieClass.php
files[] = lib/GonzoClass.php
files[] = lib/KermitClass.php

 

------------------------------
Drupal 8: yourmodule.info.yml
------------------------------
name: 'My D7 Module'
type: module
description: 'This module needs to be ported to D8.'
core: 8.x
version: VERSION
package: 'Custom Modules'

configure: my_module.admin

dependencies:
  - drupal:ctools
  - drupal:panels
  - drupal:views

# Files are no longer listed in an .info file.

if you need to change the theme .info, pls view 

.info files are now .info.yml files

 

Step 2: Debugging And Convert the hook_menu mechanism

after making the first changes to the module, upload it, install it and watch the errors, if the website becomes unavailable, don't worry,
with Drush do:

  1. clean cache and check the website:  > drush cr  
  2. uninstall module: > drush pm-uninstall mymodule

 

Converting hook_menu:

  1. create a routing configuration file in the module root
  2. add folders to the module according to drupal9 structure strategy: src/Form, assets
  3. create routing definitions in the mymodule.routing.yml according to your hook_menu definition

    mymodule.settings:
        path: '/admin/config/mygroup/mymodule'
        defaults:
            _form: '\Drupal\mymodule\Form\SettingsForm'
            _title: 'DrupalVIP Mail settings'
        requirements:
            _permission: 'access administration pages'
        options:
            _admin_route: TRUE  

    review the example above, the task items are :
    1. create folder src/Form
    2. move the settings form from folder:includes to folder: src/Form
    3. convert modular coding to object coding, for example the setting form definition

  4. Convert your page callbacks to controllers

  5. Convert your form builders to FormInterface
  6. Read more: Upgrading and converting Drupal 7 modules

 

 

 

New Form coding

<?php
namespace Drupal\drupalvip_sentry\Form;



/**
 * Configure settings for this module.
 */
class SettingsForm extends ConfigFormBase {

    /**
     * {@inheritdoc}
     */
    public function getFormId() {
      return 'settings_form';
    }

    /**
     * {@inheritdoc}
     */
    protected function getEditableConfigNames() {
        return [ 'mymodule.settings',   ];
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state) {
        
    }
    
    /**
     * {@inheritdoc}
     */
    public function validateForm(array &$form, FormStateInterface $form_state) {
        
    }
    
    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
        
    }    
    
}