0

I have a views-view-unformatted.html.twig template working fine where I can print fields from a view of nodes.

Example: having i = 3, print row number 3 title

{{ rows[i]['content']['#row']._entity.title.0.value }}

This view has also a working relationship with flag. I see it working in the view preview.

How can I print the flag link inside views-view-unformatted.html.twig?

6
  • Have you tried {{ rows[i]['content']['#row']._entity.flag_field_name.value }}? Replace flag_field_name with the actual field name of the flag field. Commented Oct 29, 2019 at 23:02
  • @RickBergmann, View > Devel gives a "link_flag" field. {{ rows[i]['content']['#row']._entity.link_flag.value }} is not producing anything.
    – Augusto
    Commented Oct 30, 2019 at 9:43
  • It's difficult to tell without seeing the render array. Try to use {{ dump( rows[i]['content']['#row']._entity) }} that should list all the fields in the render array, then you can drill into the fields, and the link_flag should be listed there. Fields are usually prefixed with field_. Maybe paste the output of the dump in the question if it's not too long. Commented Oct 30, 2019 at 14:08
  • Edit: I see that flag does not prefix with field_. I am using the flag module in 1 of my projects. I will try to replicate this case in my project and post when I find something. Commented Oct 30, 2019 at 14:24
  • 1
    Looks like there could be something renderable in the values array protected values -> array(10). I tried to reproduce this but I also find it more difficult to get flag values. Usually I make view blocks and render them in twig instead of using the view-unformatted template, and I use twig block template overrides to render the fields / content in the view. I can also suggest asking for a support request in the flag project page on Drupal.org, maybe someone can suggest another option there. Commented Nov 12, 2019 at 23:41

3 Answers 3

1

Not being able to find better solution nor documetation, I built this working hack:

  1. add a fake text field entity. IE: for articles: http://yoursite/admin/structure/types/manage/article/fields > Add field > Add text plain field

  2. hook the view in a custom module:

    function MYMODULE_views_pre_render(ViewExecutable &$view){
      if( $view->id() == 'VIEWID' ){
        foreach($view->result as $r) {
        // from https://www.drupal.org/comment/reply/295383/12504094
        $node = $r->_entity;
        $flag_link = [
          '#lazy_builder' => ['flag.link_builder:build', [
          $node->getEntityTypeId(),
          $node->id(),
          'FLAG',
        ]],
        '#create_placeholder' => TRUE,
      ];
      $flag_link = render($flag_link);
      $r->_entity->set('field_fake_flag_field', $flag_link); //.
        }
      }
    }
    
  3. print (finally) your flag link in the twig template:

    {{ rows[i]['content']['#row']._entity.field_fake_flag_field.0.value }}

Twig is cool. It takes a while, sometimes.

0

I used {{ fields.link_flag.content }} in a views-view-fields--some_view_name.html.twig and that worked for me

0

I have found this

{{ view.style_plugin.getField(KEY, 'FIELD_NAME') }}

at this link https://dharmeshsavaliya.wordpress.com/2020/03/26/accessing-views-row-fields-inside-views-view-unformatted-html-twig-in-drupal-8/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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