[Drupal 8] Migrating Drupal 7 modules to Drupal 8
https://www.zyxware.com/sites/default/files/styles/user_image/public/default_images/index.png?itok=2YmREnrP
BY abhai.sasidharan
4 years ago
Drupal
0
comments comment

Drupal, up until now a PAC (Presentation Abstraction Control) architecture, has taken a step closer to MVC (Model View Controller) architecture. Although nowhere have they mentioned about MVC, the new Drupal 8 is strictly object oriented. Another feature in Drupal 8, that is different from the existing versions of Drupal is that it is not entirely Drupal. Drupal 8 uses many components in symfony like it's ClassLoader, YAML file structure, twig and so on. For a detailed description please visit the Drupal 8 home page.

In this article, I will explain how to convert an existing Drupal 7 module to Drupal 8. In Drupal 7, the first steps you follow to create a module is

  • Create a <module name>.info file
  • Create a <module name>.module file
  • Create a <module name>.install file if a schema is required

These are the basic steps for a simple module creation. Plugins, css, images, etc can be added as required.

This is basically the same in Drupal 8 except we have to follow a yaml file structure in Drupal 8. The <module name>.info file should be renamed as <module name>.info.yml.

Say, for a module zyx, if the zyx.info file in Drupal 7 was :


name = ZYX
description = Some module to something.
package = ZYX
core = 7.x
files[] = zyx.module
files[] = zyx.install
; Information added by Drupal.org packaging script on 2013-12-12
version = "7.x-3.0"
core = "7.x"
project = "zyx"
datestamp = "1386880109"

In Drupal 8, the zyx.info.yml would be :


name: ZYX
description: 'Some module to something.'
package: ZYX
core: 8.x
version: 7.x-3.0
project: zyx
datestamp: '1386880109'
type: module

After the <module name>.info.yml file has been defined, you can go to the extend page in your Drupal site and see that the module has appeared in Drupal 8. The <module name>.install and <module name>.module files remain where they are. The schema definition remains unchanged in Drupal 8. The .module file has many changes which we will discuss now.

Firstly, hook_menu has been replaced in Drupal 8 by another yml file called <module name>.routing.yml. This is also a requisite for Drupal 8 module. Clear the hook_menu and move it with corresponding yml syntax to the <module name>.routing.yml file in the root of the module folder :

In Drupal 7 :


<?php

function zyx_menu() {
  $items = array();
  $items['zyx/one'] = array(
    'title' => 'Zyx entry',
    'description' => 'Enter zyx info into database',
    'page callback' => 'Drupal_get_form',
    'page arguments' => array('zyx_form'),
    'access arguments' => array('access zyx content'),
    'type' => MENU_NORMAL_ITEM,
  );
}

In Drupal 8 : zyx.routing.yml


zyx.form:
  path: /zyx/one
  defaults:
    _title: 'Zyx entry'
    _form: \Drupal\zyx\Form\ZyxForm
  requirements:
    _permission: 'access zyx content'

As you can see in the yml file, the path to the form is defined and we notice another difference, which is the form definition has been moved from the .module file to a class in the form folder. We have to create a folder structure in the module folder to implement the MVC part of Drupal 8 i.e.,


module/
  src/
    Controller/
    Form/ !-- Each form should be in a separate ClassName.php file. If any.
    Plugin/
      Block/ !-- If any.
  <module name>.module
  <module name>.routing.yml
  <module name>.menu.links.yml
  <module name>.info.yml
  <module name>.permissions.yml
  <module name>.install

This is standard for all Drupal modules. This is the PSR-4 compatible folder structure (Confer with PSR documentation). The form should be defined in a php file in the Form folder. This is the controller for the form. An example would be, in this case, "ZyxForm.php". Delete the form definition in <module name>.module file and copy it into the controller class definition in the form folder making the changes like below. The form definition for this would be :


<?php

/**
 * @file
 * Contains \Drupal\zyx\Form\ZyxForm.
 */

