68
® \ Drupal 8 Kickstart An Overview for Developers Stanford Drupal Camp 2016 Stanford, CA -- April 1 - 2, 2016 Peter Sawczynec Customer Success Engineer

Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

  • Upload
    others

  • View
    20

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

® \

Drupal 8 Kickstart An Overview for Developers

Stanford Drupal Camp 2016Stanford, CA -- April 1 - 2, 2016

Peter Sawczynec

Customer Success Engineer

Page 3: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8: Mission

● D8: A service returning a response of format-agnostic data structures

● Whether the request comes from a desktop browser, mobile phone, or another website the response data will be returned consistently

● How the response data gets formatted is, as much as possible, a distinct and separate set of actions

Page 4: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8: Mission

● Leverage existing industry-standard technologies so that D8 can interface with and be programmed like other globally-recognized PHP frameworks using PHP OOP concepts

● To achieve the missions D8 is built on top of the Symfony framework components

Page 6: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 Online Documentation

● Check the documentation creation date to judge the timeliness and accuracy of online D8 documentation

● Other than docs found on Drupal.org, D8 online documentation older than October or November 2015 is very unlikely to be fully accurate

Page 7: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 / Symfony: Special Note

D8 will upgrade to Symfony 3.0 in a minor release and drop Symfony 2.x backwards compatibilityPosted by catch on January 7, 2015

While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue to get bug fixes and security releases more actively and for a longer time period.

Core, contrib and custom modules should not rely on any deprecated Symfony APIs, since these may be removed in any Drupal 8 minor release...

Page 8: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 Essentials

DrushComposer

YAMLPHP OOP

Comments / AnnotationsTesting

Page 9: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 and Drush

Drush A command line tool for managing Drupal that provides uncountable shortcut commands● Drush executes Drupal admin tasks 3 - 10x faster

than using the admin pages● Install drush with Composer

Page 10: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 and Drush

Drush can run update.php, clear cache, log you in, change user passwords, disable/enable modules, execute sql queries, manage features. Example drush commands:drush status drush uli drush cc all drush updb

drush en devel -y drush pmi devel

drush upwd --password="newsecurepasswoed" "admin"

drush sqlq 'SELECT schema_version FROM system WHERE name="views"'

drush sqlq "UPDATE system SET schema_version = 12 WHERE name='views'"

Page 11: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 and Composer

● Composer helps you declare, manage and install dependencies of PHP projects, ensuring you have the right stack everywhere

● Composer uses .json files to keep track of the versions of php libraries and other software that you might employ in your website. Then when you need with a single composer command one can download new or update all the software

Page 12: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 and Composer

An enterprise D8 website with a large resources dir can download and keep current all that software with two commands:composer -install composer -update

Page 13: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 and Composer Manager

Composer Manager Module Allows contributed modules and your own custom modules to manage the inclusion of PHP and other supporting libraries via Composer. Info Documentation

Page 14: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 and YAML Files

YAML (*.yml files)● A simple, clean format (similar to JSON) for storing

structured data that is easier to read/write than XML ● YAML is a recursive acronym for:

"YAML Ain't Markup Language"● All Drupal 8 configuration is created using YAML and

during installation pulled from *.yml files

Page 15: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 and YAML Files

● YAML is case sensitive● YAML structure is created by using indenting with

spaces. YAML does not allow the use of tabs● Use 2 spaces for YAML indenting in Drupal

Schema Files (*.schema.yml files)● Schema files define the expected structure and

allowed elements of YAML files (like DTD for XML)

Page 16: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®

Sample YAML filecore-services.yml

D8 and YAML Files

Page 17: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 and PHP OOP

Class● A set of functions and properties organized in a file

that offer a service● Controllers, Routers, Forms, and Plugins are all

major types of classes in D8. ● In general all functionality created for D8, including

your custom modules, is expected to be created in class files

Page 18: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 and PHP OOP

Interface ● A class with empty default methods that all other

classes based on it must offer● Every single method declared in an Interface will have

to be implemented in the subclass. A class can implement multiple Interfacesclass MyClass implements ParentInterfaceclass MyClass implements SomeInterface, OtherInterface

Page 19: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 and PHP OOP

Abstract Class ● A class with default abstract methods that classes

based on it must offer ● Only Abstract methods have to be implemented by the

