41
PREPROCESSING PARAGRAPHS: A BEGINNERS GUIDE DRUPALCON SEATTLE WEDNESDAY 4:00-4:30PM ROOM 602-604

DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

PREPROCESSING PARAGRAPHS:A BEGINNERS GUIDE

DRUPALCON SEATTLE WEDNESDAY 4:00-4:30PM ROOM 602-604

Page 2: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

LARRY WALANGITANSenior Developer at Chromatic

@larrywalangitan

Page 3: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

CHROMATIChttps://chromatichq.com

@chromatichq

Page 4: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

HTTPS://GIT.IO/fjf6Y

Page 5: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

OUTCOMES

• Preprocess paragraphs and structure your preprocessing methods.

• Create custom render arrays and override twig templates.

• Reference nested entities and pull data into paragraph twig templates.

• Debug your preprocessing and twig files without running out of memory.

Page 6: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

PREREQUISITES

•Familiar with:

•Drupal 8

•Twig

•PHP

•Paragraphs Module

Page 7: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

PARAGRAPHS

• An entity with configurable fields.

• Can have different types

• Also uses view modes

Page 8: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays
Page 9: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays
Page 10: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

USE CASE

Page 11: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

COMPONENT LIBRARY

Page 12: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays
Page 13: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays
Page 14: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

PREPROCESSING • Data transformation

• Separation of Concerns

• Logic lives outside of the template

• Customization - Render Arrays

Page 15: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

A PREPROCESSING RECIPE

Page 16: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

01 02

03 04

Each paragraph type is defined in hook_theme.

All preprocessing handled by a Class.

Each paragraph type has a corresponding twig template.

Each paragraph type has a corresponding method.

Page 17: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

HOOK_THEME: SETUP

• Base hook

• Variables

• Path

Page 18: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

HOOK_THEME: SETUP

• Example image

Page 19: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

HOOK_THEME: BASE HOOK• Base hooks - defined in another module’s

hook_theme

• paragraphs.module has a paragraph base hook

• Allows us to override and extend the base hook with our own custom variables.

Page 20: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

HOOK_THEME: VARIABLES

• variables - an array of custom variables and their default values.

Page 21: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

HOOK_THEME: PATH

• path - where a paragraph’s custom template lives

Page 22: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

PREPROCESS CLASS• src/paragraphPreprocessor.php

Page 23: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

PREPROCESS METHOD

• preprocessFoo

• Retrieves data

• Returns variables by reference for the render array defined in hook_theme

Page 24: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays
Page 25: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

PARAGRAPH TEMPLATE• paragraph—foo.html.twig

Page 26: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

SERVICES.YML

• Create a service: chromatic_paragraphs.paragraphs_preprocess

• Calls ParagraphsPreprocess class

Page 27: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays
Page 28: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

TEMPLATE_PREPROCESS_PARAGRAPH__FOO

• This function prepares variables for our paragraph__foo template .paragraph—foo.html.twig

• Use the new service to call our preprocessing method.

Page 29: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays
Page 30: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

NESTED ENTITIES• Retrieving data from entity references.

• Steps:

1. Load the referenced entity

2. Return values

Page 31: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays
Page 32: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

PUTTING IT ALL TOGETHER

Page 33: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

01 02

03 04

Each paragraph type is defined in hook_theme.

All preprocessing handled by a Class.

Each paragraph type has a corresponding twig template.

Each paragraph type has a corresponding method.

Page 34: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

WHEN IT ALL GOES WRONG

Page 35: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

DEBUGGING TOOLS

• Module: Twig Vardumper

• Symfony Component: var_dumper

Page 36: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays
Page 37: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

WHY NOT IN THE THEME?

Page 38: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

01 02

03 04

All preprocessing happens in .theme

Each paragraph type is defined in hook_theme.

Each paragraph type has a corresponding twig template.

Each paragraph type has a corresponding template override function.

Page 39: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

QUESTIONS & ANSWERS

Page 40: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

Join us for contribution opportunities

Friday, April 12, 2019

Mentored

Contributions

9:00-18:00

Room: 602

First Time Contributor Workshop

General

Contributions

9:00-12:00

Room: 606

9:00-18:00

Room: 6A

#DrupalContributions

Page 41: DrupalCon 2019 - Preprocessing Paragraphs › sites › default › files...OUTCOMES • Preprocess paragraphs and structure your preprocessing methods. • Create custom render arrays

What did you think?

Locate this session at the DrupalCon Seattle website:

https://events.drupal.org/seattle2019/sessions/preprocessing-paragraphs-beginners-guide

Take the Survey!

https://www.surveymonkey.com/r/DrupalConSeattle