Advanced Components
  • Welcome
  • Start here
    • Setup
    • Dependencies
  • Getting Started
    • Front-end tooling
    • Create a new D9 theme
    • Component Architecture
    • Drupal Best Practices
    • Drupal Attributes
    • Twig Blocks
  • Basic Components
    • Global styles
    • Adding webfonts
    • Heading Component
      • Improving the Heading
    • Button Component
  • A component's lifecycle
    • Hero Component
    • Include statements
    • Drupal prep
    • Drupal entities
    • Homepage content type
    • View modes
    • Add Hero to homepage
    • Drupal cache & twig debugging
    • Twig template suggestions
    • Getting field values
    • Integrating the Hero
    • Drupal Libraries
    • Hero Image styles
  • Card component
    • Card Component
    • Component Variants
    • Card Variant
    • Author component
  • Drupal site building
    • Blog articles
    • View modes
    • Drupal Views
    • Author
    • Generate content
    • Adding Blog Lists to the Homepage
  • Drupal Integrations
    • Integrating the Card
      • Taxonomy terms
    • Integrating the Card Wide
    • Author integration
    • From our blog
    • Integrating From our blog
    • Featured Content
    • Integrating Featured Content
    • Blog image styles
    • Blog detail page
  • Extras
    • Image Styles
    • Navigation
  • Resources
    • Resources
Powered by GitBook
On this page
  • Passing field values to components
  • Exercise: Debugging Twig

Was this helpful?

  1. A component's lifecycle

Getting field values

PreviousTwig template suggestionsNextIntegrating the Hero

Last updated 3 years ago

Was this helpful?

Earlier we enabled twig debugging in order to identify the right template suggestions for our components. Twig debugging also means available within the scope of the twig template we are working with.

In addition to enabling Twig debugging, we need the which provides tools to help us identify the right variables in our templates. One of these tools is Kint. Kint provides a nice user interface to print content arrays. This makes it easy for developers to see all available variables for the content currently being rendered on the page.

Passing field values to components

Kint is a PHP Debugging tool. Kint for PHP is a tool designed to present your debugging data in the absolutely best way possible. In the Presenter template method you'll quickly discover that when passing the value of a field to your component, Drupal does not give us the raw value of that field, we're instead given a , and when that array is rendered, it includes a lot of default markup that will often get in the way. While we may be tempted to just pluck the value that we want from the render array and pass it to our component, it's best to try and let Drupal fully render the field to avoid cache invalidation issues.

Exercise: Debugging Twig

  1. Before you can debug Twig, you need to enable the devel module

  2. In the twig template you wish to get variables for (i.e. node--article--hero.html.twig), type {{ dpm() }}

  3. Clear Drupal's cache and reload the page

  4. You should see the render output as shown below:

The example below shows us the value of the title field for the hero. Notice how some fields have a [ >** ]** sign next to it. This means each field is an array you can drill down into until you get to its value. See example below:

In the example above we expanded label until we get to the value of it. This shows us the actual value entered in Drupal for this field (How to build drupal websites with components). So for us to get the right field value when integrating the components with Drupal we need to declare the full field structure as shown to us above. In this example it would be something like this:

content.field_image.0['#context'].value
content.field_name|field_value

This will get us the same value but it will do it in a responsible way without breaking Drupal caching validation.

However, this is not necessarily best practice. Although using the format above to get the field's value will work, there are some issues related to Drupal cache validation that can arise from this approach. A better approach would be to use the or modules. Using one of these modules would allow us to format the field in such a way that caching validation is not compromised. Example:

Further reading: .

Twig Field Value
Twig Tweak
Ensuring Drupal 8 Block Cache Tags bubble up to the Page
discovering and inspecting twig variables
devel module
render array