subclass. A class can only implement one abstract class at a time.class MyClass extends ParentClassclass MyClass extends ParentClass implements SomeInterface, OtherInterface

Page 20: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 and PHP OOP

Trait ● A set of php functions in one file that supply a useful

set of related functions

Page 21: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8: Genric PHP Class Overview

file: Mammal.php (an abstract class file)abstract class Mammal {

protected $age_;

//below are functions I think all mammals will have,including people

abstract public function setAge($age);

abstract public function getAge();

abstract public function eat($food);

}

file: Plane.php (an interface file)interface Plane {

public function Fly();

}

file: Gun.php (an interface file)interface Gun{

public function shoot();

}

Page 22: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8: Genric PHP Class Overview

file: Person.php (a class file)use /Mammal.php

use /Person.php

use /Gun.php

class Person extends Mammal implements Plane,Gun {

protected $job_;

// Person features from Mammal

public function setAge($age){ $this->age_ = $age; }

public function getAge(){ return $this->age_; }

public function eat($food){ echo '<br/>I eat ' ,$food ,' today<br/>'; }

// Only a person has these feature.

public function setJob($job){ $this->job_ = $job; }

public function getJob(){ echo 'My job is ' , $this->job_; }

//-----------------------------------------

// Below methods are implementations from interfaces

public function fly(){ echo '<br/>I use plane,so I can fly<br/>'; }

public function shoot(){ echo 'I use gun,so I can shoot<br/>'; }

}

Page 23: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8: Genric PHP Class Overview

file: people_in_action.php (a regular php file)

use /Person.php

$People = new Person();

print '<pre>';

print_r(get_class_methods('People'));

print '</pre>';

$People->setAge(24);

print $People->getAge();

$People->eat('egg');

$People->setJob('PHP devepop');

print $People->getJob();

$People->fly();

$People->shoot();

Page 24: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 and PHP OOP

Dependency Injection ● Initiating a class, but telling the class what you

want it to use to work. ● See: What is Dependency Injection?

by Fabien Potencier

Page 25: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 and PHP OOP

Services ● Something a class offers, e.g. “map this node’s

location by address, returns latitude and longitude” Plugins ● In D8 plugins are used, for example, to make Blocks,

in that your Block and what describes it, builds it, and controls access to the Block is found in a special kind of class called a plugin

Page 26: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 and Code Comments

Comments and special comments called Annotations are very important in D8Properly formatted comments are used by D8 to create documentation, identify tests, and in some cases for D8 to make discovery of services and other plugin functionalityLinks: Drupal Comments Annotations in Drupal

Page 27: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®Method Chaining

Method Chaining (used by jQuery, PHP, Drupal) Allows us to run a series of methods, one after the other (or in a chain), because each method in the chain after it executes returns a full object with the the changes applied to it jQuery Method Chaining example: $("#edit-button").css("color","red").slideUp(2000).slideDown(2000);

Page 28: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®Method Chaining (multiline)

jQuery method chaining (multiline):$("#p1").css("color","red")

.slideUp(2000)

.slideDown(2000);

D8 Example (multiline):db_update('example') ->condition('id', $id) ->fields(array('field2' => 10)) ->execute();

Above using the Database Abstraction Layer where db_update returns an UpdateQuery object

Page 29: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®Short Array Syntax

As of PHP 5.4. In use throughout D8. The short array syntax replaces array() with [].

// Array Language Construct, Constructor function

$build = array(); $build = array(0 => 'value0');

// Short Array Syntax (requires PHP 5.4)

$build = []; $build = [0 => 'value0']; $build = ['value0', 'value1', 'key2' => ['value2', 'value3']];

Page 30: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®Type Hinting

function tracking_inject_page_attachments(array &$page) { $tracking = new TrackingInject(); $tracking_header_items = $tracking->getTrackingInjectCollections(); $tracking_header = $tracking_header_items['html_head']; // Add each header tracking element into page HEAD area. if (!empty($tracking_header)) { foreach ($tracking_header as $tracking_header_item) { …. }

}

public function onResponse(FilterResponseEvent $event) { $response = $event->getResponse(); if ($response instanceOf RedirectResponse && !devel_silent()) { …. }

}

Use type hinting to specify the expected data type of an argument in a function declaration. When you call the function, PHP checks whether or not the arguments are of the specified type. If not, the run-time will raise an error and execution will be halted.

Page 31: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8

Drupal 8

