Zum Inhalt springen

Twig: Unterschied zwischen den Versionen

Aus Computech CMS Wiki
Patrick Itten (Diskussion | Beiträge)
Keine Bearbeitungszusammenfassung
Simon Zeberli (Diskussion | Beiträge)
Zeile 38: Zeile 38:
   // Block suggestions for custom block bundles.
   // Block suggestions for custom block bundles.
   if (isset($variables['elements']['content']['#block_content'])) {
   if (isset($variables['elements']['content']['#block_content'])) {
     array_splice($suggestions, 1, 0, 'block__bundle__' . $variables['elements']['content']['#block_content']->bundle());
     array_splice($suggestions, 1, 0, 'block__bundle__NEME' . $variables['elements']['content']['#block_content']->bundle());
   }
   }
}
}

Version vom 27. November 2018, 13:22 Uhr

Allgemein

Twig Drupal8 Module

Templates

<a class="use-ajax" data-dialog-type="modal" href="/contact">Contact US</a>

hook_preprocess_page in die.theme-Datei einfügen

function THEMENAME_preprocess_page(&$variables) {
    //custom logic of when to include the library
    // ....
    $variables['#attached']['library'][] =  'core/drupal.dialog.ajax';  

}

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__NEME' . $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.