namespace Drupal\zyx\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;

class Zyx extends FormBase {
   /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'zyx_form';
  }
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['zyx_data'] = array(
      '#type' => 'textfield',
      '#title' => t('Data'),
      '#size' => 30,
      '#maxlength' => 200,
      '#description' => t('Zyx data.'),
      '#required' => TRUE,
    );
     $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t($submit),
    );
    return $form;
  }
  public function validateForm(array &$form, FormStateInterface $form_state) {
    //Code to validate here. Same as Drupal 7.
  }
  public function submitForm(array &$form, FormStateInterface $form_state) {
    //Code to submit here. Same as Drupal 7.
  }
}

Secondly, permissions are no longer set inside the .module file itself. You have to include a .permissions.yml file.

In Drupal 7 :


<?php
/**
 * Implements hook_permission().
 *
 * Access and view content defined.
 */
function zyx_permission() {
  return array(
    'access zyx content' => array(
      'title' => t('Access content for the Zyx module'),
    ),
  );
}

In Drupal 8 : zyx.permissions.yml


'access zyx content':
  title: 'Access content for the Address book module'

Thirdly, menu links are a separate file named <module name>.lniks.menu.yml unlike Drupal 8 which included it in the hook_menu itself. It is as follows :


zyx.form:
  route_name: zyx.form
  title: 'Zyx Entry'
  description: 'Enter zyx info into database'
  parent: zyx.page

Fourthly, any custom page defined in the <module name>.module will also have to be removed from the <module name>.module file. This will have to be moved to the controller folder as a Controller class similar to how form was done only difference is the class to extend :


<?php /**
 * @file
 * Contains \Drupal\zyx\Controller\DefaultController.
 */

namespace Drupal\zyx\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
/**
 * Default controller for the zyx module.
 */
class ZyxController extends ControllerBase {
  //Custom page function(s) here. Copy the functions and from <module name>.module and place it here.
}

Finally, blocks are implemented as plugins in Drupal 8. If you look at the folder structure mentioned above, you can see that in the plugin folder there is block folder. This is because blocks are no longer defined in the conventional style; hook_block_info(), hook_block_view() have been deprecated. Your block definitions should be in another controller class similar to form. An example :

ZyxChow.php


<?php 
/**
 * @file
 * Contains \Drupal\zyx\Plugin\Block\ZyxChow.
 */

namespace Drupal\zyx\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Provides the ZyxChow block.
 *
 * @Block(
 *   id = "zyx_chow",
 *   admin_label = @Translation("Zyx's Chow")
 * )
 */
class ZyxChow extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    // Code to create a block. Just like Drupal 7.
    //Return a renderable array;
  }
  
}

Here, the @block part in the comment section is a mandatory. It defines the machine name and name of the block.

These are the basic things to be changed in a Drupal 7 module to make it work in a Drupal 8 installation. A few key things to remember while migrating to Drupal 8 are the PSR-4 folder compatibility, YAML file and correct syntax to use, all paths should be defined in routing YAML file. Migrating a module to Drupal 8 can be very long and tedious due to the vast differences from Drupal 7 to Drupal 8. There is another way to migrate modules to Drupal 8, which is using a Drupal 8 module called Drupal module upgrader. This module is in its development stages and does the converting fine for modules with a simple file structure, although it is found to be buggy when migrating projects with more than one modules like the coder or devel module.

Resources:


RELATED ARTICLE

close

