migrate yourself. code -> module -> mind

55

Upload: valentine-matsveiko

Post on 12-Apr-2017

121 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: Migrate yourself. code -> module -> mind
Page 2: Migrate yourself. code -> module -> mind

Intro

is a web framework

?

Page 3: Migrate yourself. code -> module -> mind

Common Framework subsystems

Security- Auth- ACL- ?

Database- PDO- ORM- Migrations- ?

Routing

AJAX/J

Web servises/resources

View

Page 4: Migrate yourself. code -> module -> mind

Ready… Steady… Migrate!

Migrate yourself!

Page 5: Migrate yourself. code -> module -> mind

Migrate yourself!: Porting a module guide

STEP #1

Rename my_module.info -> me_module.info.yml

AND…

Page 6: Migrate yourself. code -> module -> mind

Migrate yourself!: Porting a module guide

That’s all!

Congrats, you’re ported a module

lets make an adoptations now)))

Page 7: Migrate yourself. code -> module -> mind

Migrate yourself!: src / Mind

- OOP

- PSR-4

- Annotations

- Dependency Injection

- Service Containers

- Plugins

- Entities

/ CoreConcepts.php

Page 8: Migrate yourself. code -> module -> mind

Migrate yourself!: src / Mind / Annotations.php

@see core/lib/Drupal/Core/Annotation/Mail.php

Page 9: Migrate yourself. code -> module -> mind

/ CoreConcepts / DependencyInjection.php

DI is implementation of the IoC design pattern

Page 10: Migrate yourself. code -> module -> mind

NO!YES

/ CoreConcepts / DependencyInjection.php

Page 11: Migrate yourself. code -> module -> mind

/ CoreConcepts / ServiceContainers.php

@are Auto-instantiate service-orientedactionable classes with allregistered dependencies

Page 12: Migrate yourself. code -> module -> mind

/ CoreConcepts / Plugins.php

“Plugin - A discreet class that executes anoperation within the context of a givenscope, as a means to extend Drupal’sfunctionality”

Page 13: Migrate yourself. code -> module -> mind

/ CoreConcepts / Plugins.php

- Block- FieldWidget- FieldType- Filter- ImageEffect- Tips- Display- ActionPlugin- EntityTypePlugin…...

USED IN:

ctools- panels- hybridauth- feeds- addressfield

fluxservice:- own plugin system,

cfr- own plugin system,

D8 D7

Page 14: Migrate yourself. code -> module -> mind

/ CoreConcepts / Plugins.php

- Lazy loaded- Unified code (using interfaces)- extensible- reusable across a projects

- each plugin type should have own plugin manager, wasted?

PROFIT

NOTICE

Page 15: Migrate yourself. code -> module -> mind

/ CoreConcepts / Plugins.php

Plugin Manager DiscoveryFactory Mapper

STANDS ON

Page 16: Migrate yourself. code -> module -> mind

/ CoreConcepts / Plugins.php

AnnotatedClassDiscoveryHookDiscovery YamlDiscoveryStaticDiscovery

DISCOVERY

Page 17: Migrate yourself. code -> module -> mind

/ CoreConcepts / Plugins.php

DEMO

Page 18: Migrate yourself. code -> module -> mind

/ CoreConcepts / Entity.php

ONLY DEMO :)2X

Page 19: Migrate yourself. code -> module -> mind

Cache API

Drupal 7 caching style?Forget it!

Page 20: Migrate yourself. code -> module -> mind

New Cache API

Drupal 8 Cache API

Page 21: Migrate yourself. code -> module -> mind

New Cache API

Request cache_default bin:$cache = \Drupal::cache();

Request a particular bin(cache_custom):$custom_cache = \Drupal::cache('custom');

Retrieve a cached data:$data = \Drupal::cache()->get('my_value');

Save data to the cache:$data = \Drupal::cache()->set('my_value', ['some_data']);

Page 22: Migrate yourself. code -> module -> mind

New Cache API

Cache tags

Page 23: Migrate yourself. code -> module -> mind

New Cache API

Invalidate a cache items with a particular tag(s):

\Drupal\Core\Cache\Cache::invalidateTags(array('node:5', 'my_tag'));

Retrieve an existent entity’s cache tags:● \Drupal\Core\Entity\EntityInterface::getCacheTags()● \Drupal\Core\Entity\EntityTypeInterface::getListCacheTags()

Cache tags allow us to invalidate caches more smartly.

Page 24: Migrate yourself. code -> module -> mind

New Cache API

Cache contexts

Page 25: Migrate yourself. code -> module -> mind

New Cache API

Cache contexts are analogous to HTTP's Vary header.

It helps Drupal to decide whether the cached response can be used rather than requesting a fresh one.

Contexts examples:theme (vary by negotiated theme)

user.roles (vary by the combination of roles)

languages (vary by all language types: interface, content …)

url (vary by the entire URL)

Page 26: Migrate yourself. code -> module -> mind

Routing

Drupal 8 Routing System

Page 27: Migrate yourself. code -> module -> mind

Routing

*.routing.yml

example.content: path: '/example' defaults: _controller: '\Drupal\example\Controller\ExampleController::content' _title: 'Hello World' requirements: _permission: 'access content'

