Skip to content

Instantly share code, notes, and snippets.

@ptitb
Last active July 22, 2016 11:36
Show Gist options
  • Save ptitb/c4a8a0662fc54881067c7a9e9c1c717f to your computer and use it in GitHub Desktop.
Save ptitb/c4a8a0662fc54881067c7a9e9c1c717f to your computer and use it in GitHub Desktop.
Drupal 8 *.theme file snippets for Twig template
<?php
use Drupal\image\Entity\ImageStyle;
use \Drupal\file\Entity\File;
function theme_preprocess_page(&$vars) {
if (($node = \Drupal::routeMatch()->getParameter('node')) && $node instanceof \Drupal\node\NodeInterface) {
// Get imagestyle url for use in Twig template
if(isset($node->field_header_image->target_id)) {
$path = File::load($node->field_header_image->target_id)->getFileUri();
$url = ImageStyle::load('header-style')->buildUrl($path);
} else {
$url = '';
}
$vars['header_background_image_url'] = $url;
// Get rendered field without label for use in Twig template
if (isset($node->field_header_cta_button)) {
$field_header_cta_button = $node->get('field_header_cta_button');
$vars['header_cta_button'] = $field_header_cta_button->view(array('label' => 'hidden'));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment