Skip to main content

8 - Get custom comments of node

Source Link

8 - Get custom comments of node

I'm currently working on a simple reviewing system. I've created a custom comments type ("Review") with an integer list ("Rating") that can be added to my nodes. Now, while displaying the note, I would like to get an average rating based on the reviews posted.

I am quite new to Drupal, and I thought that I might achieve this by creating a custom twig template for the product-type nodes and getting the required value in a preprocess function.

I succeeded in getting the field_reviews object with

$variables['comment_field'] = $node->get('field_reviews');  

and when I kint()ed it out, I got a really big file structure, but the required data was in there.

However, whenever I tried following the path down there through dozens of variables and arrays, when I reached the review itself, kinting yielded NULL or the page didn't load at all.

I'm getting the feeling that this approach is not ideal and that I must've overlooked something much easier, but I can't seem to figure out what.