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?

  • 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. – Rick Bergmann Oct 29 at 23:02
  • @RickBergmann, View > Devel gives a "link_flag" field. {{ rows[i]['content']['#row']._entity.link_flag.value }} is not producing anything. – augusto Oct 30 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. – Rick Bergmann Oct 30 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. – Rick Bergmann Oct 30 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. – Rick Bergmann Nov 12 at 23:41
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.

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.