Score:0

Loading a field value within a twig template

cn flag

I'm building a multi-country website using Drupal 8. The company would like to have each region manage their website independently but still have a "global" site. I'm building a theme which has some hardcoded areas (telephone numbers, copyright notice etc) which I want each region to be able to modify themselves. My thinking is to create a content area for say "copyright".

How do I render a field value for a content entity within a twig theme? Is there a better way to handle this kind of process?

Score:1
pt flag

Drupal has some great native concepts for doing just what you're trying to accomplish.

The main one you're looking for is built right in. It's called Blocks. Blocks can arbitrary entities that can be placed in theme regions you define on your site. Most of how Drupal handles rendering content is in some way tied to blocks be it system managed blocks like the "Main Content" block, Menu blocks, and so on.

You can define basic blocks which are just WYSIWYG content and place them in a theme region, but then also restrict them by content type, language, path, etc.

You can also define Custom Block Types which are fieldable like any other entity. You can control their appearance with twig, and set up any simple or complex field design you wish.

Depending on how you are choosing to group content and define what a "Country" is in Drupal, you can add contributed modules like Context to have extremely granular control over what gets rendered where.

Rendering a field in twig templates can be extremely easy, or extremely hard depending on where you are trying to render it. I highly suggest reading through The Drupal theming guide for a general understanding on how it all works.

Drupal templates are broken down into layers.

  • html.html.twig - which is the very basic scaffold of a web page
  • page.html.twig - which is the base semantic structure of your page(s) and where your regions are placed.
  • region.html.twig - which is the wrapper code for your regions where blocks live
  • block.html.twig - where blocks get themed - can render fields in here that are created on block entities
  • node.html.twig - Nodes are your "pages" of content you create like blog posts, about us content and so on. These can also have fields and are rendered at this level.
  • field.html.twig - Individual fields also provide their own templates and can be altered as you wish.
  • And so on. (there are many other template types, too many to mention here, but you get the idea) See more reading: https://www.drupal.org/docs/theming-drupal/twig-in-drupal/twig-template-naming-conventions

I hope this helps!

PS: As a last note, I would not be building new sites using Drupal 8. Drupal version 8 is end of life. You should be building on Drupal 9. The good news is Drupal 9 functionally is nearly identical to 8 and you should be able to upgrade your existing work to it with little issue.

mauzilla avatar
cn flag
Thank you for the useful feedback
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.