
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

Advantages of a dedicated offshore software development team
September 10th 2025
Not only do we offer a team with proven experience in more than 50 technologies, but we also guarantee daily communication.

Steps to hiring a software development team
September 10th 2025
In this article, we'll explain it all, and at the end, you can contact us immediately if you're interested in learning more.

What type of software developers can you hire?
September 9th 2025
A team of software and technology experts is the trump card of the most successful companies worldwide, and this is a field in which Rootstack excels with a group of engineers at the top of their game

Top it staff augmentation companies: Things to look out for
September 8th 2025
We can design a delivery cell tailored to your needs, with the governance, SLAs, and value plan your business needs to accelerate its technological roadmap

How to choose the right software outsourcing company
September 3rd 2025
Software outsourcing is a modality in which a company chooses to hire an external company to offer services related to software development.

Staff Augmentation vs Managed Services
September 3rd 2025
Rootstack can help you with any of these two services you choose. We have the experience to advise you, work together, and bring the project to a successful conclusion.