_method: 'GET'

Page 28: Migrate yourself. code -> module -> mind

Routing

src/Controller/ExampleController.php:

class ExampleController extends ControllerBase { /** * {@inheritdoc} */ public function content() { return ['#type' => 'markup', '#markup' => t('Hello World!')]; }

}

Page 29: Migrate yourself. code -> module -> mind

Routing

Arguments inside routes:*.routing.yml

example.content: path: '/example/{my_arg}' defaults: _controller: '\Drupal\example\Controller\ExampleController::content' _title: 'Hello World' requirements: _permission: 'access content'

Page 30: Migrate yourself. code -> module -> mind

Routing

src/Controller/ExampleController.php:

class ExampleController extends ControllerBase { /** * {@inheritdoc} */ public function content($my_arg) { return ['#type' => 'markup', '#markup' => $my_arg]; }

}

Page 31: Migrate yourself. code -> module -> mind

Form API

Form API

Page 32: Migrate yourself. code -> module -> mind

Form API

New Form(s) implementation workflow

Page 33: Migrate yourself. code -> module -> mind

Form API

\Drupal\Core\Form\FormInterface - everything what you usually need!

- getFormId() - specify Form ID- buildForm() - build form array- validateForm() - form validation handler- submitForm() - form submit handler

Page 34: Migrate yourself. code -> module -> mind

Form API

New HTML 5 elements:

● '#type' => 'tel'● '#type' => 'email'● '#type' => 'number'● '#type' => 'date'● '#type' => 'url'● '#type' => 'search'● '#type' => 'range'● etc.

Page 35: Migrate yourself. code -> module -> mind

Form API

Not enought? - Create your own!

Page 36: Migrate yourself. code -> module -> mind

Form API

Integrate the form in a request:mymodule.test_form: path: 'mymodule/test_form' defaults: _form: '\Drupal\mymodule\Forms\ExampleForm' _title: 'Test form' requirements: _permission: 'access content'

Page 37: Migrate yourself. code -> module -> mind

Form API

Retrieving the form outside of routes:

$form = \Drupal::formBuilder()

->getForm('Drupal\example\Form\ExampleForm');

Page 38: Migrate yourself. code -> module -> mind

Form API

Forms altering...doesn’t really changed

Page 39: Migrate yourself. code -> module -> mind

Form API

Rendering forms programmatically:// Retrieve a form array.

$form = \Drupal::formBuilder()

->getForm('Drupal\example\Form\ExampleForm');

// Render it.$rendered_form = \Drupal::service('renderer')

->render($form);

Page 40: Migrate yourself. code -> module -> mind

Libraries

Libraries

Page 41: Migrate yourself. code -> module -> mind

Libraries

*.libraries.yml:my_library: version: 1.x css: theme: css/styles.css: {} js: js/script.js: {}

dependencies:

- core/drupal

- core/jquery

Page 42: Migrate yourself. code -> module -> mind

Libraries

Attaching libraries:function mymodule_element_info_alter(array &$types) {

$types['table']['#attached']['library'][] = 'mymodule/my_library';

}

Note: Can be attached to any render-able array.

Page 43: Migrate yourself. code -> module -> mind

Libraries

Twig attach libraries:

{{ attach_library('mymodule/my_library') }}

<div>Some markup {{ message }}</div>

Page 44: Migrate yourself. code -> module -> mind

Libraries

Disable aggregation:my_library: version: 1.x css: theme: css/style.css: {preprocess: false} js: js/script.js: {preprocess: false}

Page 45: Migrate yourself. code -> module -> mind

Libraries

CDN/externally hosted libraries:angular.angularjs:

remote: https://github.com/angular/angular.js

version: 1.4.4

js:

https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:

{ type: external, minified: true }

Page 46: Migrate yourself. code -> module -> mind

Libraries

Inline JavaScript is highly discouraged!

Page 47: Migrate yourself. code -> module -> mind

Migrate yourself!: src / Module / Multilingual / ConfigVariables.php

Page 48: Migrate yourself. code -> module -> mind

Migrate yourself!: src / Module / Multilingual / ConfigVariables.php

STEP #1 add variables with text, string type to the root of config_object var inside a my_module.schema.yml

WARNING: works only with first level depth text-based elementsin the config_object

Page 49: Migrate yourself. code -> module -> mind

Migrate yourself!: src / Module / Multilingual / ConfigVariables.php

STEP #2 add default local task, my_module.links.task.yml

Page 50: Migrate yourself. code -> module -> mind

Migrate yourself!: src / Module / Multilingual / ConfigVariables.php

STEP #3 make config_object translateablemy_module.config_translation.yml

Page 51: Migrate yourself. code -> module -> mind

Migrate yourself!: src / Module / ViewsIntegration / CustomTable.php

@see core/modules/tracker/tracker.views.inc

Page 52: Migrate yourself. code -> module -> mind

Dev Tools

Don’t forget about a new Dev tools:● Drupal console

● Kint

● REPL

● Webprofiler

● etc.

Page 53: Migrate yourself. code -> module -> mind

Conclusion

is awesome!

Page 54: Migrate yourself. code -> module -> mind

Any questions?

Page 55: Migrate yourself. code -> module -> mind

Thanks!