
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

External threats to computer security | Rootstack
December 3rd 2025
Learn about the main external threats to cybersecurity and how to protect your business from ransomware and other attacks. Contact Rootstack for a consultation

Rootstack Cybersecurity Solutions: Protection and Monitoring
December 3rd 2025
Discover Rootstack's cybersecurity solutions. Protect your business with data management, advanced monitoring, and proactive defense strategies

Core Banking Software Platforms: Key Features to Look for in 2026
November 25th 2025
This article explores what a core banking system is, the advantages of modernizing it, the key features of current solutions, and how to choose the right provider

Custom Web Application Development for Banking Companies
November 19th 2025
Discover why custom web application development is key for banking. Improve security, UX, and scalability with customized solutions.

Software development services company: What could we do?
November 19th 2025
We are not simply a vendor that writes code; we are strategic partners that translate business objectives into tangible digital solutions

Mobile app development phases: This is how we work
November 17th 2025
The phases of mobile application development represent a strategic process that goes far beyond code. It is a structured path designed to deliver value, minimize risks, and create a product that drives your company’s growth