
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

Portfolio management and collection software for rural areas of Panama
July 22nd 2025
Panama, although it has a robust financial system, still faces a marked gap between urban and rural areas in terms of access to financial technologies

Q Business Suite: The future of the workplace AI in the cloud, combined with QuickSight and Q Apps
July 21st 2025
The workspace of the future is here: Q Business Suite

What's new in Odoo 18: from roadmap to action
July 18th 2025
In this blog, we review the most notable features that are already impacting the daily operations of thousands of businesses

Key developments in Validated ID in 2025
July 18th 2025
In this blog we review the main developments that will mark 2025 for this key partner in the digital identity ecosystem, with whom we have completed multiple interesting projects and we are excited about what the future holds

Software unit testing companies in Colombia: Rootstack your best option
July 16th 2025
In Colombia, Rootstack has established itself as one of the leading companies in this field, offering solutions tailored to the highest standards of software quality.

Types of software testing for your fintech project
July 15th 2025
If your fintech company is in the final stages of selecting a technology testing agency, this article will clarify the types of testing you should require for your project, with a technical, realistic, and results-based approach