1 of 41

Drupal 8 Module Development

Not scary at all!

Jay Beaton

https://jaybeaton.com/

26 October 2017

Triangle Drupal Users’ Group

2 of 41

What we’ll cover

  • What's new? Why should I use D8?
  • Show Drupal 7 example module.
  • Show how Drupal 8 modules differ and create the same module in D8.
  • Using Drupal Console to generate code.
  • How to find help on D8.

3 of 41

What's new? Why should I use D8?

4 of 41

What's new? Why should I use D8?

  • Better content authoring (sidebar!)
  • Responsive by default
  • Better language support
  • Configuration management
  • Performance (caching)
  • Modern development framework (OOP, symfony, composer, etc.)
  • Drupal 7 is going away!

5 of 41

Better content authoring (Sidebar!)

6 of 41

What's new? Why should I use D8?

  • Better content authoring (sidebar!)
  • Responsive by default
  • Better language support
  • Configuration management
  • Performance (caching)
  • Modern development framework (OOP, symfony, composer, etc.)
  • Drupal 7 is going away!

7 of 41

Drupal 7 is going away!

Drupal 6 - 13 February 2008

Drupal 7 - 5 January 2011

Drupal 8 - 19 November 2015

Drupal 9 - ????

8 of 41

Drupal 7 Example Module

  • Shows HTML widget with weather for given Zip code.
  • Custom field on User for Zip code.
  • Block to show weather in site’s Zip code.
  • Configuration setting form for block Zip code.
  • Custom Template file for widget display.
  • Page to show 3 different weather widgets.

9 of 41

Weather Widget Module - Block

10 of 41

Weather Widget Module - Configuration

11 of 41

Weather Widget Module - Page

12 of 41

Weather Widget Module - Github

13 of 41

OK, but how do we do that in D8?

14 of 41

Let’s jump in!

15 of 41

Let’s jump in!

16 of 41

Let’s jump in!

17 of 41

Drupal 8 module diffs - yml files

18 of 41

Drupal 8 module diffs - Routing

19 of 41

Drupal 8 module diffs - Forms

20 of 41

Configuration form

21 of 41

Drupal 8 module diffs - Blocks

22 of 41

Weather Widget Block

23 of 41

Drupal 8 module diffs - Twig

24 of 41

Drupal 8 module diffs - Controllers

25 of 41

Drupal 8 module diffs - Entity data access

26 of 41

Drupal 8 module diffs - Database calls

27 of 41

Custom page

28 of 41

See, that wasn’t scary at all!

29 of 41

Drupal Console

30 of 41

It will write code for you!!!

31 of 41

Drupal Console - Generate Module Code

# drupal generate:module

Creates:

  • Module directory
  • .info.yml
  • Twig template

Module is here:

https://github.com/jaybeaton/tridug-d8-development-demo/tree/master/d8/tridug_weather_dc

32 of 41

Drupal Console - Generate Form Code

# drupal generate:form

Creates:

  • Form class
  • Routing entry for form page

33 of 41

Drupal Console - Generate Block Code

# drupal generate:plugin:block

Creates:

  • Block plugin class

34 of 41

Drupal Console - Generate Controller Code

# drupal generate:controller

Creates:

  • Controller class
  • Controller method(s)
  • Routing entries for pages

35 of 41

How to find help on D8?

36 of 41

Google!

37 of 41

Google!

Yes, but…

Search for “drupal 8 …” and limit the search to the last year.

Lots of help on Drupal Stack Exchange, too:

https://drupal.stackexchange.com/

38 of 41

Drupal API Documentation site

https://api.drupal.org/api/drupal

Start with a search for the Drupal 7 function that you’d use.

Often, there are comments saying what to use in D8.

Or, if it exists in D8, you can click on the D8 link.

39 of 41

Deprecated!

If the function is listed as “Deprecated”, it will show what to use instead.

That way, you’ll be ready for Drupal 9 with no changes!

40 of 41

Questions?

41 of 41

Go forth and build in D8!