on 01st January 2008 / by webmaster
We have added a few new modules to our site recently. Two of these are the StumbleThis module and the DiggThis module. Drupal already had a DiggThis module available for download but there was no StumbleThis module. StumbleUpon has been a popular source of traffic for zyxware.com and we wanted users to have an option to add any of our pages to their StumbleUpon favorites. So we created our own StumbleUpon module. Normally people use stumbleupon toolbar to stumble websites. However when the toolbar is not enabled this provides an easy way to add the URL to the stumbleupon favorites. The StumbleThis button will be shown on the left of the content inline. Positioning of the StumbleThis button can be controlled by the weight option in the admin menu. By setting a negative value you can have the button displayed on top of the post while a positive value will take it to the bottom of the post.Click here to download the StumbleThis module for Drupal. The module has been tested with Drupal 5.1. Please use the comments form below if you have any questions or suggestions. Open Source Web Development Drupal Blogging Free Software Drupalgive Leave a reply Your email address will not be published. Required fields are marker * Sean (not verified) access_time 23 Jan 2019 - 06:03 Hi there, I uploaded the files, enabled the modules to use SU, and now I see the link, but there is no icon... How can I fix this? Thanks, Sean webmaster access_time 23 Jan 2019 - 06:03 In reply to Icon doesn&#039;t show by Sean (not verified) What do you see when you view the source? Search for stumblethis_button and you should be able to see the code for the image and the URL. Then you should be able to troubleshoot from there. Juicy Couture Addict (not verified) access_time 23 Jan 2019 - 06:03 thanks for the post. would love to hear more of you. by the way, drupal's really popular nowadays as it has easy and fast features. you agree with me? thanks. Add new comment
more_horiz
close

on 16th January 2008 / by webmaster
Most webmasters do not realize this, but a lot of the content on lot of websites can be accessed from multiple URLs. A simple example would be where www.example.com and example.com leads to the same page. This is a fatal mistake in Search Engine Optimization and search engines penalize you for duplicate content. The correct configuration would be where the above two urls will lead you to the same page but example.com will redirect you to www.example.com with a 301 (Moved permanently) status which will not result in search engines penalizing the page. It is very easy to configure 301 redirects using Apache .htaccess file and the process is the same for a Drupal installation also. The article is relevant only to those webservers running Apache. There are two ways in which you can issue a 301 redirect using .htaccess on an Apache Webserver. One is using a mod_alias Redirect statement and the other is using the mod_rewrite Rewrite statements. Redirect statements take paths and assign corresponding paths to be redirected to while rewrite statements take regular expression patterns and associate paths where matched urls have to be redirected to. When you install Drupal, it will automatically insert statements in the .htaccess file that will allow you to redirect www.example.com to example.com or vice versa. The only thing you have to do normally would be to uncomment the statements corresponding to your preference of accessing your domain ie with or without the www part. # If you want the site to be accessed WITH the www. only, adapt and # uncomment the following: # RewriteCond %{HTTP_HOST} ^example\.com$ [NC] # RewriteRule .* http://www.example.com/ [L,R=301] # # If you want the site to be accessed only WITHOUT the www. prefix, adapt # and uncomment the following: # RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC] # RewriteRule .* http://example.com/ [L,R=301] There is however a problem with the above redirections in that they lose the rest of the URL. Say for example if you try to access example.com/somepath and your configuration is to use the www prefix the user will be redirected to www.example.com but the somepath part will be lost. This can easily be rectified by the following small change in the Rewrite Rule # For the first option you can use the following rewrite rule # RewriteRule (.+) http://www.example.com/$1 [L,R=301] # # whereas for the second you can use the following rewrite rule # RewriteRule (.+) http://example.com/$1 [L,R=301] These modifications will ensure that the user will be redirected to the correct page with or without the www prefix and without losing on your page ranks. So make the necessary modifications and make sure that you can access the pages to your site using only one version of the URL. The above configuration of .htaccess should be used not just for Drupal websites but for all websites. If however your problem is the reverse, ie you don't have pages corresponding to the URLs that the user typed, ie if you are getting lot of 404 errors (page not found errors) and users are thrown to the default Page not found page, you can try installing the Drupal Search404 Module to search for the keywords in the URL and show relevant results to the user. You can read more about apache mod_alias and apache mod_rewrite at the apache official documentation Apache mod_rewrite and Apache mod_alias Web Development Drupal SEO Drupal Planet Leave a reply Your email address will not be published. Required fields are marker * Anonymous (not verified) access_time 23 Jan 2019 - 06:03 Hello. I'm trying to make example.com show as www.example.com, and I'm running into difficulties. I'm on Apache 2.0 and using the following lines in my httpd.conf file: RewriteEngine on RewriteCond %{HTTP_HOST} ^xxxxxxxxxx\.com$ [NC] RewriteRule ^(.*)$ http://www.xxxxxxxxxx.com/$1 [L,R=301] When I go to http://xxxxxxxxxx.com, I get http://www.xxxxxxxxxx.com (as expected). However, when I go to http://xxxxxxxxxx.com/node/1 (it's a Drupal site), I get a 404 thrown and the URL changes to xxxxxxxxxx.com/var/www/drupal/node/1. Same thing with www.xxxxxxxxxx.com/node/1. Any suggestions? I want to run without Drupal's .htaccess file (instead incorporating these calls into my httpd.conf file). webmaster access_time 23 Jan 2019 - 06:03 In reply to Rewrite including filesystem path by Anonymous (not verified) I think the problem is with the base path which results in the redirection to /var/www/ part. The best approach I would think is to start with drupal htaccess and then strip out parts and move to httpd SNVC (not verified) access_time 23 Jan 2019 - 06:03 This is definitely a good guide. Thanks for this. wellyson access_time 23 Jan 2019 - 06:03 This is really nice and helpful. Add new comment
close

