Last active
March 17, 2017 13:26
-
-
Save zerolab/6e0574c82c29447d807dac97e3559db6 to your computer and use it in GitHub Desktop.
Drupal 8 pass data down ER fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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']; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# | |
// .. 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