Skip to main content
Warn users to do it as request by OP
Source Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

Normally you'dPutting this in page.html.twig is a bad idea. Since this template will be used for every page rendered from Drupal using that theme.

Instead you should simply create a View. Views can be used to query a certain content type and to list nodes in a certain view mode or just certain fields of these nodes (the title and a color field for thatexample). At bestThen configure your view to provide a block. And then and place that block in the region you wanna have the Flowers printed. With a view

Views and block will take care of the caching. And you are much more flexible latercan use the block's visibility settings to have it displayed only on paths or nodes where you really need it.

 

But to do it withoutIf you still insist on coding this in a viewtemplate (not recommended, too performance-heavy, too much logic in templates) you first have to pass all Flower nodes to the template. So in your MYTHEME.theme put:

/**
 * Implements template_preprocess_page().
 */
function MYTHEME_preprocess_page(&$variables) {
  
  $query = \Drupal::entityTypeManager()->getStorage('node')->getQuery();

  // Get all Flower node IDs.
  $nids = \Drupal::entityQuery('node')
    $query->condition('type', 'flower')
    ->execute();

  // Load all Flower nodes.
  $nodes = \Drupal\node\Entity\Node::loadMultiple($nids);

  // Pass them to page.html.twig.
  $variables['flowers'] = $nodes;
}

Then in your page.html.twig do:

{% for flower in flowers %}
  {{ flower.title.value }}
  {{ flower.field_color.value }}
{% endfor %}

Normally you'd simply create a view for that. At best a block. And then place that block in the region you wanna have the Flowers printed. With a view you are much more flexible later on.

But to do it without a view you first have to pass all Flower nodes to the template. So in your MYTHEME.theme put:

/**
 * Implements template_preprocess_page().
 */
function MYTHEME_preprocess_page(&$variables) {
  
  // Get all Flower node IDs.
  $nids = \Drupal::entityQuery('node')
    ->condition('type', 'flower')
    ->execute();

  // Load all Flower nodes.
  $nodes = \Drupal\node\Entity\Node::loadMultiple($nids);

  // Pass them to page.html.twig.
  $variables['flowers'] = $nodes;
}

Then in your page.html.twig do:

{% for flower in flowers %}
  {{ flower.title.value }}
  {{ flower.field_color.value }}
{% endfor %}

Putting this in page.html.twig is a bad idea. Since this template will be used for every page rendered from Drupal using that theme.

Instead you should simply create a View. Views can be used to query a certain content type and to list nodes in a certain view mode or just certain fields of these nodes (the title and a color field for example). Then configure your view to provide a block and place that block in the region you wanna have the Flowers printed.

Views and block will take care of the caching. And you can use the block's visibility settings to have it displayed only on paths or nodes where you really need it.

 

If you still insist on coding this in a template (not recommended, too performance-heavy, too much logic in templates) you first have to pass all Flower nodes to the template.

/**
 * Implements template_preprocess_page().
 */
function MYTHEME_preprocess_page(&$variables) {
  
  $query = \Drupal::entityTypeManager()->getStorage('node')->getQuery();

  // Get all Flower node IDs.
  $nids = $query->condition('type', 'flower')->execute();

  // Load all Flower nodes.
  $nodes = \Drupal\node\Entity\Node::loadMultiple($nids);

  // Pass them to page.html.twig.
  $variables['flowers'] = $nodes;
}

Then in your page.html.twig:

{% for flower in flowers %}
  {{ flower.title.value }}
  {{ flower.field_color.value }}
{% endfor %}
Source Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

Normally you'd simply create a view for that. At best a block. And then place that block in the region you wanna have the Flowers printed. With a view you are much more flexible later on.

But to do it without a view you first have to pass all Flower nodes to the template. So in your MYTHEME.theme put:

/**
 * Implements template_preprocess_page().
 */
function MYTHEME_preprocess_page(&$variables) {
  
  // Get all Flower node IDs.
  $nids = \Drupal::entityQuery('node')
    ->condition('type', 'flower')
    ->execute();

  // Load all Flower nodes.
  $nodes = \Drupal\node\Entity\Node::loadMultiple($nids);

  // Pass them to page.html.twig.
  $variables['flowers'] = $nodes;
}

Then in your page.html.twig do:

{% for flower in flowers %}
  {{ flower.title.value }}
  {{ flower.field_color.value }}
{% endfor %}