The History and Future of Drupal Theming.

50
The History and Future of Drupal Theming by Carl Wiedemann (c4rl) Wednesday 2013-09-25 • 14:15

description

Okay. Once upon a time there was a content management system that needed to render markup. So this Content Managment System returned strings of HTML. Well that really wasn't that great because there were these people out in the world called Front End Developers and it just so happens that Front End Developers actually care about what the HTML looks like. Oh, by the way, this Content Management System is named Drupal. Read more: https://prague2013.drupal.org/session/history-future-drupal-theming

Transcript of The History and Future of Drupal Theming.

Page 1: The History and Future of Drupal Theming.

The History and Future of Drupal Theming

by Carl Wiedemann (c4rl)Wednesday 2013-09-25 • 14:15

Page 2: The History and Future of Drupal Theming.

Hi!

c4rlCarl Wiedemann

Page 3: The History and Future of Drupal Theming.

Where have we been?

Where are we going?

Discussion and planning :)

Page 4: The History and Future of Drupal Theming.

2000OO-based(!) theming,somewhat overridable.

Page 5: The History and Future of Drupal Theming.

2001Default markup in core provides more

scalability.

Page 6: The History and Future of Drupal Theming.

2002The well-known

theme() function appears.

Page 7: The History and Future of Drupal Theming.

2003Template-based

theming (xtemplate) to avoid string concatenation

(in some cases).

Page 8: The History and Future of Drupal Theming.

2005PHPTemplate arrives. Template variable overrides exist.

FormAPI and altering arrives in concept.

Page 9: The History and Future of Drupal Theming.

2007Theme registry.

Preprocess functions, .tpl.php file overrides.

Page 10: The History and Future of Drupal Theming.

2007hook_elements()

appears. "Structured" vs "rendered" data

bring together modern RenderAPI.

Page 11: The History and Future of Drupal Theming.

ALTER ALL OF THE THINGS

ALTER ALL OF THE THINGS

Page 12: The History and Future of Drupal Theming.

http://drupal.org/node/134478#comment-538674

Posted by bjaspan on October 15, 2007 at 1:39pm

"menu callbacks ought to return structured data, not rendered data"

Page 13: The History and Future of Drupal Theming.

<?php

// OLD

return theme('foo', $bar);

<?php

// NEW

return array( '#theme' => 'foo', '#bar' => $bar,);

Page 14: The History and Future of Drupal Theming.

hook_page_alter()template_preprocess_foo()

template_preprocess()template_process_foo()

template_process()

Templates can be overridden...

Preprocessors can be overridden...

Processors can be overridden...

Theme functions still exist...

hook_theme_registry_alter()...

Page 15: The History and Future of Drupal Theming.

by 2009Many APIs. Theme

system is brittle, inconsistent,

difficult to grok.

Page 16: The History and Future of Drupal Theming.
Page 17: The History and Future of Drupal Theming.
Page 18: The History and Future of Drupal Theming.

Based on what we’ve built…

What do we need?

Page 19: The History and Future of Drupal Theming.

What do we need? (1)

Template-based markup that can be extended and

overridden.

hook_theme(), template_preprocess(), hook

suggestions, theme registry, Twig (PHPTemplate).

Page 20: The History and Future of Drupal Theming.

What do we need? (2)

Abstracted, alterable structure.

Render arrays, hook_node_view(), hook_page_alter()

Page 21: The History and Future of Drupal Theming.

What do we need? (3)

Sensible, accessible, API.

theme(), drupal_render(), render()/hide()/show().

Page 22: The History and Future of Drupal Theming.

2012DrupalCon Denver Core

Convo, Twig initiative, yay!

Page 23: The History and Future of Drupal Theming.

2012-2013Twig conversion,Portland Sprints,

killed process layer, markup cleanup, yay.

Page 24: The History and Future of Drupal Theming.

So where are we going?

Page 25: The History and Future of Drupal Theming.

8.xKill concatenation 1757550

Theme system architecture 2004872

Theme registry service 1886448

theme() deprecation 2006152

Page 26: The History and Future of Drupal Theming.
Page 27: The History and Future of Drupal Theming.

