Score:0

Is there a way to render the title of every node from a single content type?

us flag
jfc

I have a page on a Drupal 9 site that will have a filter and a search bar. The filter needs to have each product and each region the product is sold in.

So I created 2 content types - Product and Region, and created a node for each one that just has a title.

I want to loop through each Product and Region node and display them on the page that has the filter.

Is it possible to render this data in a TWIG file?

Kevin avatar
in flag
You could solve this in 5 minutes with Views.
leymannx avatar
ne flag
Does this answer your question? [Get all nodes of a specific content type and print the data of each node inside page.html.twig](https://drupal.stackexchange.com/questions/279353/get-all-nodes-of-a-specific-content-type-and-print-the-data-of-each-node-inside)
Score:2
de flag

Templates are for formatting the output of data. They essentially wrap the data in HTML. Data generation is not handled in templates, it happens earlier in the pipeline, and the data is passed to the template, to wrap the data in HTML.

In this case, you want a View, as Kevin indicated in the comments. Views is a Drupal core module that provides a GUI for creating database queries and outputting the resulting data. You can then use the Twig Tweaks module and call a view in your Twig template as follows:

{{ drupal_view('who_s_new', 'block_1') }}

If you need to theme the output of the view, the Views module provides methods for templating the output of the view.

jfc avatar
us flag
jfc
Can I have multiple node types available in the view? In this case I would need it to get data from both my Product and Region nodes
Jaypan avatar
de flag
Views is extremely flexible. If you google, you can almost always find the solution you want, though it may need an add-on module. So the answer to your question is yes.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.