0

I have a view showing the nodes of a content type which has reference entity fields to another content type.

I am trying to iterate through this view with templates.

If I choose to show nodes for my format, I can easily access the referenced entities and its fields, but I cannot track in which row.content I am.
If I switch to show fields, I can use the global view results count and I have an effective counter. However, I cannot access the referenced entities to get the values of the fields.

So far I have not found anything that actually addresses this particular problem. I am using the views-view-unformatted template which in turn calls the node--view template. I am unable to "pass" along the row index from the unformatted views template to node view template.

I already know that this is doable when using fields, but fields is out of the question because of the inability to access referenced entities.

These are possible solutions to this issue:

  • Show content and somehow track the row.content index between twig templates
  • Show fields to be able to actually access the fields of referenced entities

Does anyone have any suggestion?

4
  • This seems like the wrong approach, you should adjust your css/js to the nested data.
    – 4uk4
    Commented Feb 1, 2018 at 19:07
  • I'm sorry but that makes absolutely no sense at all. There is definitely an issue with fields in a view that cannot access child entities as easily as a content can. Commented Feb 1, 2018 at 21:46
  • 2
    Referenced entities are rendered on their own. You can only control the rendering result by specifying a view mode. This is not an issue, this is by design, because entities are cached and can be reused in different places. So if you deal with nested entities, you have to nest your css/js selectors as well. If you switch Views to show fields then you get a powerful tool to build sql queries, which deals with referenced entities by joining related tables, which somehow flattens the nested structure, but can get quite messy if you have a lot of referenced entities.
    – 4uk4
    Commented Feb 1, 2018 at 22:10
  • "I already know that this is doable when using fields, but fields is out of the question because of the inability to access referenced entities." - This doesn't seem accurate to me, although I can't fact check at the moment. Have you tried playing with relationships on your views, via the entity reference fields?
    – rooby
    Commented Feb 4, 2018 at 13:46

2 Answers 2

0

Ok, so I finally solved my problem. I went with 'solution #1' in the original question. I created a single view showing content with unformatted list format and then triplicated that. Here's the template set up I followed:

page.html.twig
views-view--page.html.twig
views-view-unformatted.html.twig
node--view.html.twig

page--page-url.html.twig contained the main html layout template. Within this page, I used twig_tweak's drupal_view('viewname', 'viewmode', parameter) where I wanted the specially formatted view to appear. I had three different view formats. So, in page--page-url.html.twig, it looked something like this:

drupal_view('beta', 'page_1', parameter) << node--view--beta--page_1.html.twig
<html code>
drupal_view('beta', 'page_2', parameter) << node--view--beta--page_2.html.twig
<more html code>
drupal_view('beta', 'page_3', parameter) << node--view--beta--page_3.html.twig
<rest of html code>

The node--view--beta--page_1.html.twig is where the view html was formatted per row.

To handle the row_index, I used the preprocess function to find it for me. In Drupal 8, row_index is not available in the view itself (it should be, but it is null). So, each time node--view is called, the corresponding preprocessor is executed and it iterates through the view nodes to find the matching nid and hence, the row index:

function theme_name_preprocess_node__view__beta(&$vars) {
    $view = $vars['view'];
    $node = $vars['node'];
    foreach($view->result as $key => $row){
        if($row->nid == $node->id()){
            $vars['row_index'] = $row->index;
        }
    }
}

In the html.twig, I simply used {{ row_index }} where needed.

Hope this helps anyone else in similar situation.

1
  • This is great, thanks for sharing. A small thing however, $vars['view'] isn't available so you should try $view = views_get_current_view();
    – tostinni
    Commented Sep 7, 2021 at 2:12
-1

Possibly use devel and kint modules.

Once both modules are enabled add {{ kint(fields) }} or {{ kint(content) }} to see what your options are for displaying your view's data. If the view wrks successfully then you should be able to display it using twig e.g. if you find field_name in kint and it output's the correct data then add <p>{{ fields.field_name }}</p> to your template

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.