<div>Drupal Theming Do's and Don'ts</div>
Are you a Drupal enthusiast looking to take your theming game to the next level? Well, you're in for a treat! In this blog post, we're going to dive into some essential best practices for Drupal theming.
View Modes
❌Don’t use the same view mode for everything
✅Do use view modes for unique content displays
View modes are an invaluable tool when it comes to theming in Drupal. They allow you to create unique presentations for your content, tailored to specific use cases. Here's why you should embrace them:
Tailored content display. View modes allow you to customize how your content appears in different contexts. You can make each one look just right, whether it's a teaser on a list page or a full content view.
Field management and formatting. Tired of displaying all the fields, even when you only need a few? View modes let you selectively show or hide specific fields, ensuring a clutter-free design. What’s more, view modes enable you to apply different formatters to each field.
Control referenced entities. View modes are also incredibly useful when displaying referenced entities as you can customize how a referenced entity appears per view mode.
Custom templates. Having different view modes also allows you to create specific templates that you can easily override and customize to your liking.
Some common examples of custom view modes:
- Search result – used when the content is displayed as part of a search result
- Featured – used when displaying the content in a more prominent way
- Embedded – used when embedding the content inside a rich-text editor
Here you can see the recipe content type with multiple view modes configured.
Printing Field Values
❌Don’t access entity fields directly
✅Do use the content object when printing field values
Accessing and printing out entity fields directly places the burden of displaying fields on you and your code further increasing technical debt. So avoid printing entity fields like this:
<div>{{node.field_example.value}}</div>
Instead, use the content object:
<div>{{content.field_example}}</div>
Using the content object means you'll benefit from field widgets and formatters. There are tons of contrib modules that provide additional field widgets and formatters that you can use.
The content object also allows you to take advantage of field preprocess functions as well as field templates. These are out-of-the-box theming helpers that allow you to fine-tune the appearance of your fields. This approach is very close to Drupal standards.
However, there's an exception to this rule. You may access entity fields directly when checking raw values (like lists or keys) or when you need to verify the truthiness of a field. In these specific cases, it's acceptable to access the fields directly from the entity object instead of the content object. See the sections on empty fields and ternary operations below for tips on how to check raw values.
Beyond Content Fields
❌Don’t overfocus on fields and forget the rest of your content
✅Do pay attention to internal data and use the without Twig filter
It’s normal to find yourself printing the content fields individually, but it’s essential not to overlook the rest of your content. Here’s what to keep in mind:
Ensure processing of important internal data. Your content may contain essential internal data, such as hidden form tokens or cache tags.
Customize your view mode to hide fields. One of the strengths of view modes is their ability to hide specific fields. By leveraging view modes effectively, you can ensure that certain fields are hidden when they are not needed in a particular context.
Use the without Twig filter. To print the rest of the content effortlessly, you can make use of the without Twig filter. This filter allows you to exclude specific fields from the rendering, ensuring that only the necessary content is displayed.
// Print content fields individually.{{content.field_foo}}{{content.field_bar}}// But don't forget to "flush" the rest of the content.{{content|without('field_foo','field_bar')}}
Clean, Lean Templates
❌Don’t allow clutter and "Drupalisms" to build up
✅Do use contrib modules to tidy up your code
The Fences and No Markup modules can help you achieve more concise and readable code for cleaner, more efficient templates.
These modules allow you to control the HTML markup and structure of your content without creating unnecessary clutter. Your templates will be easier to maintain and understand as a result.
They also allow for more standardized markup that’s easier to style with CSS. Drupal generates a lot of markup by default, often containing "Drupalisms"—patterns and classes that are specific to the Drupal ecosystem. Fences and No Markup enable you to strip away these Drupal-specific elements.
Remove wrapper elements with the Fences module or completely remove all default markups with the No Markup module.
Empty Fields
❌Don’t rely on rendering functions to tell you if a field is empty
✅Do check the entity property directly
One area that often goes overlooked is handling empty fields. It’s best practice to check the entity property directly instead of relying on rendering functions to determine if a field is empty. By accessing the raw data, you can efficiently assess whether the field contains content without triggering unnecessary rendering processes.
If you have to render a field before checking if it's empty, do so judiciously. Render the field once and store the result. Then perform your checks on the stored value. This approach minimizes the performance impact associated with repeated rendering calls.
Consider the following example:
{%ifcontent.field_name|renderisnotempty%}{{content.field_name}}{%endif%}
This can be optimized as follows:
{%ifcontent.field_exampleisnotempty%}{%ifnode.field_example.value%}{%ifnotnode.field_example.isEmpty()%}
Or, if rendering is necessary, do it once and test:
{%setrendered_field=content.field_example|render|trim%}{%ifrendered_field%}{{rendered_field}}{%endif%}
Ternary Operations
✅Do use ternary operations to make your code look concise
❌Don’t use them in situations where they might compromise code readability
Simplicity often leads to elegance. One way to achieve a cleaner and more concise codebase is by embracing Twig ternary operations. Here are some practical examples:
Example A: If foo, echo “yes” else echo “no”:
{{foo?'yes':'no'}}
Example B: If foo, echo it, else echo “no”:
{{foo?:'no'}}
or
{{foo?foo:'no'}}
Example C: If foo, echo “yes”, else echo nothing:
{{foo?'yes'}}
or
{{foo?'yes':''}}
Example D: If foo is defined and not null, echo it, “no” otherwise:
{{foo??'no'}}
Example E: If foo is defined (empty values also count), echo it, “no” otherwise:
{{foo|default('no')}}
While Twig ternary operations are excellent for simplifying straightforward conditions, it's essential to use them judiciously. Reserve their use for situations where the conditions are concise and don't compromise code readability.
Twig Features
❌Don’t allow regular PHP code to become bloated and unwieldy
✅Do use Twig to create efficient and maintainable templates
Twig compiles templates down to plain optimized PHP code. Maximizing the potential of Twig goes a long way in creating efficient and maintainable templates. With single directory components (SDCs) becoming part of Drupal core, familiarizing yourself with Twig, and its built-in functions, is now more practical than ever.
Include: Insert Static Template Content
The include statement allows you to insert static template content from another file into your current template. This can be especially handy when you have reusable components or snippets of code that you want to include across multiple templates.
For example:
{#Includeheadercontentfromaseparatetemplatefile#}{%include'themes/my_theme/templates/header.html.twig'%}
Extends: Template Inheritance
The extends statement is the foundation of template inheritance. It enables you to create a base template with common elements and then extend or override specific sections in child templates. This promotes consistency and reduces redundancy in your theming. Some examples are below.
Base template: base.html.twig
<divclass><headerclass><h1class>{%blockheader%}{%endblock%}<main>{%blockcontent%}{%endblock%}</main></h1class></headerclass></divclass>