-
-
Save tombola/d40d59d3f93ddca796a6a1e1c6302f3f to your computer and use it in GitHub Desktop.
Drupal 8 pass data down ER fields
This file contains hidden or 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 the item number from a multi-value | |
* entityreference field to the entities concerned. | |
*/ | |
/** | |
* Implements hook_preprocess_field(). | |
* Change `field_name` to your field machine name. | |
*/ | |
function example_preprocess_field__field_name(&$variables) { | |
foreach ($variables['items'] as $key => $item) { | |
$variables['items'][$key]['content']['#field_item_number'] = $key + 1; | |
} | |
} | |
/** | |
* Implements hook_preprocess_node(). | |
*/ | |
function example_preprocess_node(&$variables) { | |
if (isset($variables['elements']['#field_item_number'])) { | |
$variables['field_item_number'] = $variables['elements']['#field_item_number']; | |
} | |
} |
This file contains hidden or 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 field_item_number %} | |
This is item number {{ field_item_number }} in the field which references it. | |
{% else %} | |
{{ content }} | |
{% endif %} | |
</article> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment