Transcript
Page 1: Drupal8 simplepage v2
Page 2: Drupal8 simplepage v2

Drupal 8 simple pageA walk for Drupal 8 module development

Page 3: Drupal8 simplepage v2

@estoyausenteSamuel Solís

Page 4: Drupal8 simplepage v2

</spam>

Coupon code 10€ discount*

<spam>

#DrupalSummer2016

*if($totalAmount > 60€)

Page 5: Drupal8 simplepage v2
Page 6: Drupal8 simplepage v2
Page 7: Drupal8 simplepage v2

GO!

Page 8: Drupal8 simplepage v2

D7 Site building?

Page 9: Drupal8 simplepage v2

D7 modules?

Page 10: Drupal8 simplepage v2

PHP development?

Page 11: Drupal8 simplepage v2

Drupal 7

Page 12: Drupal8 simplepage v2

Drupal 8

Page 13: Drupal8 simplepage v2

This slides have examples

based on my experience

for more information please read the API page

Page 14: Drupal8 simplepage v2

Index

1. Configuration manager

2. Routing

3. Forms

4. Services

5. Theming

Page 15: Drupal8 simplepage v2

Configuration manager

Impresiones

1. ¡Funciona!

2. ¡Funciona bien!

3. Overrides entre entornos -> Settings

4. ¿Menús?

5. Un poco caos

Page 16: Drupal8 simplepage v2

Configuration manager

Impresiones

1. ¡Funciona!

2. ¡Funciona bien!

3. Overrides entre entornos -> Settings

4. ¿Menús?

5. Un poco caos

Page 17: Drupal8 simplepage v2

Configuration manager

Impresiones

1. ¡Funciona!

2. ¡Funciona bien!

3. Overrides entre entornos -> Settings

4. ¿Menús?

5. Un poco caos

Page 18: Drupal8 simplepage v2

Configuration manager

Impresiones

1. ¡Funciona!

2. ¡Funciona bien!

3. Overrides entre entornos -> Settings

4. ¿Menús?

5. Un poco caos

Page 19: Drupal8 simplepage v2

Configuration manager

Impresiones

1. ¡Funciona!

2. ¡Funciona bien!

3. Overrides entre entornos -> Settings

4. ¿Menús?

5. Un poco caos

Page 20: Drupal8 simplepage v2

Configuration manager

Take a look

1. https://www.drupal.org/project/config_tools

2. https://www.drupal.org/project/config_update

3. https://www.drupal.org/project/config_sync

4. https://www.drupal.org/project/config_devel

5. https://www.drupal.org/project/features

Page 21: Drupal8 simplepage v2

Mi first module

Drupal generate:module

Page 22: Drupal8 simplepage v2

Routing

example_dyb_landing_content: path: '/landing-my-awsome-path' defaults: _controller: '\Drupal\example_dyb_landing\Controller\LandingContentController::pageContent' requirements: _access: 'TRUE'

example_dyb_landing/example_dyb_landing.routing.yml

Page 23: Drupal8 simplepage v2

Routing

example_dyb_landing_content: path: '/landing-my-awsome-path' defaults: _controller: '\Drupal\example_dyb_landing\Controller\LandingContentController::pageContent' requirements: _access: 'TRUE'

example_dyb_landing/example_dyb_landing.routing.yml

Start with slash /

Page 24: Drupal8 simplepage v2

Controller

