December 2015
  • Drupal

Geocoder_Autocomplete: Drupal 7 to 8 Module Migration

My firs porting of a Drupal 8 Module.

During the Hackaton held as part of the Italian Drupal Day 2015 of Bologna, I (itamair) have been engaged in developing a Drupal 8 possibile version of the Geocoder_Autocomplete module, together with other members of the Drupal community (cronos, rafael92, gik000).

This module is very simple. In its version for Drupal 7 consists of a hook_menu and a callback function that has focused its main application logic: a call to a Web service from Google that returns in real time auto-complete addresses for the processed geo locations, based on the string inputed by the user. 

The module also creates and exposes a specific text field widgets that enable these features.

The simplicity of this module really helps the understanding of the basic logic ruling the porti and the construction of modules in Drupal 8.

First it was necessary to make sure you have a compatible version of PHP (>= 5.5.9), and download the latest version of Drupal 8 (of course).

Essential tools for developers of modules in Drupal 8, are also Drush (in its 7 version) and Composer.

With these tools you can use the Drupal Module Upgrader (DMU) as usage analysis and "kickoff" tool in migration of the module to D8.

Once you put the D7 module version in Drupal modules folder, DMU allows you to use the following commands:

- drush dmu-analyze MODULE_NAME, to scan code and get a report of code that needs updating;

- drush dmu-upgrade MODULE_NAME, to attempt to upgrade your Drupal 7 module's code to Drupal 8 automatically,;

Obviously this is a tool (very) useful but by no means exhaustive, and also in the case of such a simple module as geocoder_autocomplete represented, it should be accounted only as a first step of the porting process.

The rest of the work was done by writing code by hand, with the indispensable support of PhpStorm, integrated with extensions and plugins for Symfony, Drupal and the Drupal Coding Standards.

In essence the new proposed final module consists of the following files:

  • geocoder_autocomplete.info.yml: contains the module signature and definitions; 
  • geocoder_autocomplete.module: contains the module callback functions and hooks;
  • geocoder_autocomplete.permissions.yml: defines and lists module defined permissions;
  • geocoder_autocomplete.routing.yml: defines “routes”, and the controllers' methods that act as routes callbacks;
  • geocoder_autocomplete.services.yml: defines the custom services that are used inside the module;
  • src/GeocoderJsonConsumer.php:  the definition of the custom (and reusable) service  performing the geocoding application logic, through the google webservice endpoints;
  • src/Controller/GeocoderController.php: the defining of the controller class of the module, and its method that acts as a callback to the route, and that uses the geocoding service, through dependecy injection
  • src/Plugin/Field/FieldWidget/GeocoderAutocomplete.php: the definition of geocoder_autocomplete field widgets, as an extension of the fields "string"

The diagram in the figure is useful to better understand the differences between Drupal 7 and Drupal 8 functional logics and the higher fragmentation into different files of the code (at least in the case of a so simple module).

Here under is attached the geocoder_autocomplete module, in this D8 release like sent on drupal.org: https://www.drupal.org/node/2633962

 

  • Drupal 8
  • Simfony