1

I am struggling since a couple of days with my problem now and I have no idea what else I can do to come to a stage of enlightenment.

In Drupal 8 I have a content type called "Release" and a content type "Article" which, among others, contains Reviews of Releases. So in Releases I created an entity reference field called "Review" which references Article content type.

For index pages I could create views of Articles and there I pretty easy could integrate the fields of the referencing release entity via the Relations section in views administration. There the reverse direction of a relationship is not a problem.

How do I access the fields of the release in a node--article.html.twig template?

Edited due to character limitation in the comments section (I hope this is allowed): Thank you so much, @JeremyM4an, but there was a little misunderstanding due to my bad explanation of the problem. I should have mentioned that article has a reference to a taxonomy term, called field_post_category.

So a review is an article with field_post_category == 'Review' and finally a release has an entity reference field, called field_review, referring to article. After reading your answer I tried this:

$category_field = $node->get('field_post_category');
$category_field = $category_field->getValue();
if( isset($category_field[0]['target_id']) && $category_field[0]['target_id'] ){
  $term = taxonomy_term_load($category_field[0]['target_id']);
  if($term && $term->get('name')->value == 'Reviews'){
    $release = \Drupal::entityQuery('node')
    ->condition('type', 'release')
    ->condition('field_review.entity:node.nid', $node->id())
    ->execute();
    // Load release node and store in variables.
    $variables['release'] = Node::load(array_values($release)[0]);
  }
}

But this does not work.

1 Answer 1

2

You can get the "relationships" to your referencing node in a preprocess function, in your theme's mytheme.theme file:

function mytheme_preprocess_node(&$variables) {
  if (!empty($variables['node'])) {
    $node = $variables['node'];
    if ($node->getType() === 'article') {

      // Get the review by article node id.
      $review = \Drupal::entityQuery('node')
        ->condition('type', 'review')
        ->condition('field_article', $node->id())
        ->execute();

      // Get release by review node id.
      $release = \Drupal::entityQuery('node')
        ->condition('type', 'release')
        ->condition('field_review', array_pop($review))
        ->execute();

      // Load release node and store in variables.
      $variables['release'] = Node::load(array_values($release)[0]);
    }
  }
}

Then you can access the fields in your template:

{{ release.field_name.value }}
7
  • I'm wondering if there's ever a case where this preprocessor gets called without a node in $variables['node']? Just wondering if it's overly cautious. Also I think OP only has 2 content types: Article and Release. And lastly, wondering if you'll need an 'IN' when you supply an array as a condition value. But the essence of the answer remains correct :) +1
    – Beebee
    May 14, 2020 at 19:06
  • @Beebee Thanks for the review! 1. Yes it's probably overly cautious :) 2. If I understood correctly, the OP has a release which references a review, which references an article. 3. Which condition are you referring to? I did realize I made a mistake in the field_review condition, fixing now...
    – JeremyM4n
    May 14, 2020 at 19:38
  • @JeremyM4n: My question obviously was too unclear, sorry for this: The review ist of type article with taxonomy term 'Review' (field_post_category). So I removed the 'Get the review' part of your function and changed condition for getting the release to: condition('field_review', $node->id()). But this does not work. Could you please clarify for me what I have to change? (Also see my edited question above) Thank you so much. :)
    – rockie667
    May 15, 2020 at 8:38
  • @rockie667 It looks like you have a few additional questions, outside of the original question. I would recommend keeping this question simple as how to get referenced nodes in your template, and research the mentioned functions to understand their properties and how to use them. If you still have other questions it would be better to ask in a new question, specific to that problem. If this answer solves your original question, on how to get the data in general, you can mark it as the accepted answer. Thanks!
    – JeremyM4n
    May 15, 2020 at 9:03
  • @JeremyM4n: Thanks for your comment, but I think the question is still the same (thanks to your suggested solution a bit more concrete): How do I get the referencing node when I have the node id of the current node. So, the question now is simply: Whats wrong with my condition('field_review', $node->id())?
    – rockie667
    May 15, 2020 at 9:54

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.