Drupal 8 snippets

Brug term som body class

<?php
/**
 * Prepares variables for the html.html.twig template.
 */

use Drupal\taxonomy\Entity\Term;
use
Drupal\Component\Utility\Html;

function
THEME-NAVN_preprocess_html(&$variables) {

  if (
$node = \Drupal::routeMatch()->getParameter('node')) {

   
/**
     * Get the term id from the 'field_section' taxonomy field.
     * If greater than zero, load the term and acquire the name value
     * as a css safe value.
    */
if ($node->hasField('field_DIT-FELT')) {
   
$tid = $node->get('field_DIT-FELT')->target_id;
    if(
$tid > 0) {
     
$term_object = Term::load($tid);
     
$variables['attributes']['class'][] = 'rce-' . Html::getClass($term_object->get('name')->value);
    }
  }
}
}
?>

Path to theme

{{ directory }}

<img src="/{{ directory }}/img/mit-billede.png" />