Dev days Szeged 2014: Plugin system in drupal 8

Post on 21-Nov-2014

344 views 0 download

Tags:

description

Drupal dev days 2014 presentation in Szeged. Plugin system in drupal 8. This is the unchanged version of my slides.

Transcript of Dev days Szeged 2014: Plugin system in drupal 8

Drupal 8 plugin system aspilicious!

About me •  Bram Goffings

@aspilicious

•  Drupal developer

•  Work for Nascom www.nascom.be

•  Maintainer of Display Suite

•  Drupal 8 core contributor

Target audience

•  Site builders & module developers

•  Experience with module development

•  Understanding of the hook system

•  Know how to handle object oriented code

Success?

•  Nobody is scared anymore

•  Everyone can make a *basic* plugin type

Not the expert

•  I’m an end user, just like you

•  Learned by trying (and failing)

•  I can say stupid things

Drupal developer •  Mr. Steven

•  Brave drupal 7 expert

•  Made his first Drupal 8 site

•  Wants to build a plugin system

Client

•  Mr. Arnold

•  Rich webmaster

•  Loves cars

•  I want to choose my favorite car on my profile from a predefined list.

•  It should be possible to plug in different cars into the list.

•  The name and an image of the selected car should placed on the homepage.

I take the job…

Step 1

I want to choose my favorite car on my profile from a predefined list.

Arnold’s wireframe

Steven’s plan

•  Add a “Car selector” field

•  Create a custom select widget

The drupal 7 way

•  Implements hook_field_widget_info()

•  Implements hook_field_widget_form()

Those hooks are gone!

They are plugin-y-fied!

What is a plugin?

Plugins are small pieces of functionality that are swappable.

Plugins that perform similar functionality are of the same plugin type.

Field widget plugins!

•  There is a “widget” plugin type!

•  Each field widget is from that type!

Structure

SelectWidget.php

namespace Drupal\options\Plugin\Field\FieldWidget;

SelectWidget.php

namespace Drupal\options\Plugin\Field\FieldWidget;

SelectWidget.php

namespace Drupal\options\Plugin\Field\FieldWidget;

SelectWidget.php

namespace Drupal\options\Plugin\Field\FieldWidget;

Namespaces

•  Namespaces were introduced in PHP 5.3

•  Used to avoid name collisions

PSR-0

•  Aims to provide a standard file, class and namespace convention to allow plug-and-play code.

•  Uses the defined namespaces to automatically load files when needed.

•  One on one mapping between namespaces and directory structure.

SelectWidget.php

namespace Drupal\options\Plugin\Field\FieldWidget;

Structure

PSR-4

•  “New standard”

•  Makes alternative mapping possible

•  Drupal will use it to remove “unneeded” folders. The empty *drupal* and *module-name* folder won’t be needed anymore.

Let’s recap

•  We know how to find plugins

•  We understand namespaces and the directory structure

•  We understand PSR-0 and the difference between PSR-4

SelectWidget.php

Available methods

Car module structure

car.info.yml

Step 1: done

Let’s recap

•  We know how to start with writing a field widget plugin

•  We know how to start writing a custom module

Step 2

It should be possible to plug in different cars into the list.

Steven’s plan

•  Create a custom plugin type named “Car”

•  Create two sample car plugins

Custom plugin type

•  Create a plugin manager service record

•  Create an annotation class

•  Create the plugin manager class

Custom plugin type

car.services.yml

Car module structure (part 2)

Sample plugins

Sample plugins

getOptions() v2

Debug output

Car module structure (part 3)

Let’s recap

•  We know how to write a basic plugin manager

•  We know how to handle annotations

•  We know how to get all the plugin definitions

Disadvantage

Each car requires a plugin

Derivative

•  Allows you to create multiple plugin definitions through a single plugin class.

Derivative example!

•  Create a yml file containing cars!

•  Create a derivative plugin!

•  Create a definition for each car in the yml file!

cars-example.yml

YamlCar.php

Step 2: done

Let’s recap

•  We know when to use derivatives

•  We are able to build one

Step 3

The name and an image of the selected car should placed on the homepage.

Steven’s plan

•  Add a function to the plugins that returns an image url

•  Create the frontpage

hook_menu is gone!

car.routing.yml

Car module structure (part 4)

Let’s recap

•  We figured out how to add a function to a plugin

•  We know how to create a plugin instance

•  We saw how we should create our routes

•  We learned how to build a page controller

The end

Questions?

THANK YOU!