View / Twig Extension : Display other items with the same field value I work on a view block which displays a list of parts (content Type : parts), here are simplified examples of parts and fields :


part_ref || part_id
ABCD || 10
BCDE || 11
CDEF || 11
DEFG || 11
EFGH || 12


In my View Filters, only one part is displayed, filtered by "part_ref".
What I would like to do is to display other parts with the same "part_id".

For example, "BCDE" is displayed, I would like to display also "CDEF" and "DEFG".

I have created a Twig Extension in order to do something like :
{{ displayRelated(11) }}

But inside my function "displayRelated", Idon't know how to get parts with the passed "part_id".

Does someone have a link or something to guide me ?
I cannot find anything on google.

Or maybe it's not the right approach ?

Thanks in advance.
I finally found a solution with a function in my Twig Extension.
I don't know if it's a clean way to do it but it works.

The function is called from the twig file :

{% set parts = partsSameId(part_id) %}

This is the simplified code inside the twig extension php file :

&lt;?php


public function partsSameId($part_id) {

    // Get nodes with the same part_id
    $nids = \Drupal::entityQuery('node')
      -&gt;condition('type','my_custom_content_type')
      -&gt;condition('my_custom_field_for_part_id',$part_id)
      -&gt;execute();

    // return array
    $html = [];

    // For each node returned, get the title.
    $i = 0;

    foreach ($nids as $the_node){
      $node = \Drupal::entityTypeManager()-&gt;getStorage('node')-&gt;load($the_node);
      $part_name = $node-&gt;getTitle();

      // return array
      $html[$i]['part_name'] = $part_name;

      $i++;
    }

    return $html;
  }