
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

How Healing Agent revolutionizes the robustness of interface-based automations
August 15th 2025
With Healing Agent, UiPath takes a crucial step toward truly autonomous, adaptable automations that are ready for changing environments

Predictive models for marketing in banking
August 13th 2025
Predictive models for banking marketing represent an extraordinary opportunity to identify new audiences, run more effective campaigns, and manage risk more accurately.

Predictive model to reduce default risk
August 12th 2025
A predictive model to reduce default risk is not just a technological tool: it is a competitive advantage and a profitability protector.

Digital Signatures for Businesses: How Rootstack Can Be Your Digital Partner
August 6th 2025
If we go to a technical definition, a digital signature for companies is a set of data that accompanies a document with the purpose of identifying the signatory without leaving room for error

Digital Signature vs. Electronic Signature
August 6th 2025
At Rootstack, together with our partner Validated ID, we have implemented multiple digital signature solutions for various companies and industries, so, based on our experience, we can help you in this process

Most important features of a Digital Signature Solution
August 6th 2025
This is nothing more than software to facilitate your company's processes, avoiding the use of physical papers that can be damaged, lost or, in the worst case, fall victim to forged signatures that can lead to legal problems