Page 32: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®Install Drupal 8mv default.services.yml services.ymlcp default.settings.php settings.phpchmod 777 services.ymnlchmod 777 settings.php[Continue and complete Drupal 8 install]

chmod 655 services.ymnlchmod 655 settings.phpcd ../../composer install

[Create your database]

mkdir <new site name>cd <new site name>git clone http://git.drupal.org/project/drupal.gitgit taggit checkout -b tags/<tagname> <tagname>[Perform Apache config updates, e.g. virtual directory][Start the Drupal 8 install. Pauses for settings updates]

cd sites/defaultmkdir fileschmod 777 files

Page 33: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 Top-level Directory Structure● /core Core modules and files provided by D8● /libraries Create yourself when needed for common 3rd party libraries,

e.g. a wysiwyg editor ● /modules Contrib and custom modules using sub-dirs contrib and custom

(used to be sites/all/modules) ● /profiles Contrib and custom profiles● /sites Site specific modules, themes and files. Including files uploaded

by users, such as images. The site’s YAML configuration files, active and staged

● /themes Contrib themes, custom themes and subthemes● /vendor External 3rd party libraries and projects, e.g. phpUnit, Behat

Page 34: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®

D8 top-level directory structure. Your global custom modules reside in:/modules/custom

Your global contrib modules reside in:/modules/contrib

Page 35: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 Core Directory Structure

Inside /core directory:● /core/assets Various external libraries used by Core. jQuery, underscore, modernizer etc● /core/config Configuration YAML files● /core/includes Functionality that is to low level to be modular. Such as the module system itself● /core/lib Drupal Core classes● /core/misc Frontend libraries that Drupal Core depends on. (jQuery, modernizer, etc)● /core/modules Drupal Core modules● /core/profiles Drupal Core profiles. Empty at the time of writing● /core/scripts Various CLI scripts, mostly used by developers● /core/tests Drupal Core tests● /core/themes Drupal Core themes● /core/vendor Backend libraries that Drupal Core depends on. (Symfony, Twig, etc) http://drupal.

stackexchange.com/questions/84811/what-are-all-the-directories-for-in-the-new-drupal-8-structure

Page 36: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 Module Structure

Page 37: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 Bootstrap

1. Bootstrap configuration○ Read the settings.php file, generate some other settings

dynamically, and store them both in global variables and the Drupal\Component\Utility\Settings singleton object

○ Start the class loader, takes care of loading classes○ Set the Drupal error handle.○ Detect if Drupal is actually installed. If it is not, redirect

to the installer script

Page 38: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 Bootstrap

2. Create the Drupal kernel3. Initialize the service container

(either from cache or from rebuild)4. Add the container to the Drupal static class5. Attempt to serve page from static page cache 6. Load all variables 7. Load other necessary include files

Page 39: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 Bootstrap

