Wrights HQ

A blog by Ian Wright - Front-end web developer

To create my Drupal 8 module I setup the required files & folder structure. In Drupal 7 you would require a .info & .module file. Now in Drupal 8 the only required file is the .info equivalent modulename.info.yml which contains similar information about the module. All modules which are install in Drupal 8 go into the following location /modules/, for my module I used the following file structure /modules/custom/geoplotting.

Within this directory I created the only required file geoplotting.info.yml

name: Geoplotting
description: Provides a google map formatter for geofield.
configure: geoplotting.config
type: module
package: Field types
core: 8.x
 - geofield

If you are a Drupal 7 developer you will recognise a few of the keys (name, description, package, core & dependencies), now in Drupal 8 you have to add type, the reason for this is because theme files also use YAML files.

Setting up a config page

As part of my .info.yml file I added a configure key, this is my modules configuration page. Drupal 8 now uses Symfony2 as part of core, because of this, hook_menu() is no longer required to define paths but Symfony2 routing component is used. This involves defining the routes as configuration (inside a YAML file) and handling the callback in a controller class.

From my modules root I created the following file geoplotting.routing.yml

 path: '/admin/config/geoplotting/config'
  _form: 'Drupal\geoplotting\Form\GeoplottingConfig'
  _title: 'Geoplotting'
  _permission: 'administer geoplotting config'

I’ve created a new route config for my module geoplotting (the first is the module name and the second the route name). For the path I set the path I want this route to register (accessible URL). Under defaults, I have two settings: the default page title (_title) and the form config (_form) which references our GeoplottingConfig class. Under requirements, I’ve specify a custom permission administer geoplotting config which is required to access the settings page.

Setting up custom permissions

For my module, I wanted to allow site owner to set who can access the settings page (Google API key & also What3words API – more on this later) because of this I created a customer permission.

From my modules root I created the following file geoplotting.permissions.yml

administer geoplotting config:
 title: 'Administer configuration'
 description: 'Allows users to set API keys and global geoplotting settings'
 restrict access: TRUE

This is just the start of how I’ve created my custom module during 10% time at miggle – next time I will talk about my modules configuration & help pages.