
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

Cybersecurity for Insurance Companies: Protecting Data, Trust, and Continuity
June 5th 2025
Insurance companies handle vast amounts of confidential information: personal data, financial records, medical histories, and contracts. This makes them attractive targets for cybercriminals. From identity theft to ransomware attacks, cybersecurity in the insurance sector is no longer optional—it's a strategic priority

Cybersecurity for Government: Protecting Public Information in the Digital Age
June 5th 2025
From ransomware attacks to disinformation campaigns, government institutions need to bolster their digital infrastructure with robust and adaptable cybersecurity services.

Cybersecurity measures for retail businesses
June 5th 2025
We ask you to trust us: a cybersecurity agency with more than 15 years of experience serving international clients.

Cybersecurity for banking institutions
June 5th 2025
A cybersecurity agency like Rootstack ensures that your clients' data—and their money—are protected from any external attack that threatens business well-being. With a team of experts on hand, available 24/7, you can count on them.


Cybersecurity compliance services
June 4th 2025
This article will help you understand exactly what cybersecurity compliance services are, why they're essential, and how to choose an agency that not only implements technical solutions but also keeps you within the legal and regulatory framework governing your industry.