
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

Credit Collection Software: Automate your flows with AI
October 2nd 2025
AI is one of the fastest-growing technological trends across industries, already being used in several key processes to maximize production and free up human staff to focus on key tasks.

Best Automated Invoice Management System Features
October 2nd 2025
For a system to truly add value, it's not enough to simply call itself "automation." It must incorporate a series of technical, operational, and integration components.

AI Debt Collection Software
October 2nd 2025
Artificial intelligence is no longer a novelty, or something out of a science fiction movie, but has become the best assistant and tool for any professional or company worldwide. That is why we should not fear it and, on the contrary, integrate it into the most important processes of the company.

How to use product discovery tools?
September 24th 2025
Product discovery tools, when used within a conscious and structured process, are powerful allies for reducing risk, saving costs, and building products with a greater likelihood of success

How to apply product discovery in your company?
September 24th 2025
If you are thinking about developing a software product but are not sure how to start, product discovery is the best initial investment. It not only clearly defines what you need, for whom, and why, but it also reduces costs, risks, and later frustrations

Solution Discovery Tools
September 24th 2025
If your company is considering developing software but doesn’t know where to start, the first step is not programming—it’s discovering the right solution.