D7 to D8 tutorial: pathinfo module

Last updated on
10 April 2024

This is a live step-by-step tutorial of my process of porting pathinfo module from Drupal 7.x to Drupal 8.x as part of the global sprint weekend, March 9th-10th, 2013.

The commit of Drupal 8 I was working against is:

* 957b556 - (HEAD, origin/HEAD, origin/8.x, 8.x) Issue #1932596 by ACF: Change site_name() into config in userpasswordreset.test and formtest. (4 hours ago)

Prerequisites

I installed a copy of Drupal 8 locally to test against. With that done:

  • Start a new branch of my module: $ git checkout -b 8.x-1.x
  • On my first push, create a new upstream branch git push --set-upstream origin 8.x-1.x
  • Create a development release

Steps to upgrade

Problem Reason Solution Commit
Can't enable module. .info files are now .info.yml files Rename file and change to YAML format. 1241617 and 4c2a072
Using variable_* functions instead of CMI. Site information variables converted to config system How to upgrade variables into configuration efbe1eb
Config form doesn't change variables stored via CMI. system_settings_form() deprecated in favour of system_config_form() or \Drupal\system\SystemConfigFormBase Refactor form to use system_config_form(). b4c2b57
Config form implemented procedurally. While I used system_config_form() above, the preferred method is to extend \Drupal\system\SystemConfigFormBase. Refactor hook_menu() definitions to a routing.yml, and a controller class. e9c4566
Some paths (like admin/structure/views) are not parsed correctly by pathinfo. New Symfony-based routing system Use "request" and "controller_resolver" Drupal services to find callback. Can't get this to work. Pattern is
$request = Drupal::service('request');
$foo = $request->attributes->all();

There's some issue with multiple requests on one page load. Will come back to this.

I want to use the new theming system, Twig. Completely new theme/template system: Twig Create a templates/pathinfo-served-by.html.twig file, move theme function to pre process. This can be useful: Theming Drupal 8 47086f2
Picking this up again June 27 2013, at Drupal Dev Days.
config() function is going to be deprecated. The config() function should be deprecated in favour of Drupal::config() Replace all calls to config() with \Drupal::config(). 363fb6d
The menu item is not shown at /admin/config. Before I removed hook_menu() entirely - instead, hook_menu() deals with menus, while routes are handled by the Symfony-based routing system. Restore hook_menu() to provide a menu UI for my module. 4a44e54
Nothing is output in the footer (this is the key functionality of the module). hook_init() removed Replace hook_init() with an event subscriber service. 0e5fd56

Notes

I have to admit, somewhat embarrassingly, that the module worked almost perfectly after my first commit above. Drupal 8 still has many Drupal 7 API calls intact, but I wanted to upgrade it to use D8 paradigms, so I kept going.

Help improve this page

Page status: No known problems

You can: