preprocessing paragraphs
play

PREPROCESSING PARAGRAPHS: A BEGINNERS GUIDE DRUPALCON SEATTLE - PowerPoint PPT Presentation

PREPROCESSING PARAGRAPHS: A BEGINNERS GUIDE DRUPALCON SEATTLE WEDNESDAY 4:00-4:30PM ROOM 602-604 LARRY WALANGITAN Senior Developer at Chromatic @larrywalangitan CHROMATIC https://chromatichq.com @chromatichq HTTPS://GIT.IO/ fk f6Y


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

  2. LARRY WALANGITAN Senior Developer at Chromatic 
 @larrywalangitan

  3. CHROMATIC https://chromatichq.com 
 @chromatichq

  4. HTTPS://GIT.IO/ fk f6Y

  5. 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 fj les without running out of memory.

  6. PREREQUISITES • Familiar with: • Drupal 8 • Twig • PHP • Paragraphs Module

  7. PARAGRAPHS • An entity with con fj gurable fj elds. • Can have di fg erent types • Also uses view modes

  8. USE CASE

  9. COMPONENT LIBRARY

  10. PREPROCESSING • Data transformation • Separation of Concerns • Logic lives outside of the template • Customization - Render Arrays

  11. A PREPROCESSING RECIPE

  12. 01 02 Each paragraph type is All preprocessing de fj ned in handled by a Class . hook_theme . 03 04 Each paragraph type Each paragraph type has a corresponding has a corresponding twig template . method.

  13. HOOK_THEME: SETUP • Base hook • Variables • Path

  14. HOOK_THEME: SETUP • Example image

  15. HOOK_THEME: BASE HOOK • Base hooks - de fj ned 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.

  16. 
 
 HOOK_THEME: VARIABLES • variables - an array of custom variables and their default values.

  17. HOOK_THEME: PATH • path - where a paragraph’s custom template lives

  18. 
 
 
 
 PREPROCESS CLASS • src/paragraphPreprocessor.php 


  19. PREPROCESS METHOD • preprocessFoo • Retrieves data • Returns variables by reference for the render array de fj ned in hook_theme

  20. 
 
 
 
 
 PARAGRAPH TEMPLATE • paragraph—foo.html.twig 


  21. 
 SERVICES.YML • Create a service: 
 chromatic_paragraphs.paragraphs_preprocess • Calls ParagraphsPreprocess class

  22. 
 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.

  23. NESTED ENTITIES • Retrieving data from entity references. • Steps: 1. Load the referenced entity 2. Return values

  24. PUTTING IT ALL TOGETHER

  25. 01 02 Each paragraph type is All preprocessing de fj ned in handled by a Class . hook_theme . 03 04 Each paragraph type Each paragraph type has a corresponding has a corresponding twig template . method.

  26. WHEN IT ALL GOES WRONG

  27. DEBUGGING TOOLS • Module: Twig Vardumper • Symfony Component: var_dumper

  28. WHY NOT IN THE THEME?

  29. 01 02 Each paragraph type is All preprocessing de fj ned in happens in .theme hook_theme . Each paragraph type 03 04 Each paragraph type has a corresponding has a corresponding template override twig template . function .

  30. QUESTIONS & ANSWERS

  31. Join us for contribution opportunities Friday, April 12, 2019 Mentored First Time Contributor General Contributions Workshop Contributions 9:00-18:00 9:00-12:00 9:00-18:00 Room: 602 Room: 606 Room: 6A #DrupalContributions

  32. 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

Recommend


More recommend