Using Drupal 8.
I want to display the value of a field (field_date) at the top of my page, in the header, so I need to output it in page.html.twig. I'm having issue with this, though, which I think boil down to the scope of where variables are available.
To clarify - I can print field_date in node.html.twig no problem by just using content, but since I need it so high on the page, I'm hoping for a way to print it in page.html.twig.
I'm open to any solution, but here's a possible solution I picked up from searching on this same topic:
In THEMENAME.theme
/**
* Implements template_preprocess_field().
*/
function THEMENAME_preprocess_field(&$variables) {
$element = $variables['element'];
if($element['#field_name'] == 'field_date') {
$variables['promo_date'] = $element[0]['#markup'];
dpm($variables['promo_date']);
}
}
In page.html.twig:
<div class="theme-date">{{ promo_date }}</div>
In this example, the dpm does work, but the promo_date displays nothing. Doing kint(node) returns a large number of methods, but none seem to be the fields I need. Unfortunately the solution I borrowed from was taking a page-level variable and adding it to field.twig.html, and I think the scope reversal is the problem.