PRESENT1. I believe the

biggest issue with Theming is a brittle

RenderAPI.

Page 28: The History and Future of Drupal Theming.

PRESENT2. I believe Render API must move to an

OOP design.

Page 29: The History and Future of Drupal Theming.

9.xRefactor RenderAPI

to be OO1843798

http://lb.cm/arrays-of-doom

Page 30: The History and Future of Drupal Theming.

What does might OO

RenderAPI look like?

Page 31: The History and Future of Drupal Theming.

Influential design patterns

BuilderDecorator

Page 32: The History and Future of Drupal Theming.

Builder

Page 33: The History and Future of Drupal Theming.

Builder pattern

Object creation is delegated to a series of steps, then

finally invoked.

This fulfills the data storage component of a

Renderable.

Page 34: The History and Future of Drupal Theming.

<?php

$car_builder = new CarBuilder();$car_builder->setDoors(4);$car_builder->setColor('red');$car_builder->setMake('ford');

$car = $car_builder->createCar();

Page 35: The History and Future of Drupal Theming.

Builder pattern

Builder-esque Drupalisms

hook_node_view()

hook_page_alter()

Page 36: The History and Future of Drupal Theming.

Decorator

Page 37: The History and Future of Drupal Theming.

Decorator pattern

Behavior added to object dynamically at runtime without affecting other

objects of the same class.

This fulfills runtime variable preparation.

Page 38: The History and Future of Drupal Theming.

<?php

class Coffee() { function say() { return 'I am coffee'; }}

Page 39: The History and Future of Drupal Theming.

<?php

class CoffeeDecorator() extends Coffee { function __construct($coffee) { $this->coffee = $coffee; } function say() { return $this->coffee->say(); }}

Page 40: The History and Future of Drupal Theming.

<?php

class Coffee_Cream() extends CoffeeDecorator { function say() { return parent::say . ' with cream'; }}

class Coffee_Sugar() extends CoffeeDecorator { function say() { return parent::say . ' with sugar'; }}

$c = new Coffee_Cream(new Coffee_Sugar(new Coffee()));

Page 41: The History and Future of Drupal Theming.

Decorator pattern

Decorator-esque Drupalisms

comment_preprocess_node()

mytheme_preprocess_node()

Page 42: The History and Future of Drupal Theming.

Getting markup1. Get data.-- START RENDER PROCESS --2. Define renderable.3. Alter renderable.4. Render renderable. A. Call theme callbacks. i. Call preprocessors. ii. Invoke template.-- END RENDER PROCESS --5. Send markup to client.

Page 43: The History and Future of Drupal Theming.

Disclaimer:Let’s not get hung-up on

implementation:)

Page 44: The History and Future of Drupal Theming.

Renderable definition<?php // D7/D8

return array( '#theme' => 'node', '#node' => $node,);

<?php // D9?

return new RenderableBuilder('ThemeNode', array( 'node' => $node, ));

Page 45: The History and Future of Drupal Theming.

Altering<?php // D7/D8 function mymodule_node_view(&$node) { $node->content['image']['#theme'] = 'foo';}

<?php // D9?

function mymodule_node_view_alter($builder) { $builder->get('image')->setBuildClass('ThemeFoo');}

Page 46: The History and Future of Drupal Theming.

Variable prepare (1)<?php // D7/D8 function template_preprocess_foo(&$vars) { $new_title = $vars['title'] . ' overridden by foo'; $vars['title'] = $new_title;}

Page 47: The History and Future of Drupal Theming.

Variable prepare (2)<?php // D9

/** * @file Theme class for foo.tpl.php. */

class ThemeFoo extends Renderable { function prepare() { $new_title = $this->get('node')->get('title') . ' overridden by foo; $this->set('title', $new_title); }}

Page 48: The History and Future of Drupal Theming.

MOAR

http://github.com/c4rl/renderapi

Page 49: The History and Future of Drupal Theming.

Discussion and planning :)

Page 50: The History and Future of Drupal Theming.

THANK YOU!

WHAT DID YOU THINK?

Locate this session at the DrupalCon Prague website:http://prague2013.drupal.org/node/1863

Click the “Take the survey” link