8. Register stream wrappers (public://, private://, temp:// and custom wrappers)

9. Create the HTTP Request object (using the Symfony HttpFoundation component)

10. Let DrupalKernel handle it and return response11. Send response12. Terminate request

(modules can act upon this event)

Page 40: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 YAML (*.yml) Files

Replaces .info files and used for Configuration, Routes, Menu Links, and ServicesPronounced: “YA-MUL” is short for: “YAML Ain’t Markup Language”

D8 D7<module_name>.info.yml <--> <module_name>.info file<module_name>.routing.yml <--> hook_menu for page paths<module_name>.links.menu.yml <--> hook_menu for entries on admin menu<module_name>.permissions.yml <--> hook_permissions<module_name>.services.yml <--> Describes a class: machine name, class path, mandatory arguments

Page 41: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®Sample Module Services .yml File

services: tracking_inject.manager: class: Drupal\tracking_inject\TrackingInjectManager arguments: ['@database'] tags: - { name: backend_overridable } tracking_inject.response_event: class: Drupal\tracking_inject\EventSubscriber\TrackingInjectEventSubscriber tags: - { name: event_subscriber } tracking_inject.injections: class: Drupal\tracking_inject\TrackingInject arguments: ['@config.factory']

Page 43: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 Events

D8 uses Symfony kernel and events. Kernel events available in D8 are as follows:

● KernelEvents::CONTROLLER CONTROLLER event occurs once a controller was found forhandling a request

● KernelEvents::EXCEPTION EXCEPTION event occurs when an uncaught exception appears

● KernelEvents::FINISH_REQUEST FINISH_REQUEST event occurs when a response was generated for a request

Page 44: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 Events

● KernelEvents::REQUEST REQUEST event occurs at the very beginning of request dispatching

● KernelEvents::RESPONSERESPONSE event occurs once a response was created for replying to a request

● KernelEvents::TERMINATE TERMINATE event occurs once a response was sent

● KernelEvents::VIEW VIEW event occurs when the return value of a controller is not a Response instance

Page 45: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®

SampleD8EventSubscriberclass file

Page 46: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®Services in D8

Core functionality in D8 such as current user info, current path, node info, is logged in, module exists… these are all called services● Core services in D8 are declared in: /core/core.

services.yml ● Services can be accessed throughout D8 via the

global Drupal namespace \Drupal

Page 47: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®Services in D8

Examples of using D8 core services:

\Drupal::moduleHandler()->moduleExists('content_translation');

$account = \Drupal::currentUser();

$config = \Drupal::config('some_module.settings');

Page 48: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®Services in D8

Examples of using D8 core services:

$id = $config->get('domain_id');

$request = \Drupal::request();

$exception = $request->attributes->get('exception');

$status = $exception->getStatusCode();

Page 49: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8: variable_get, variable_set

● Replaced by using a D8 core service… (also understand states, settings and overrides)

● Config is the global D8 configuration object and holds the changeable site or module configurations, e.g.: \Drupal::config('system.site') ->get('page.front');

Page 50: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8: variable_get, variable_set

● Getting a variable:\Drupal::config(‘module_name.settings’)->get(‘var_name’);\Drupal::config('system.site') ->get('page.front');

● Setting a variable:\Drupal:: configFactory()->getEditable('module_name.settings')

->set(‘var_name’, ‘some_value’)->save;● Unsetting a variable value:

$config = \Drupal::config('system.performance');$config->clear('cache.page.max_age')->save();

Page 51: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8: Config vs Settings

Settings is the global D8 settings object and holds site settings like the database settings that are in settings.php.

● A get settings example: use\Drupal\Core\Site\Settings$theme = Settings::get()->('maintenance_theme', ‘bartik’);

● A set settings in settings.php example:$settings['maintenance_theme'] = ‘my_custom_theme’;

Page 52: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8: Caching

D8 Caching has two main concepts:Caching and Cache Invalidation“Caching is easy: it's merely storing the result of an expensive computation, to save time the next time you need it. Cache invalidation is hard: if you fail to invalidate all the things that should be invalidated, you end up with incorrect results. If you invalidate too many things, your cache hit ratio is going to suffer, and you'd be inefficiently using your caches. Invalidating only the affected things is very hard.” -- Wim Leers

Page 54: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8: Caching Thought Process1. Render Array Caching. I'm rendering something. That means think of cacheability.

My Render Array Caching2. Is this something that's expensive to render, and therefore is worth caching? If the answer is "yes", then what identifies

this particular representation of the thing I'm rendering? Those are the cache keys.My Render Array Caching Invalidation (Cacheablility Metadata)

3. Does the representation of the thing I'm rendering vary per combination of permissions, per URL, per interface language, per … something? Those are the cache contexts. Note: cache contexts are completely analogous to HTTP's Vary header.

4. What causes the representation of the thing I'm rendering to become outdated? I.e. which things does it depend upon, so that when those things change, so should my representation? Those are the cache tags.

5. When does the representation of the thing I'm rendering become outdated? I.e. is the data valid for a limited period of time only? That is the max-age (maximum age). It defaults to "permanently (forever) cacheable" (Cache::PERMANENT). When the representation is only valid for a limited time, set a max-age, expressed in seconds. Zero means that it's not cacheable at all.

Cache contexts, tags and max-age must always be set, because they affect the cacheability of the entire response. Therefore they "bubble": parents automatically receive them. Cache keys must only be set if the render array should be cached.

Page 55: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8: Caching

Cache Contexts cookies :nameheaders :nameiplanguages :typerequest_formatroute .book_navigation .menu_active_trails

:menu_name .namesessionthemetimezone

url .host .query_args

:key.pagers

:pager_id .siteuser .is_super_user .node_grants

:operation .permissions .roles

:role

Page 56: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8: Caching

Cache max-ageWhat max-age allows you to do:

When $build['#cache']['max-age'] is not set:permanent cacheability (Cache::PERMANENT) is assumed.

To indicate that a render array is not cacheable at all, set: $build['#cache']['max-age'] = 0 (i.e. zero seconds).

And to indicate that a render array is cacheable only for a limited amount of time, e.g. 5 minutes, set: $build['#cache']['max-age'] = 300; // set in seconds, i.e. 300 / 60 = 5 min.

Page 57: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8: Render Array w/ Cachingfunction my_module_build_array() {$build = [

'#prefix' => '<aside>', '#markup' => t('Hi, %name, welcome back to @site!', [ ' % name' => $current_user->getUsername(), '@site' => $config->get('name'), ]), '#suffix' => '</aside>', '#theme' => 'my_module_build_arry_theme', '#cache' => [ 'contexts' => ['user', 'url . query_args:quantity’], 'keys' => ['my_module_build_render', 'cache', 'demo'], 'tags' => ['node:42:en, config . system . performance], 'max-age' => 300, ], '#pre-render' => 'my_module_build_pre_render', '#attached' => [ 'library' => 'core / jquery', 'drupalSettings' => ['foo' => 'bar'], ],

];}

Render Array Reference

Page 58: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8: Basic TwigBasic Variables {{ title }}

Conditional Logic {% if title %} <h3>{{ title }}</h3>{% endif %}

Filters {{ ponies|safe_join(", ")|lower }}

AttribibutesAttributes is an object available to every twig template. Its job is to store all the relevent attributes of the parent container and give the themer helpful methods to interact with that data.

There should not be any space between the tag name and the twig syntax. See red text in below:

<div{{ attributes }}></div>

{% set classes = [

'red','green',

]%}<div{{ attributes.addClass(classes) }}> {% if options.alignment == 'horizontal' %} {% for row in items %} <div{{ row.attributes.addClass(row_classes, options.row_class_default ? 'row-' ~ loop.index) }}> {% endfor %} </div> {% endif %}

Page 59: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 Routes and Controllers

Routing System in D8A route is a path which is defined for Drupal to return some sort of content on.For example, the default front page, '/node' is a route. When Drupal receives a request, it tries to match the requested path to a route it knows about. If the route is found, then the route's definition is used to return content. Otherwise, Drupal returns a 404.Drupal's routing system works with the Symfony HTTP Kernel. The routing system is responsible for matching paths to controllers, and you define those relations in routes. You can pass on additional information to your controllers in the route. Access checking is integrated as well.

Page 60: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 Routes and Controllers

example.routing.ymlexample.content: path: '/example' defaults:

_controller: '\Drupal\example\Controller\ExampleController::content'_title: 'Example Route Response '

requirements:_permission: 'access content'

Page 61: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 Blocks Plugin

Block plugin creation overveiw.D8 is looking for block content to be returned as render arrays.

Page 62: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 CMI

Configuration Management Initiative● Saving D8 global and module settings into and

reading settings from *.yml files and also special D8 CMI tables in the database. Links:

Configuration Mangement InitiativePrinciples of Configuration Management - Pt 1Principles of Configuration Management - Pt 2D8 CMI critical analysis

Page 63: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®Drupal Configuration Inspector

Drupal Configuration Inspector● A module that exposes the configuration settings in

use throughout your site using nice visual organization. Links:Configuration Inspector Module

Page 65: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D8 Console

Partial list of D8 Console commands: generate:controller Generate and register a controller

generate:entity:config Generate a new "EntityConfig"

generate:entity:content Generate a new "EntityContent"

generate:form:config Generate a new "ConfigFormBase"

generate:module Generate a module.

generate:plugin:block Generate plugin block.

generate:plugin:imageeffect Generate image effect plugin.

generate:service Generate service

See all Console commands

Page 66: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®D7 to D8 Module Upgrader

Module Upgrader ● A D8 module that can analyze your Drupal 7

module for needed changes and/or attempt the actual upgrade. Links:About Module UpgraderDownload D8 ‘Module Upgrader’ Module

Page 67: Drupal 8 Kickstart · While Drupal 8.0.0 will likely ship using Symfony 2.7, in a subsequent minor release we will upgrade Symfony to use the 3.x branch. This will allow us to continue

®Testing

PHPUnit, Mink, Goutte headless or a browserAbove paradigm to replace SimpleTest in the long run with Behat