
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

Banking software development company | Rootstack
February 19th 2026
We develop banking CRM systems and fintech solutions for credit unions. Transform your institution with secure and scalable technology.

Firewall solutions for IoT: Protect your business network
February 11th 2026
Discover how to mitigate IoT security risks with advanced firewalls. Protect your devices and data with Rootstack's developer solutions.

IoT Authentication: Key Methods for Protecting Devices
February 11th 2026
Discover what IoT authentication is, the most critical security risks, and how to protect your connected devices at enterprise scale with Rootstack

IoT penetration testing: Why it's crucial and necessary
February 10th 2026
Discover how penetration testing protects your business from IoT security risks. Learn to identify vulnerabilities before attackers do.

Main IoT security risks and how to mitigate them
February 10th 2026
Discover the most critical IoT security risks for businesses and learn effective strategies to mitigate them with secure development solutions.

UI design services: Improve UX and increase conversions with Rootstack
February 4th 2026
Optimize your digital platform with professional UI design services. At Rootstack, we combine strategic design and frontend development to maximize your ROI.