Porting Modules To Drupal 8

In this post I will Guide you in the steps in Porting your modules to drupal 8 and show you the major changes which it went through.

Also this is a task which im doing for the annual competition call Google code in which is a competition which pre-university students ages 13 to 17 are invited to take part in Google Code-in, our contest introducing young minds to the world of open source. With a wide variety of bite-sized tasks, it’s easy for beginners to jump in and get started no matter what skills you have. Mentors from our participating organizations are available to lend a helping hand as you learn what it’s like to work on an open source project.

Step 1:

The first thing is that you will start off with your module looking like

now when you are porting your module to Drupal 8 the files which you will be changing and modifying is the .info, .module  and .admin.inc and also you will be creating about two more files.

So now the first thing you should do is change the .info file to .info.yml

Then inside the yml file change all (=) signs to ( : ), change all double quote(") to single quote(') and also change the core to 8.x .Add a new line call type and let it equal to module eg. type:module. Change your configuration to (nameofmodule).admin or anything you want eg . configure: back_to_top_controller  which would be your controller.Then you can change the version and delete the unused ans unnecessary lines.

Step 2:

Enable your module inside your website and then  run your various tests  if necessary  or continue to step 3,more info can be found for step one and two if needed .

Step 3:

When you are done create a new file with the extension (nameofmodule).routing.yml  eg.

cause in Drupal 8 this file is used to link the separate parts of your modules together which may be accompany with other file but with this module it was used only, but if you want to know what other files to add check  here.

When editing this file you will :

  1. Start with your controller name which was created in .info.yml then colon to end the line
  2. State the file where the configuration for your module would be found eg. '/admin/config/user-interface/(name of module)'
  3. Write defaults with a colon to end line
  4. Under and inside of that  you will put the title and the main 'Form' path eg. \Drupal\(name of module)\Form\(nameofmodule)Form
  5. Then below the defaults create requirements and place the permissions below it .
  6. To get more information on your module if need more attributes check here.

An example of what it should look like is here.

Step 4:

Now you will be needed to implement your page. This is used instead of the .admin.inc file which is used to configure your forms for the module.

So to do so you are needed to:

  1. Create a folder name 'src' inside of your main module directory
  2. inside of the 'src' directory create another folder call 'Form'
  3. Then inside that folder create a file with name (nameofmodule)Form.php eg.

Step 5:

Now inside of that .php file you should then write the following :

  1. write the ending (?>) and starting php (<?php) lines
  2. write the namespace (Drupal\(name of module)\Form;)
  3. let it use the a base adapter writen like (Drupal\core\Form\(baseadpater)
  4. Create the main class which then extends too the base adapter
  5. Inside of the class create the getFormId() function which returns the form.
  6. After that function create a buildForm() function with parameters array $form, and array $form_state . This will be used to create the forms for your module in the cofiguration.
  7. Then create a function name submitForm with the same parameters as buildForm. This will be used to submit the configuration if they are changed by user.
  8. When you are finish it should look like the picture below but for extra detail if needed for more complex module check 1 and or 2

Step 6:

Now you are needed to insert you forms from the admin.inc file .To do so go into your admin.inc file and copy everything from it which is under the main class except for the return statement.

Step 7:

After Pasting you will then be needed to create a settings file which will saved the various setting which will be needed for your module .To do so :

  1. create a folder with name 'config' inside of the main directory
  2. create a directory with the name install inside of that directory
  3. Then inside of the install directory create the settings file with name (name of module).setting.yml)

4. After that inside of the file write the default setting for each form which was pasted inside of the php file .

Now the reason for all of this is because:

  • Modules store default settings in configuration files that are stored in a config/install/directory in the module root directory.
  • When a module is enabled these default settings are copied to the active configuration in the site's database.
  • When settings are changed by a site administrator these are stored in the active configuration in the database, which can be exported back to files.

Step 8:

Then now inside of the .php  file you will need to change the variables so that they correspond properly and dont result in any error and so that you can save it to the settings file.

So to do so you are going to have to create a config variable which will then link to the setting.yml file  , to do that right ($config = $this->config('setting file name ') )an example  $config = $this->config('back_to_top.setting').

Then after that you would then change all (variable_get) to ($config->get) .This is changing the variables so that it get from the config variable which then calls the settings from the setting file.

After at the  end return the function  eg.  return parent::buildForm($form, $form_state);

Step 9:

When you are finish with that you are needed to submit each of the forms so then to do that:

  1. Under the submit function create a sub-function similar to below.

2. Create variables for each form to get their values using the format ($FormName = $form_state->getValue((name of module )_name of form ) an example is shown below.

3. When you have created a variable for each form to get their values then you need to create the config variable again.

4. Then below that set the result for each of the forms using the variable which was used to get each of the values, the code to do so go like ($config ->set('(name of module_ name of form)', variable for form)), create it for each with just the (->) following the setting code for the second and forms after the second when setting.

5. After when you are done setting each finish it with the (->save();) statement to save everything which have been done.

Step 10:

When you are done changing variables and configuring everything in the .php file then you should also change the variables also in the module file also using the same config variable as before, and also remember to put he config variable under the function only with the variable_get  . An example is shown below.

Step 11:

When you are done with doing that you are basically finish and your main directory should look something like this , so you can delete the admin.inc file .

but then if you module need more things to be done or you need extra help on porting your module on to drupal 8 then you can check  help 1 and help 2 .

Then now to activate your module go on to your website and then configuration and then performance . when you have done  then clear you cache . then your module suppose to work.

Step 12:

An  example of my module is  :

For extra detailing and help check here.