on 07th January 2008 / by webmaster
We have volunteered to take up the maintenance of a very useful Drupal module - Search404. As of today we are the official maintainers of this very useful Drupal module. We know that this is going to be a challenge for us, being a young company and with a young team. But we do feel that it is our responsibility to give back to the Drupal community at least some part of what it has given us. About Search404 Module Normally when a user types in an incorrect URL in a site the web server will issue a 404 error (page not found error) and correspondingly redirect you to a 404 error page. If there is no custom 404 page on the server the browser will show the 404 page not found error to the user. This could happen when either the user types in the URL himself or he clicks on a URL incorrectly typed in another page. Another case when such errors happen is when content is deleted or moved in a site and the search engine indices have not been updated to reflect the removal or change. The web crawlers of search engines on getting a 404 error will take out the URL from their indices. The normal web user will however be totally lost when he sees the 404 error page. This is especially true if the user came from a search result page and has no idea about the navigational structure of the site or the content organization of the site. Drupal however shows its own 404 error page and avoids showing the grotesque, default 404 page that would otherwise be shown by the browser to the end user. Even this 404 error page is only a bare page with just the information that the page cannot be found. The Search404 module intelligently does a search for the terms in the URL while still issuing a 404 error. This will ensure that the URL gets taken out from the search engine index at the same time making sure that the user is given some options in terms of navigating in the site. You can check out how this works by clicking on the following non-existant URL Drupal Modules from Zyxware. Even though the page does not exist you get what you wanted to get to. If you have any questions, issues or suggestions regarding this module you can either post them as comments to this page or post your issues at the Search404 Module at Drupal.org. Open Source Web Development Drupal Leave a reply Your email address will not be published. Required fields are marker * ian douglas (not verified) access_time 23 Jan 2019 - 06:03 I notice your updates on the Drupal modules page has some patch files, but they are patches for an older version of the 5.x branch of search404. Do you have any expectation on when a version for Drupal 6 will be ready? webmaster access_time 23 Jan 2019 - 06:03 In reply to when will search404 for drupal 6 be ready? by ian douglas (not verified) Hi Ian, We are currently working on moving Search404 to Drupal 6. The port has already been done and we should have a release up on drupal.org by tomorrow or worst case by monday. Cheers Anoop John Team Zyxware Dejan (not verified) access_time 23 Jan 2019 - 06:03 In reply to when will search404 for drupal 6 be ready? by ian douglas (not verified) I think that's been up for a while... did you check the download page? Add new comment
Leave a reply
Your email address will not be published. Required fields are marker *

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2 id='jump-*'> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.