
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

AWS Architecture for Online Banking
June 25th 2025
In today's fintech landscape, AWS is not only a technological option but a fundamental strategic advantage for competing effectively in the digital marketplace.

How Rootstack's intelligent chatbots are transforming businesses' operational efficiency
June 24th 2025
At Rootstack, we've been helping organizations achieve this transformation for years, developing intelligent chatbots aligned with their business objectives.

Data Integration: the key link to a solid Business Intelligence architecture
June 24th 2025
No Business Intelligence strategy is effective if data is scattered or disorganized. Data integration is the technical backbone that enables the consolidation of information from various sources and transforms it into actionable value.

What is Business Intelligence and how to implement it: Step by step
June 24th 2025
Business Intelligence (BI) is much more than just reports — it's the ability to turn information into strategic, real-time decisions. This blog explores the technical foundations of BI, from data collection to visualization, guiding companies looking to adopt a comprehensive solution.

How to integrate a chatbot into a website: A guide for business leaders
June 23rd 2025
The implementation of chatbots responds to a clear need: offering 24/7 support, reducing operating costs, and speeding up response times.

Predictive analytics guide: Strategy, implementation, and business uses
June 23rd 2025
Leading companies are building competitive advantage by integrating predictive models into their daily operations.