November 7, 2024
A common need when displaying dates on a website is to display the start and end date of an event. If your date field is a standard Drupal datetime or daterange field, that's fairly easy. (See my post on how to get values from Drupal date fields in Twig).
If your date field is a Smart Date field and allows for recurrence, that's slightly more complicated, as it is now a multi-value field (…
Continue reading Get specific key values from multi-value field, e.g. first and last dates from recurring Smart Date field
September 6, 2024
<a href="{{ file_url(node.field_document.entity.field_media_document.entity.uri.value) }}"> {{ node.field_document.entity.field_media_document.entity.filename.value }} </a>
Of course you'll need to replace the fieldnames with yours.
The file_url function is necessary to transform the URL into a workable…
Continue reading How to get file URI and filename from referenced media entity field in Twig
October 26, 2023
This is a followup to my post "Print formatted date with correct timezone from datetime or daterange field in node and Views templates - Drupal 8".
That post deals with core Drupal Date and Daterange fields. Recently I've been working on a site with events which use a Smart Date field. The Smart Date module offers many features that core Drupal date fields don't, such as recurrence.
Raw Smart…
Continue reading Print raw values from Smart Date fields in Twig templates - Drupal 8+
November 17, 2021
Anyone who has read this blog before knows that I love the Drupal Group module. It has a companion module, Entity Group Field (not yet stable), which makes it even more useful. This adds a field to entities which allows you to add them to groups on their edit form, rather than having to go separately to the group to add them.
Here's how to print the rendered value of that field in node.html.twig…
Continue reading Print entity group field value in node.html.twig and configure display modes
September 23, 2019
Recently, I was creating an event registration Webform. I created a custom composite field with the fields needed for each registrant: first name, last name, and a set of three radio options for their organization status: member, non-member, membership fee included in event registration. Each status has a different registration fee.
I then wanted to create a computed Twig field that would…
Continue reading Calculate a value from options field in Webform custom composite element with Twig - Drupal 8
May 21, 2019
(Also see my post on how to get individual values from Smart Date fields).
I've been working on a site with events, and tearing my hair out over how to print formatted dates with the correct timezone value in Twig templates, specifically node.html.twig and views-view-fields.html.twig.
The issue was first, getting the formatted date to display in the template; and secondly, when I managed to get…
Continue reading Print formatted date with correct timezone from datetime or daterange field in node and Views templates - Drupal 8
May 17, 2019
I'm working on a website which has icons on file upload links, like so:
This works for people who can see the icons, but what about those who can't? Accessibility means giving all users an equivalent experience, regardless of how they're accessing the site.
If the icons were inline images, we could add alt text with the filetype. However, in this case, they're background images.
So, we want to…
Continue reading Add file type indicator to file field links for accessibility - Drupal 8-10
April 17, 2019
Data attributes on HTML elements can be very handy for targeting certain elements with CSS and/or Javascript.
Let's say, for example, we have a site with a menu whose links have a different background colour depending on what category they belong to, cats or dogs. The HTML might look like this:
<ul class="menu"> <li class="menu-item"> <a…
Continue reading How to add title as data attribute to menu links in Drupal 8 and Twig
March 26, 2019
I've been working on a website with the requirement to truncate certain text fields—display the first several words of the text, with a "show more" link which on click, expands the rest of the text. I had to dig a bit into the Twig documentation to figure it out, but it's easily done with Twig filters and a bit of JavaScript.
(Note that this method will strip any HTML in the text fields. It…
Continue reading Truncated expanding text field with "show more" link in Drupal 8 & Twig