0

I have a view of contents of a content type. This content type has fields which references entities of another content type (and that in turn references another set of entities of yet another content type).

I am trying to iterate through this view with twig templates.

If I choose to show contents for my format, I can easily access the referenced entities and its fields. My quandary is however, I cannot track what row.content row/index I am in.

If I switch to show fields, I can use the global view results count and I have an effective counter. However, I cannot seem to be able to access the referenced entities to get the values of the fields.

So far I have not found anything thru searching the net 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.

Now there are two possible solutions to this issue:

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

Anyone have any suggestions? Thank you.

  • This seems like the wrong approach, you should adjust your css/js to the nested data. – 4k4 Feb 1 '18 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. – Chris Howell Feb 1 '18 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. – 4k4 Feb 1 '18 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 Feb 4 '18 at 13:46
-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

-1

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.

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.