0

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.

1 Answer 1

0

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 :

<?php


public function partsSameId($part_id) {

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

    // return array
    $html = [];

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

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

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

      $i++;
    }

    return $html;
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.