5

How Drupal 8 uses Twig, how different data is transferred from general template to specific templates?

For view block I have created block template as per default template suggestions. In my new Twig template if I print {{ content }} everything (view result fields) are getting rendered as per Drupal default.

How can I access and apply my theme markup to all view fields in my block template. {{ dump(_context|keys) }} display below.

enter image description here

enter image description here

2 Answers 2

10

In block--views-block--page-fields-block-1.html.twig

You can access the view fields like this:

{% for row in content['#view'].result %} {{ content['#view'].field['field_name'].value(row) }} // change field_name to the actual field machine name. {% endfor %} 

Personally, I rather use views-view-fields--[view-name]--[machine-name].html.twig

In your case should be views-view-fields--page-fields--block-1.html.twig

Here you do not need to do a for loop and you can access the fields easily with:

{{ fields.field_name.content }} // change field_name to the actual field machine name. 
5
  • Now how can i get field_image url with first approach, currently getting file id Commented Nov 26, 2018 at 13:30
  • @harish use the Image URL Formatter module. Then in views click on your image field and set the formatter to URL. Commented Nov 26, 2018 at 23:39
  • That i have in D8 without any module, in dashboard view results i get image file url, but in view template your above twig syntax gives numeric value ( target file_id ) need to find how to get entity and then uri from this numeric file id Commented Nov 27, 2018 at 5:24
  • How can i get header and footer for view Commented Nov 29, 2018 at 5:56
  • @NoSssweat thanks for ans but i want URL value with same code above {{ content['#view'].field['field_name'].value(row) }} Commented Apr 22, 2021 at 6:05
1

If you want to render the block into your custom template:

  1. Create your twig file block--views-block-page-fields-block-1.html.twig inside templates folder in your custom module.

  2. implement hook_theme like the following:

  /** * Implements hook_theme(). */ function yourModuleName_theme($existing, $type, $theme, $path) { return [ 'block__views_block_page_fields_block-1' => [ 'base hook' => 'block' ], ]; }  
  1. Clear cache.
  2. Now you have content variable.

If you use Twig Tweak you can use drupal_view_result to get view result, so you can access to the fields like:

{% for result in drupal_view_result(content['#view'].id, content['#view'].current_display) %} {{ result._entity.field_name.value }} {% endfor %} 
6
  • Thanks for your answer....I am having access to content variable without hook_theme, My question is how can i access specific fields in my template using content. e.g. field_title, field_field_slides from this content variables as i shared in image none of key in content taking me to view fields Commented Nov 24, 2018 at 17:15
  • I want something information that i can traverse through content variable to get my view fields .... don't i have access them in this block template ? Commented Nov 24, 2018 at 17:18
  • The question wasn't about rendering a block... was about how to access view field values. Commented Nov 26, 2018 at 9:47
  • Thanks ..Twig tweak approach is also working..Good that i could easily get image source for image field result._entity.field_image.entity.fileuri Commented Nov 26, 2018 at 17:28
  • You are welcome, i'm glad that help. Commented Nov 26, 2018 at 17:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.