Twig
Erscheinungsbild
Allgemein
Twig Drupal8 Module
Twig Schnipsel
| Was | Twig |
|---|---|
| Variable übersetzen | {{ 'Read more'|trans }}
|
| Beispiel | Beispiel |
| Beispiel | Beispiel |
Templates
Modal
- Module mit Modal Form bauen: https://befused.com/drupal/modal-module
Node / Kontakt / Views / Webform in einem Modal öffnen: (mit Breitenanpassung)
<a class="use-ajax" data-dialog-options="{"width":800}" data-dialog-type="modal" href="/contact">Contact US</a>
hook_preprocess_page in die.theme-Datei einfügen um die modal-library anzuhängen:
function THEMENAME_preprocess_page(&$variables) {
//custom logic of when to include the library
// ....
$variables['#attached']['library'][] = 'core/drupal.dialog.ajax';
}
Modal mit URL Parametern
URL-Parameter an Form übergeben:
function THEMENAME/MODULENAME_form_alter(&$form, $form_state, $form_id) {
// target any form provided by the contact module. If you want to target a specific form use the form ID
if (strpos($form_id, 'contact_message_') !== FALSE) {
// Set the default subject based off the subject query param.
$default_subject = \Drupal::request()->query->get('subject');
$form['subject']['widget'][0]['value']['#default_value'] = $default_subject;
}
}
URL-Parameter mitgeben (Beispiel Anmeldung mit Subject 'Anmeldung: [Titel] [Datum]'):
{% set eventDateTimestamp = node.field_date.value|date('U') %}
{% set urlParams = {'subject': 'Anmeldung: ' ~ node.label ~ ' ' ~ eventDateTimestamp|date('j.n.Y') }|url_encode %}
<a class="use-ajax" data-dialog-type="modal" data-dialog-options="{"width":400}" href="/contact?{{ urlParams }}">Anmelden</a>
Layout
html.html.twig
page.html.twig
maintenance-page.html.twig
region.html.twig
Block
Template suggestion für custom-Blocktyp erzeugen
/**
* Implements hook_theme_suggestions_HOOK_alter() for form templates.
* @param array $suggestions
* @param array $variables
*/
function THEMENAME_theme_suggestions_block_alter(array &$suggestions, array $variables) {
// Block suggestions for custom block bundles.
if (isset($variables['elements']['content']['#block_content'])) {
array_splice($suggestions, 1, 0, 'block__bundle__' . $variables['elements']['content']['#block_content']->bundle());
}
}
Content Node
Views
Field
Paragraph
Image Style Hardcodiert setzen
- einem Bild Feld - den Bildstil anpassen / Hardcodiert
{#{{ content.field_slider_bild }}#}
{% set image = content.field_slider_bild.0|merge({'#image_style': 'bild'}) %}
{# {% set image = image|merge({'#image_style': 'bild'}) %} Bildstil #}
{{ image }}
- Ein Bild, einem Bildstil einfügen -> kann für Paralax oder Hintergurnd verwendet werden.
{# Benötigtes Modul: https://www.drupal.org/project/twig_tweak #}
{# slider bild bildstile berechnen #}
{% set url_image_style = paragraph.field_slider_bild.0.entity.uri.value | image_style('slider_klein') %}
{# {{ url_image_style }} #}
Theme
THEMENAME.breakpoints.yml
THEMENAME.info.yml
THEMENAME.libraries.yml
THEMENAME.theme
preprocess function
Value beim Feld ausgeben
function THEMENAME_preprocess_node(&$variables) {
if ($variables['node']->getType() === 'NODEYPE') {
$variables['var_GESETZTE_VARIABLE'] = $variables['node']->field_FELDNAME->value;
}
}
Damit wird die Variable {{ var_GESETZTE_VARIABLE}} in jeder Twig datei aktiv.
Sprach Umschalter
in Datei THEMENAME.theme Folgenden Code einfügen:
function THEMENAME_preprocess_html(&$variables) {
//sprachumschalter classe
$variables['language'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
}
Damit wird die Variable {{ language }} in jeder Twig datei aktiv.