namespace Drupal\example_dyb_landing\Controller;use Drupal\Core\Controller\ControllerBase;use Drupal\Core\Url;class LandingContentController extends ControllerBase { public function pageContent() { $content = []; //Render array return $content; }}

example_dyb_landing/src/Controllers/LandingContentController.php

Page 25: Drupal8 simplepage v2

Forms

namespace Drupal\example_dyb_landing\Form;use Drupal\Core\Form\FormBase;use Drupal\Core\Form\FormStateInterface;class ContactForm extends FormBase { public function getFormId() { return 'contact_form'; }

example_dyb_landing/src/Form/ContactForm.php

Page 26: Drupal8 simplepage v2

Forms

public function buildForm(array $form, FormStateInterface $form_state) { $form['mail'] = array( '#type' => 'email', '#title' => $this->t('Email'), '#required' => TRUE, '#attributes' => ['class' => ['formcontrol']], );

return $form; }

example_dyb_landing/src/Form/ContactForm.php

Page 27: Drupal8 simplepage v2

Forms

public function validateForm(array &$form, FormStateInterface $form_state) { $values = $form_state->getValues(); if(!\Drupal::service('email.validator')->isValid($values['mail'])) { $form_state->setErrorByName('mail', $this->t('The email address %mail is not valid.', array('%mail' => $values['mail']))); } }

example_dyb_landing/src/Form/ContactForm.php

Page 28: Drupal8 simplepage v2

Forms

public function submitForm(array &$form, FormStateInterface $form_state) { $values = $form_state->getValues(); $to = \Drupal::config('system.site')->get('mail'); $language_interface = \Drupal::languageManager()->getCurrentLanguage(); \Drupal::service('plugin.manager.mail')->mail('example_dyb_landing', 'contact_message', $to, $language_interface, $values, 'no-replyđ@mail.com'); drupal_set_message($this->t('Thank for contact us. Your message has been sent correctly.'));}

example_dyb_landing/src/Form/ContactForm.php

Page 29: Drupal8 simplepage v2

Forms

$output['bottom']['form'] = [ '#type' => 'container', '#attributes' => ['class' => ['left']], 'form' => \Drupal::formBuilder()->getForm('Drupal\example_dyb_landing\Form\ContactForm'), ];

example_dyb_landing/src/Form/ContactForm.php

Page 30: Drupal8 simplepage v2

Forms

$output['bottom']['form'] = [ '#type' => 'container', '#attributes' => ['class' => ['left']], 'form' => \Drupal::formBuilder()->getForm('Drupal\example_dyb_landing\Form\ContactForm'), ];

example_dyb_landing/src/Form/ContactForm.php

Page 31: Drupal8 simplepage v2

Services

*This is a spanish joke

*

Page 32: Drupal8 simplepage v2

Services

services: rest_client.client: class: Drupal\rest_client\RestClient

example_dyb_landing/rest_client.services.yml

Page 33: Drupal8 simplepage v2

Services

namespace Drupal\rest_client;/** * Class RestClient * @package Drupal\rest_client */ class RestClient{ protected $url; public function __construct() { $this->url = 'myserver.com'; }

example_dyb_landing/src/RestClient.php

Page 34: Drupal8 simplepage v2

Services

public function getSpecialities($country = 'ES') { $cid = 'rest_client_getSpecialities_' . $country; if ($cache = \Drupal::cache()->get($cid)) { return $cache->data; }

//$output = getSomeStuff();

\Drupal::cache()->set($cid, $output); return $output; } return [];}

example_dyb_landing/src/RestClient.php

Page 35: Drupal8 simplepage v2

Servicios

public function getSpecialities($country = 'ES') { $cid = 'rest_client_getSpecialities_' . $country; if ($cache = \Drupal::cache()->get($cid)) { return $cache->data; }

//$output = getSomeStuff();

\Drupal::cache()->set($cid, $output); return $output; } return [];}

example_dyb_landing/src/RestClient.php

Page 36: Drupal8 simplepage v2

dependencies: - rest_client

example_dyb_landing/example_dyb_landing.info.yml

Dependency injection

Page 37: Drupal8 simplepage v2

namespace Drupal\register_form\Form;use Drupal\rest_client\RestClient;

class RegisterForm extends FormBase{ protected $client; public function __construct(RestClient $client) { $this->client = $client; } public static function create(ContainerInterface $container) { return new static( $container->get('rest_client.client'), ); }

example_dyb_landing/src/Form/RegisterForm.php

Dependency injection

Page 38: Drupal8 simplepage v2

Dependency injection

public function buildForm(array $form, FormStateInterface $form_state) {

$form['speciality'] = [ '#type' => 'select', '#options' => $this->client->getSpecialities(), '#title' => $this->t('Speciality'), '#required' => TRUE, '#attributes' => ['class' => ['formcontrol']],]; return $form;

}

example_dyb_landing/src/Form/RegisterForm.php

Page 39: Drupal8 simplepage v2

My first theme

Drupal generate:theme

Page 40: Drupal8 simplepage v2

Mi primer theme

name: publictype: themedescription: Public theme.package: Othercore: 8.xlibraries: - public/global-stylingbase theme: classyregions: content: Content header: Header footer: Footer

public/public.info.yml

Page 41: Drupal8 simplepage v2

Add css/js

global-styling: version: 1.x css: theme: css/style.css: {} js: js/public.js: {} dependencies: - core/jquery - core/drupal - core/drupalSettings

public/public.libraries.yml

Page 42: Drupal8 simplepage v2

Preprocess

function public_preprocess_breadcrumb(&$variables){ //Add current page to breadcrumb. $request = \Drupal::request(); $route_match = \Drupal::routeMatch(); $page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject()); $variables['breadcrumb'][] = array( 'text' => $page_title, );}

public/public.theme

Page 43: Drupal8 simplepage v2

Twig

{% if breadcrumb %} <nav class="breadcrumbs" role="navigation" aria-labelledby="system-breadcrumb"> <h2 class="visually-hidden">{{ 'Breadcrumb'|t }}</h2> <ol class="itemlistbread"> {% for item in breadcrumb %} <li class="itembread"> {% if item.url %} <a href="{{ item.url }}" class="itembread__link">{{ item.text }}</a> {% else %} <span>{{ item.text }}</span> {% endif %} </li> {% endfor %} </ol> </nav> {% endif %}

public/templates/elements/breadcrumb.html.twig

Page 44: Drupal8 simplepage v2

Templates

<?php db_query('DROP TABLE {users}’);

?>

Page 45: Drupal8 simplepage v2

Templates

<?php db_query('DROP TABLE {users}’);

?>

Page 46: Drupal8 simplepage v2

Homework

• Caché

• Composer

• REST

• Migrate

• The change is so big…

Page 47: Drupal8 simplepage v2

Questions?

Page 48: Drupal8 simplepage v2
Page 49: Drupal8 simplepage v2

Thanks and enjoy this DrupalSummer!

@estoyausenteSamuel Solís


Top Related