Skip to main content

Working in Twig, I had to add classes to a pre-existing array.  Whilst I've grown used to the patterns in PHP, these cannot be applied in Twig.  Instead I had to merge the new array elements.

Non associative arrays

Working from a base level, I'll begin as if the original array hasn't been set.  The merge filter expects as first argument an array whose content will be merged with the assigned variable:

{% set myArray = [] %}

{% set myArray = myArray|merge(['tw-flex-inline']) %}
{% set myArray = myArray|merge(['tw-rounded-xl', 'tw-border-gray-300']) %}

{# 
    The content of myArray is
    myArray = ['tw-flex-inline','tw-rounded-xl','tw-border-gray-300'] 
#}

Note that you can build any complex content using the same syntax:

{% set myArray = [] %}

{% set myArray = myArray|merge([
    ['tw-flex-inline'],
    ['tw-rounded-xl', 'tw-border-gray-300'],
    ['tw-text-gray-600']
]) %}

{% set myArray = myArray|merge([
    [
        ['tw-rounded-xl', 'tw-border-gray-300']
    ],
    [
        ['tw-text-gray-600']
    ],
]) %}

 

Associative arrays

To add items to an associative array, you would only pass as first argument an array with brackets with the new values:

{# Note that the original array in this case has an item #}
{% set myArray = {
    "first": 'tw-flex-inline'
} %}

{% set myArray = myArray|merge({
    "second": 'tw-rounded-xl'
}) %}

{% set myArray = myArray|merge({
    "third": 'tw-border-gray-300'
}) %}
 
{# 
    The content of myArray is
    myArray = {
        "first":'tw-flex-inline',
        "second":'tw-rounded-xl',
        "third":'tw-border-gray-300'
    }
#}

Note: that the merge filter uses array_merge in the background, which means that if you are working with an associate array, if the key already exists on the element it will be overwritten:

{# Note that the original array in this case has an item #}
{% set myArray = {
    "first": 'tw-flex-inline',
} %}

{# Add the "second" key with value 2 #}
{% set myArray = myArray|merge({
    "second": 'tw-rounded-xl',
}) %}

{# Change the value of the "second" key#}
{% set myArray = myArray|merge({
    "second": 'tw-rounded-lg',
}) %}
 
{# 
    The content of myArray is
    myArray = {
        "first":'tw-flex-inline',
        "second":'tw-rounded-lg'
    }
#}

 

Example of how the merge method was used in Drupal 9 

{%
set classes = [
  'tw-inline-flex',
  'tw-items-center',
  'tw-form-input',
  'tw-rounded-xl',
  'tw-border-gray-300',
  'tw-text-gray-600'
]
%}
{% apply spaceless %}
{% if autocomplete_message %}
  <div class="claroness-autocomplete">
    <input{{ attributes.addClass(classes) }}/>
    <div class="claroness-autocomplete__message hidden" data-drupal-selector="autocomplete-message">{{ autocomplete_message }}</div>
  </div>
  {{ children }}
{% else %}
  <input{{ attributes.addClass(classes) }}/>{{ children }}
{% endif %}
{% endapply %}

 

Related articles

Andrew Fletcher04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...
Andrew Fletcher17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...