On Github lauriii / theme-system-dcbaltics-2015
Drupal 7
<?php
$variables['list'] = theme('item_list', array(
'items' => $items,
));
Drupal 8
<?php
$variables['list'] = [
'#theme' => 'item_list',
'#items' => $items,
];
<?php
/**
* Implements hook_preprocess_HOOK() for node templates.
*/
function MYTHEME_preprocess_node(&$variables) {
$variables['theme_hook_suggestions'][] = 'node__' . 'first_suggestion';
$variables['theme_hook_suggestions'][] = 'node__' . 'more_specific_suggestion';
}
<?php
/**
* Implements hook_theme_suggestions_HOOK_alter() for node templates.
*/
function MYTHEME_theme_suggestions_node_alter(array &$suggestions, array $variables) {
$suggestions[] = 'node__' . 'first_suggestion';
$suggestions[] = 'node__' . 'more_specific_suggestion';
}
<?php
/**
* Implements hook_preprocess_node().
*/
function hook_preprocess_node(&$variables) {
// This will be run as first.
}
/**
* Implements hook_preprocess_node__article().
*/
function hook_preprocess_node__article(&$variables) {
// This will be run as second.
}
<?php
/**
* Implements hook_preprocess_node().
*/
function hook_preprocess_node(&$variables) {
// Add new class.
$attributes->addClass('my-own-class');
// Remove elements default class.
$attributes->removeClass('default-class');
// Set elements id to kitten.
$attributes->setAttribute('id', 'kitten');
}
<div{{ attributes }}>
<div {{ attributes }}>
<div✖{{ attributes }}>
{%
set classes [
'a-lot-of',
'classes-needed',
'for-this-element'
]
%}
<div{{ attributes.removeClass('my-own-class').addClass('better-class', classes) }}>
{{ sandwich.cheese }}
// Array key.
$sandwich['cheese'];
// Object property.
$sandwich->cheese;
// Also works for magic get (provided you implement magic isset).
$sandwich->__isset('cheese'); && $sandwich->__get('cheese');
// Object method.
$sandwich->cheese();
// Object get method convention.
$sandwich->getCheese();
// Object is method convention.
$sandwich->isCheese();
// Method doesn't exist/dynamic method.
$sandwich->__call('cheese');
Drupal 7
<?php
// We hide the comments and links now so that we can render them later.
hide($content['comments']);
hide($content['links']);
print render($content);
Drupal 8
{# We give you what you ask for. #}
{{ content|without('comments', 'links') }}
Meant to manipulate content. Simply takes the first parameter from a variable or inline string and returns it in different format.
{% set name = 'Lauri' %}
{# Print varibale using lenght filter. #}
{{ name|length }}
5
More viable functions with multiple parameters meant to create simple front-end logic
{{ dump() }}
However I'm going to show you how to add these by your self!
<?php
/**
* A class providing my own Twig extension.
*/
class TrimString extends \Twig_Extension {
/**
* {@inheritdoc}
*/
public function getFilters() {
return [
new \Twig_SimpleFilter('trim_string', array($this, 'trimString')),
];
}
public function trimString($string, $length = 10) {
return strpos($string, 0, $length);
}
}
services:
trim_string.twig.trimstring:
class: Drupal\trim_string\TwigExtension\TrimString
arguments: ['@renderer']
tags:
- { name: twig.extension }
trim_string.services.yml
{% set variable = 'my text' %}
{{ variable|trim_string(2) }}
my
<?php
/**
* A class providing my own Twig extension.
*/
class MyTwigExtension extends \Twig_Extension {
/**
* {@inheritdoc}
*/
public function getFunctions() {
return [
new \Twig_SimpleFunction('url', [$this, 'getUrl'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']),
];
}
}
# engine: phptemplate
themename.info.yml
<?php
print drupal_render([
'#markup' => '<script>alert('xss')</script>',
]);
Will print as:
<script>alert('xss')</script>
<?php
$render = [
'#markup' => '<em>some markup</em>',
'#allowed_tags' => ['em'],
];
<?php
$possible_unsafe_var = 'some markup';
$render = [
'#type' => 'inline_template',
'#template' => '<em>{{ var }}</em>',
'#context' => [
'var' => $possible_unsafe_var,
],
];