0

I am having issue with displaying one field twice in my template having different display modes.

I have a content type basic page with field field_gallery (which is entity reference on paragraph). In template node--page--full.html.twig I want to render field_gallery twice. Once using default display mode and second using teaser.

<div class="container">
    {# this as teaser #}
    {{ content.field_gallery }} 

    {# this as default or whatever #}
    {{ content.field_gallery }}
</div>

I tried do it using hook_preprocess_node, but could not achieve the result I want.

$variables['custom_display_mode'] = $variables['node']->get('field_gallery')
    ->view(array(
       'label' => 'hidden',
       'type' => 'teaser'
));

I do not want to use any custom module, I'm kinda new to drupal hooks and i'm sure that it must be possible somehow, to set custom variable and print it in my template.

Thanks.

0

An array with 'type' => 'teaser' is specifying a field formatter, but you probably don't have a field formatter with this name.

You need to add the view mode (display mode) as string argument to view():.

$variables['custom_display_mode'] = $variables['node']->get('field_gallery')
  ->view('teaser');

For more info see How to apply a Field Formatter to a node field programmatically?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.