Skip to content

Instantly share code, notes, and snippets.

@zerolab
Last active March 17, 2017 13:26
Show Gist options
  • Save zerolab/6e0574c82c29447d807dac97e3559db6 to your computer and use it in GitHub Desktop.
Save zerolab/6e0574c82c29447d807dac97e3559db6 to your computer and use it in GitHub Desktop.
Drupal 8 pass data down ER fields
<?php
/**
* @file
* An example of how to pass your custom variable to the first item in a multi-value
* entityreference field.
*/
/**
* Implements hook_preprocess_field().
* Change `field_name` to your field machine name.
*/
function example_preprocess_field__field_name(&$variables) {
$variables['items'][0]['content']['#my_custom_var'] = 'foobar!';
}
/**
* Implements hook_preprocess_node().
*/
function example_preprocess_node(&$variables) {
if (isset($variables['elements']['#my_custom_var'])) {
$variables['foo'] = $variables['elements']['#my_custom_var'];
}
}
{#
// .. usual template stuff
#}
<article>
{% if my_custom_var %}
Got {{ my_custom_var }}!
{% else %}
{{ content }}
{% endif %}
</article>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment