9. Drupal 8 loves themers!!
●
Configuración Archivo .yaml
●
CSS (Arquitectura, nomenclatura, etc)
●
Inspector de plantillas / variables
incorporado.
●
Mismas plantillas front y back
●
...
10. Archivo de configuración
D7
my_theme.info
name = Bartik
description = A flexible,
Recolorable ...
package = Core
version = VERSION
core = 7.x
stylesheets[all][] = css/layout.css
stylesheets[all][] = css/style.css
regions[header] = Header
regions[help] = Help
regions[page_top] = Page top
regions[page_bottom] = Page bottom
regions[highlighted] = Highlighted
...
settings[shortcut_module_link] = 0
D8
my_theme.info.yaml
name: Bartik
type: theme
description: 'A flexible, recol...
package: Core
version: VERSION
core: 8.x
stylesheets:
all:
- css/layout.css
print:
- css/print.css
ckeditor_stylesheets:
- css/ckeditor-iframe.css
regions:
header: Header
help: Help
settings:
shortcut_module_link: '0'
11. Archivo de configuración
D7
my_theme.info
; Add a new CSS file:
stylesheets[all][] = layout.css
; Override a CSS file:
; WARNING: The file is moved from its original position to the
;
end of the CSS files stack! Use the PHP override code below.
stylesheets[all][] = system.theme.css
; Remove a module CSS file by *adding* it but without supplying
; the actual file in the filesystem:
stylesheets[all][] = node.css
13. Archivo de configuración
D8
mytheme.info.yml
# Add a new CSS file:
stylesheets:
all:
- layout.css
# Override a CSS file:
stylesheets-override:
- system.theme.css
# Remove a CSS file:
stylesheets-remove:
- node.css
http://dgo.to/1876600
14. Organización archivos CSS
Drupal 8 sigue la guía de estilo SMACSS
para categorizar sus reglas CSS:
●
●
●
●
●
Base — CSS reset.
Layout — estructura general, grid
systems.
Component — elementos UI reusables.
State — estilos que alteran los
elementos según la interacción.
Theme — estilos visuales (“look-andfeel”) de los componentes.
15. Organización archivos CSS
Base
●
Uso de normalize.css
http://necolas.github.io/normalize.css/
●
Y seguimiento de Boilerplate
http://html5boilerplate.com/
16. Organización archivos CSS
Layout – Component - State
●
*.module.css
●
*.admin.module.css
●
Borrar referencias a tags (li.first)
●
Sustituir # por .
●
Archivos -rtl.css. separados
●
Uso de selectores descriptivos
●
Reducir «supuestos»
20. Organización archivos CSS
●
Example Layout – Component - State:
/**
* Progress Bar small variant
*/
.progress--small .progress__track {}
.progress--small .progress__bar {}
.progress--small .progress__cancel {}
21. Inspección de plantillas
●
Gracias a Twig tenemos un DEBUG de código:
●
Editando settings.php
$settings['twig_debug'] = TRUE;
●
Obteniendo automáticamente:
<!-- THEME DEBUG -->
<!-- CALL: theme('node') -->
<!-- FILE NAME SUGGESTIONS:
* node--article.html.twig
* node--2.html.twig
x node.html.twig
-->
<!-- BEGIN OUTPUT from
'core/modules/node/templates/node.html.twig' -->
...
<!-- END OUTPUT from
'core/modules/node/templates/node.html.twig' -->
22. Inspección de variables
●
Gracias a Twig tenemos un DEBUG de código:
●
Editando settings.php
$settings['twig_debug'] = TRUE;
●
Podemos usar:
{{ dump(title) }}
Obteniendo un resultado como krumo
●
BYE BYE DEVEL_THEMER module!
23. Beneficios inherentes de TWIG
AL NO SER PHP:
●
Podemos usar las mismas plantillas desde el
BACK (PHP) como desde el FRONT (backbone,
underscore, ...).
https://github.com/schmittjoh/twig.js
●
Las plantillas son interpretables (y editables)
desde los navegadores. ¿ Contemplate ?
24. Puntos flojos de Drupal 7
●
Sintaxis
●
Inconsistencia
●
Complejidad
●
Redundancia
●
Seguridad
25. Puntos flojos de D7: Sintaxis
<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?>
clearfix"<?php print $attributes; ?>>
<?php print render($title_prefix); ?>
<?php if (!$page): ?>
<h2<?php print $title_attributes; ?>>
<a href="<?php print $node_url; ?>"><?php print $title; ?></a>
</h2>
<?php endif; ?>
<?php print render($title_suffix); ?>
<?php if ($display_submitted): ?>
<div class="meta submitted">
<?php print $user_picture; ?>
<?php print $submitted; ?>
</div>
<?php endif; ?>
<div class="content clearfix"<?php print $content_attributes; ?>>
<?php
// We hide the comments and links now so that we can render them later
hide($content['comments']);
...
D7
node.tpl.php
28. Puntos flojos de D7: Sintaxis
Una única manera, mucho más fácil y reducida!
●
Mostrar variables: {{ title }}
●
Procesar: {% if not page %}
●
Comentar: {# That's just comment #}
29. Puntos flojos de D7: Inconsistencia
Si queremos modificar el marcado en Drupal
7 tenemos dos opciones:
●
Mediante TPL
●
Mediante funciones theme()
30. Puntos flojos de D7: Inconsistencia
D7
node.tpl.php
<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?>
clearfix"<?php print $attributes; ?>>
<?php print render($title_prefix); ?>
<?php if (!$page): ?>
<h2<?php print $title_attributes; ?>>
<a href="<?php print $node_url; ?>"><?php print $title; ?></a>
</h2>
<?php endif; ?>
<?php print render($title_suffix); ?>
<?php if ($display_submitted): ?>
<div class="meta submitted">
<?php print $user_picture; ?>
<?php print $submitted; ?>
</div>
<?php endif; ?>
<div class="content clearfix"<?php print $content_attributes; ?>>
<?php
// We hide the comments and links now so that we can render them later.
hide($content['comments']);
...
31. Puntos flojos de D7: Inconsistencia
D7
node.pages.inc
función theme_node_preview()
39. Puntos flojos de D7: Complejidad
En Drupal 8 se trata de reducir esta
complejidad:
–
–
–
Pensando en casos de uso
Pensando en lo que el usuario (themer)
necesita.
Meta Issue: http://dgo.to/2004872
40. Puntos flojos de D7: Complejidad
¿De qué manera?
–
1: Build the theme registry
–
2: Callback suggestions alter
–
3: Prepare variables
–
4: Alter variables
–
5: Render output
–
Meta Issue: http://dgo.to/2004872
41. Puntos flojos de D7: Redundancia
Cuando generamos varios TPL repetimos líneas
de código:
●
node--type.tpl.php
●
node--article.tpl.php
●
node--book.tpl.php
●
node--whatever.tpl.php
●
...
42. Puntos flojos de D7: Redundancia
Twig permite extender una plantilla,
heredando sus líneas y modificando
solamente las diferencias.
43. Puntos flojos de D7: Redundancia
Ejemplo comment-wrapper.html.twig
<section id="comments" class="{{ attributes.class }}"{{ attributes }}>
{% if comments %}
{% codeblock title %}
{{ title.prefix }}
<h2 class="title">{{ 'Comments' | t }}</h2>
{{ title.suffix }}
{% endcodeblock %}
{{ comments }}
{% endif %}
{% if form %}
<h2 class="title comment-form">{{ 'Add new comment' | t }}</h2>
{{ form }}
{% endif %}
</section>
44. Puntos flojos de D7: Redundancia
Ejemplo de otra plantilla
(comment-wrapper-forum.html.twig)
extendiendo la anterior.
{% extends "comment-wrapper.html.twig" %}
{% codeblock title %}
{# code block here is empty to prevent title from showing on forums #}
{% endcodeblock %}
45. Puntos flojos de D7: Redundancia
Existe un problema «parecido »con las
funciones actuales de theme:
46. Puntos flojos de D7: Redundancia
Solución D8: Render arrays con herencia
de sugerencias de templates.
47. Puntos flojos de D7: Seguridad
Posibilidad de usar PHP en las plantillas para:
●
Imprimir cualquier tipo de valor...
<?php print $node->field_whatever['und'][0]['value']; ?>
●
Realizar llamadas MySQL...
<?php db_query(“DROP TABLE {node}”); ?>
...
48. Puntos flojos de D7: Seguridad
Solución D8: usar Twig para tratar la
información en la presentación y sanear
previamente las variables para restringir los
posibles datos a mostrar:
<h3>{{ node.field_whatever.0 }}</h3>
54. Nuevos theme hooks
NUEVO! hook_prepare() o
hook_theme_prepare_TEMPLATE_SUGGESTION()
hook
theme
hook
hook
theme
suggestions
theme
suggestions
alter
hook
prepare
hook
prepare
alter
(preprocess)
default
template
drupal
render
template
override
hook
theme
registry
theme
registry
alter
* Todavía en progreso http://dgo.to/2004872
55. Nuevos theme hooks
NUEVO! hook_prepare_alter() o
hook_theme_prepare_TEMPLATE_SUGGESTION_alter()
hook
theme
hook
hook
theme
suggestions
theme
suggestions
alter
hook
prepare
default
template
hook
prepare
alter
(preprocess)
drupal
render
template
override
hook
theme
registry
theme
registry
alter
Parecido a
hook_form_alter and hook_form_FORM_ID_alter
●
*Todavía en progreso
56. En D8 todo es «renderizable»
●
●
●
Todo es una Array hasta el final.
Se evita tener que bucear (regexp) en variables
de tipo string para alterar el marcado.
El marcado está en las plantillas SOLAMENTE
57. Traducción de cadenas en plantillas
●
●
http://dgo.to/2047135
Implementación de t() y format_plural() con
contexto incluido:
{% set count = comments|length %}
{% trans %}
{{ count }} comment was deleted successfully.
{% plural count %}
{{ count }} comments were deleted successfully.
{% endtrans %}
{% trans with {'context': 'Long month name', 'langcode': 'fr'} %}
January
{% endtrans %}
58. «Theme Component Library»
●
●
Crear un set «standard» de plantillas base para
todos los elementos comunes.
Ahora mismo es una lista de deseos
http://dgo.to/1804488
59. Conclusiones
●
Con D8 los themers seremos felices (¡al
fin!)
●
...
●
We need Drupal Heros!
●
http://dgo.to/1921610
●
http://dgo.to/1757550
●
Ask to @rteijeiro