Zum Inhalt springen

Twig: Unterschied zwischen den Versionen

Aus Computech CMS Wiki
Simon Zeberli (Diskussion | Beiträge)
 
(24 dazwischenliegende Versionen von 3 Benutzern werden nicht angezeigt)
Zeile 5: Zeile 5:
==Twig Drupal8 Module==
==Twig Drupal8 Module==
* https://www.drupal.org/project/twig_tweak
* https://www.drupal.org/project/twig_tweak
==Twig Parameter==
==Twig Schnipsel==
{| class="wikitable"
[https://blog.usejournal.com/getting-drupal-8-field-values-in-twig-22b80cb609bd Diverse Twig Schnipsel]
{| class="wikitable sortable"
|-
|-
! Was!! Twig
! Was!! Twig
|-
|-
| Variable übersetzen|| <syntaxhighlight lang="twig">{{ 'Read more'|trans }}</syntaxhighlight>
| Variable übersetzen || <syntaxhighlight lang="twig">{{ 'Read more'|trans }}</syntaxhighlight>
|-
|-
| Beispiel || Beispiel
| For Schleife || <syntaxhighlight line='line' lang="twig"> {% for val in array %}
    {{ val }}
{% endfor %}</syntaxhighlight>
|-
|-
| Beispiel || Beispiel
| Array enthält || <syntaxhighlight line='line' lang="twig">{% if "wert" in array %}</syntaxhighlight>
|-
| Array enthält nicht || <syntaxhighlight line='line' lang="twig">{% if "wert" not in array %}</syntaxhighlight><syntaxhighlight line='line' lang="twig">{% if content.field_slider_titel|render or content.field_slider_text|render is not empty %}</syntaxhighlight>
|-
| Odd & Even || <syntaxhighlight line='line' lang="twig">
    {% for row in rows %}
      {% set parity = cycle(['odd', 'even'], loop.index0) %}
      <tr {{ row.attributes.addClass(parity) }}>
</syntaxhighlight>
|}
 
==Field Value==
{| class="wikitable sortable"
|-
! Field Type !! Twig
|-
| image ||  <syntaxhighlight lang="twig">{{  }}</syntaxhighlight>
|-
| dropdown ||  <syntaxhighlight lang="twig">{{ content.field_slider_text_position['#items'].getString() }}</syntaxhighlight>
|-
| image URI||  <syntaxhighlight lang="twig">{{  paragraph.field_slider_bild.0.entity.uri.value }}</syntaxhighlight>
|}
|}


Zeile 23: Zeile 46:
Node / Kontakt / Views / Webform in einem Modal öffnen: (mit Breitenanpassung)
Node / Kontakt / Views / Webform in einem Modal öffnen: (mit Breitenanpassung)
<syntaxhighlight line='line' lang="html5">
<syntaxhighlight line='line' lang="html5">
<a class="use-ajax" data-dialog-options="{&quot;width&quot;:800}" data-dialog-type="modal" href="/contact">Contact US</a>
<a class="use-ajax" data-dialog-options="{&quot;width&quot;:420,&quot;title&quot;:&quot;Kontakt Info&quot;}" data-dialog-type="modal" href="/contact">Contact US</a>
</syntaxhighlight>
</syntaxhighlight>
hook_preprocess_page in die.theme-Datei einfügen um die modal-library anzuhängen:
hook_preprocess_page in die.theme-Datei einfügen um die modal-library anzuhängen:
Zeile 46: Zeile 69:
</syntaxhighlight>
</syntaxhighlight>
URL-Parameter mitgeben (Beispiel Anmeldung mit Subject 'Anmeldung: [Titel] [Datum]'):
URL-Parameter mitgeben (Beispiel Anmeldung mit Subject 'Anmeldung: [Titel] [Datum]'):
<syntaxhighlight line='line' lang="html5">
<syntaxhighlight line='line' lang="twig">
{% set eventDateTimestamp = node.field_date.value|date('U') %}
{% set eventDateTimestamp = node.field_date.value|date('U') %}
{% set urlParams = {'subject': 'Anmeldung: ' ~ node.label ~ ' ' ~ eventDateTimestamp|date('j.n.Y') }|url_encode %}
{% set urlParams = {'subject': 'Anmeldung: ' ~ node.label ~ ' ' ~ eventDateTimestamp|date('j.n.Y') }|url_encode %}
Zeile 52: Zeile 75:
</syntaxhighlight>
</syntaxhighlight>


===Datumfeld===
<syntaxhighlight line='line' lang="twig">
{% if content.field_datum_bis|render is not empty %}
  {{ content.field_datum.0["#attributes"].datetime|date('U')|format_date('datum_von') }} bis {{ content.field_datum_bis.0["#attributes"].datetime|date('U')|format_date('author') }}
{% else %}
  {{  content.field_datum.0["#attributes"].datetime|date("U")|format_date('author')  }}
  {# {{ dump(content.field_datum.0["#attributes"].datetime )}} #}
{% endif %}
</syntaxhighlight>
==Layout==
==Layout==
===html.html.twig===
===html.html.twig===
Zeile 83: Zeile 115:
===Image Style Hardcodiert setzen===
===Image Style Hardcodiert setzen===
* einem Bild Feld - den Bildstil anpassen / Hardcodiert
* einem Bild Feld - den Bildstil anpassen / Hardcodiert
<syntaxhighlight line='line' lang="html5">
<syntaxhighlight line='line' lang="twig">
    {#{{ content.field_slider_bild }}#}
{#{{ content.field_slider_bild }}#}
    {% set image = content.field_slider_bild.0|merge({'#image_style': 'bild'}) %}
{% set image = content.field_slider_bild.0|merge({'#image_style': 'bild'}) %}
    {# {% set image = image|merge({'#image_style': 'bild'}) %}    Bildstil  #}
{# {% set image = image|merge({'#image_style': 'bild'}) %}    Bildstil  #}
    {{ image }}
{{ image }}
</syntaxhighlight>
</syntaxhighlight>
* Ein Bild, einem Bildstil einfügen -> kann für Paralax oder Hintergurnd verwendet werden.
* Ein Bild, einem Bildstil einfügen -> kann für Paralax oder Hintergurnd verwendet werden.
<syntaxhighlight line='line' lang="html5">
<syntaxhighlight line='line' lang="twig">
    {# Benötigtes Modul: https://www.drupal.org/project/twig_tweak #}
{# Benötigtes Modul: https://www.drupal.org/project/twig_tweak #}
    {# slider bild bildstile berechnen #}
{# slider bild bildstile berechnen #}
    {% set url_image_style = paragraph.field_slider_bild.0.entity.uri.value | image_style('slider_klein')  %}
{% set url_image_style = paragraph.field_slider_bild.0.entity.uri.value | image_style('slider_klein')  %}
    {# {{ url_image_style }} #}
{# {{ url_image_style }} #}
</syntaxhighlight>
 
===Vorhandene  "Image Style" auslesen===
<syntaxhighlight line='line' lang="twig">
$variables['var_image_styles'] = \Drupal::entityQuery('image_style')->execute();
</syntaxhighlight>
</syntaxhighlight>



Aktuelle Version vom 23. Juni 2021, 15:15 Uhr

Allgemein

Twig Drupal8 Module

Twig Schnipsel

Diverse Twig Schnipsel

Was Twig
Variable übersetzen
{{ 'Read more'|trans }}
For Schleife
 {% for val in array %}
    {{ val }}
{% endfor %}
Array enthält
{% if "wert" in array %}
Array enthält nicht
{% if "wert" not in array %}
{% if content.field_slider_titel|render or content.field_slider_text|render is not empty %}
Odd & Even
    {% for row in rows %}
      {% set parity = cycle(['odd', 'even'], loop.index0) %}
      <tr {{ row.attributes.addClass(parity) }}>

Field Value

Field Type Twig
image
{{  }}
dropdown
{{ content.field_slider_text_position['#items'].getString() }}
image URI
{{  paragraph.field_slider_bild.0.entity.uri.value }}

Templates

Node / Kontakt / Views / Webform in einem Modal öffnen: (mit Breitenanpassung)

<a class="use-ajax" data-dialog-options="{&quot;width&quot;:420,&quot;title&quot;:&quot;Kontakt Info&quot;}" 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';
}

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="{&quot;width&quot;:400}" href="/contact?{{ urlParams }}">Anmelden</a>

Datumfeld

{% if content.field_datum_bis|render is not empty %}
   {{ content.field_datum.0["#attributes"].datetime|date('U')|format_date('datum_von') }} bis {{ content.field_datum_bis.0["#attributes"].datetime|date('U')|format_date('author') }}
{% else %}
   {{  content.field_datum.0["#attributes"].datetime|date("U")|format_date('author')  }}
   {# {{ dump(content.field_datum.0["#attributes"].datetime )}} #}
{% endif %}

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 }} #}

Vorhandene "Image Style" auslesen

$variables['var_image_styles'] = \Drupal::entityQuery('image_style')->execute();

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.