Upload
valentine-matsveiko
View
121
Download
0
Embed Size (px)
Citation preview
Intro
is a web framework
?
Common Framework subsystems
Security- Auth- ACL- ?
Database- PDO- ORM- Migrations- ?
Routing
AJAX/J
Web servises/resources
View
Ready… Steady… Migrate!
Migrate yourself!
Migrate yourself!: Porting a module guide
STEP #1
Rename my_module.info -> me_module.info.yml
AND…
Migrate yourself!: Porting a module guide
That’s all!
Congrats, you’re ported a module
lets make an adoptations now)))
Migrate yourself!: src / Mind
- OOP
- PSR-4
- Annotations
- Dependency Injection
- Service Containers
- Plugins
- Entities
/ CoreConcepts.php
Migrate yourself!: src / Mind / Annotations.php
@see core/lib/Drupal/Core/Annotation/Mail.php
/ CoreConcepts / DependencyInjection.php
DI is implementation of the IoC design pattern
NO!YES
/ CoreConcepts / DependencyInjection.php
/ CoreConcepts / ServiceContainers.php
@are Auto-instantiate service-orientedactionable classes with allregistered dependencies
/ CoreConcepts / Plugins.php
“Plugin - A discreet class that executes anoperation within the context of a givenscope, as a means to extend Drupal’sfunctionality”
/ 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
/ 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
/ CoreConcepts / Plugins.php
Plugin Manager DiscoveryFactory Mapper
STANDS ON
/ CoreConcepts / Plugins.php
AnnotatedClassDiscoveryHookDiscovery YamlDiscoveryStaticDiscovery
DISCOVERY
/ CoreConcepts / Plugins.php
DEMO
/ CoreConcepts / Entity.php
ONLY DEMO :)2X
Cache API
Drupal 7 caching style?Forget it!
New Cache API
Drupal 8 Cache API
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']);
New Cache API
Cache tags
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.
New Cache API
Cache contexts
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)
Routing
Drupal 8 Routing System
Routing
*.routing.yml
example.content: path: '/example' defaults: _controller: '\Drupal\example\Controller\ExampleController::content' _title: 'Hello World' requirements: _permission: 'access content'
_method: 'GET'
Routing
src/Controller/ExampleController.php:
class ExampleController extends ControllerBase { /** * {@inheritdoc} */ public function content() { return ['#type' => 'markup', '#markup' => t('Hello World!')]; }
}
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'
Routing
src/Controller/ExampleController.php:
class ExampleController extends ControllerBase { /** * {@inheritdoc} */ public function content($my_arg) { return ['#type' => 'markup', '#markup' => $my_arg]; }
}
Form API
Form API
Form API
New Form(s) implementation workflow
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
Form API
New HTML 5 elements:
● '#type' => 'tel'● '#type' => 'email'● '#type' => 'number'● '#type' => 'date'● '#type' => 'url'● '#type' => 'search'● '#type' => 'range'● etc.
Form API
Not enought? - Create your own!
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'
Form API
Retrieving the form outside of routes:
$form = \Drupal::formBuilder()
->getForm('Drupal\example\Form\ExampleForm');
Form API
Forms altering...doesn’t really changed
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);
Libraries
Libraries
Libraries
*.libraries.yml:my_library: version: 1.x css: theme: css/styles.css: {} js: js/script.js: {}
dependencies:
- core/drupal
- core/jquery
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.
Libraries
Twig attach libraries:
{{ attach_library('mymodule/my_library') }}
<div>Some markup {{ message }}</div>
Libraries
Disable aggregation:my_library: version: 1.x css: theme: css/style.css: {preprocess: false} js: js/script.js: {preprocess: false}
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 }
Libraries
Inline JavaScript is highly discouraged!
Migrate yourself!: src / Module / Multilingual / ConfigVariables.php
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
Migrate yourself!: src / Module / Multilingual / ConfigVariables.php
STEP #2 add default local task, my_module.links.task.yml
Migrate yourself!: src / Module / Multilingual / ConfigVariables.php
STEP #3 make config_object translateablemy_module.config_translation.yml
Migrate yourself!: src / Module / ViewsIntegration / CustomTable.php
@see core/modules/tracker/tracker.views.inc
Dev Tools
Don’t forget about a new Dev tools:● Drupal console
● Kint
● REPL
● Webprofiler
● etc.
Conclusion
is awesome!
Any questions?
Thanks!