
Learn some tips about Drupal 8
Share
Table of contents

Learning a new system usually means learning about its little quirks. Drupal 8 has its fair share of quirks. Here are some quick tips that I have learned in the past few months working on my first Drupal 8 project.
### addExpression
Suppose you need to get the last time an item in a table was edited. You can build a dynamic query and, instead of using fields(), you'd use addExpression() like so:
`$query->addExpression('MAX(mod_date)', 'col_alias');`
Now you will get the highest timestamp.
`$timestamp = $query->execute()->fetchField();`
### Differences between `$entity->field_name` and `$entity->get('field_name')`
`$entity->field_name` will return null if the field does not exist while `$entity->get('field_name')` will throw an Exception. Sometimes you'll want one behavior over another so keep that in mind.
### Adding a class to a item list elements
I almost went mad looking for an answer until I dived into the code and learned about `#wrapper_attributes`. Just add this to your item list render array and you're good to go. It will apply those attributes to each `` tag within your item list.
### appendItem
If you need to add a value to a multivalue field in an entity, you can use `appendItem()` to get the job done.
`$entity->field_multivalue->appendItem($some_other_entity_id);`
Over the coming weeks, I or any of my fellow developers here at [Rootstack](http://rootstack.com) will be posting quick tips for other developers new to Drupal 8. I'm sure you will learn a thing or two or, at the very least, we will save you from a mild headache. 
 
Related blogs

Modern Javascript Landscape
October 31st 2025
It is a good time to be working with javascript, competition is driving the best out of each player while the onboarding time being very minimal thanks to the standards each solution is keeping.

Healthcare App Development: trends for 2026
October 29th 2025
With this technological disruption in the sector, certain vulnerabilities may certainly arise, and it brings up the eternal dilemma of the human touch and how much technology should be allowed to interfere in the processes.

Artificial intelligence in healthcare: Integrations and benefits
October 29th 2025
If you lead a healthcare institution and are considering incorporating AI solutions, this article describes the most relevant integrations, the concrete benefits, and how an expert software development partner can ensure a secure, scalable implementation aligned with clinical and business objectives

Third edition of the annual Rootstack event: AI with real results
October 29th 2025
Beyond the technological deployment, what made the difference was the human approach: each presentation revolved around how technology can improve people's lives, optimize businesses, and create sustainable opportunities

Solution Discovery Techniques
October 24th 2025
This article, aimed at business leaders who need to ensure a solid return on investment and minimize technological uncertainty, explains why the discovery phase is so vital
