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';
}
scottishleader_
and two with a name starting withcapoeira_
makes me wonder you are prefixing the function names with the wrong theme machine name.