0

I created a new article content and am overriding that by creating a template for it (ie: node--4.html.twig).

Then inside this template, I want to loop through all nodes that belong to article content type; and to achieve that, I created my .theme file and added the following code based on the answer for this question here:

<?php
  function capoeira_preprocess_page(&$variables) {
  $articles = \Drupal::entityQuery('node')->condition('type', 'article')->execute();
  $nodes = \Drupal\node\Entity\Node::loadMultiple($articles);  
  $variables['articles'] = $nodes;
}

Inside the template I do have:

{{ kint(articles) }}

inside my template and I go refresh the page I show article as NULL.

How do I loop through the articles?

EDITED:

None of the below work:

function capoeira_preprocess_page(&$variables) {
  $articles = \Drupal::entityQuery('node')->condition('type', 'article')->execute();
  $nodes = \Drupal\node\Entity\Node::loadMultiple($articles);
  $variables['name'] = $nodes;
}

function capoeira_preprocess_node(&$variables){
  $variables['name'] = 'My name comes here';
}

function capoeira_preprocess_node_article(&$variables){
  $variables['name'] = 'My name comes here';
}
8
  • Leaving out the fact a theme should not have code that queries entities, as that is code for a module, did you check the theme to which you added that code is the theme used for the page? Are you using the correct machine name in those hooks? The fact you show a function with a name starting with scottishleader_ and two with a name starting with capoeira_ makes me wonder you are prefixing the function names with the wrong theme machine name.
    – avpaderno
    Commented Oct 3, 2019 at 7:32
  • It's also not clear why, for every page Drupal shows with that theme, you would show a list of nodes or data obtained from that list of nodes.
    – avpaderno
    Commented Oct 3, 2019 at 7:42
  • @kiamlaluno , the themename is capoeira, inside it, I have a file named capoeira.theme, which has the preprocess functions. I want to add the functionality for my node--4.html.twig template, which is used to show all the news(news are from article content type). Hope I could explain better Commented Oct 3, 2019 at 7:49
  • You should also show the code you are using in the template file to loop through the list of nodes. If that code is wrong, we cannot tell you how to fix it without seeing it.
    – avpaderno
    Commented Oct 3, 2019 at 7:53
  • Then, as usual, if you are editing the code of an enabled theme, you need to first clear Drupal cache, or Drupal will not notice there are new hooks in the code. That, or first disable the theme, before adding new hooks.
    – avpaderno
    Commented Oct 3, 2019 at 7:55

1 Answer 1

0

you can use

function YOURTHEME_preprocess_node_article(&$variables){
  $node = $variables['node'];
   $nid = $node->id();
   // similarly you can fetch other fields
}

then override article content type as node--article.html.twig and pass data to twig file

3
  • I initially had HOOK_preprocess_node and the result was the same. I now tried to modify the template name as per your answer but still no success. Commented Oct 3, 2019 at 5:48
  • @deSousa see updated answer
    – mintra
    Commented Oct 3, 2019 at 5:57
  • I just edited my question. My understanding is that you choose the name after "preprocess" based on the template name in usage, whether its a page, node or node type. If so is true, then using soccer_preprocess_node should work because my template name is node--4.html.twig, which redundantly speaking its node. Anyway, nothing is working for some odd reason. Commented Oct 3, 2019 at 6:08

Your Answer

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

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