Direkt zum Inhalt

Devbits

Kleine Code-Schnipsel aus unserem Alltag mit Drupal, PHP und DevOps: Problem, Lösung, Code.

05.10.2026 | Pascal Crott

How to add an element to an array at the first position in Twig

When using the merge function in Twig, the new element is appended to the array. However, we would like the new element to prepend the existing array.

{# The elements array we want to add a '0' to at the beginning. #}
{% set elements = ['1', '2', '3'] %}
{# Working: Merge elements to a new array and override the variable. The result will be ['0', '1', '2', '3']. #}
{% set elements = ['0']|merge(elements) %}
{# Not working: The result will be ['1', '2', '3', '0']. #}
{% set elements = elements|merge(['0']) %}

Issue:

When using the merge function in Twig, the new element is appended to the array. However, we would like the new element to prepend the existing array.

Solution:

Create a new array and append the original array to it.

22.06.2026 | Pascal Crott

Gin: Make use of the simple table

Gin per default enhances all tables to use sticky headers and other goodies. But sometimes you just want to have a simple table without the enhancements.

<?php

/**
 * Implements hook_theme_suggestions_HOOK_alter() for table.
 */
function hook_theme_suggestions_table_alter(array &$suggestions, array $variables): void {
  if (empty($variables['attributes']['class'])) {
    return;
  }

  if (is_array($variables['attributes']['class']) && in_array('ief-entity-table', $variables['attributes']['class'])) {
    $suggestions[] = 'table__simple';
  }
}

Issue:

Gin per default enhances all tables to use sticky headers and other goodies. But sometimes you just want to have a simple table without the enhancements.

Solution:

There is a "table--simple" template provided by Gin which you can make use of by defining the theme suggestion for your table. In this example we use simple tables for inline entity form tables, since those are complex tables which don't always work together smoothly with gin tables.

22.06.2026 | Mathias Grab

How to enable Twig debug in Drupal

You work with Twig templates and need to use the debugger.

parameters:
  twig.config:
    debug: true
    auto_reload: null
    cache: false

Issue:

You work with Twig templates and need to use the debugger.

Solution:

Code solution:
Enable Twig debug by copying the snippet provided from root/web/sites/default.services.yml into root/web/sites/development.services.yml

See also: Drupal Docs on Twig

Sitebuilding solution:
Navigate to the administrative area via the admin toolbar to Configuration -> Development -> Development settings or via the path /admin/config/development/settings
 

Enabling twig debug 1


Enable the checkbox Twig development mode, as well as Twig debug mode and Disable Twig cache.
 

Enabling twig debug 3

For further information regarding Drupal and Twig take a look at  https://www.drupal.org/docs/develop/theming-drupal/twig-in-drupal/debugging-twig-templates

Info

This should only be used for local development environments but disabled in production.

23.04.2026 | Lothar Ferreira Neumann

Add custom text to gin_login with Form Decorator

First we create a module, lets name it 'gin_login_text'. If you have drush installed, you can generate a module using 'drush generate module'.

We will use the form_decorator module to alter the output of the gin_login module to look something like this:
 

An example what the result looks like when using gin_login_text.

This DevBit provides a step-by-step tutorial how to get there. 

First we create a module, lets name it 'gin_login_text'. If you have drush installed, you can generate a module using 'drush generate module'.

To insert our text into the gin_login we need to find the proper render array. For this we look up the logout screen with the inspector:

A screenshot from the gin_login screen with inspector.

In the picture we can see a FILE NAME SUGGESTION: 'region--pre-content.html.twig', thanks to the enabled Twig debug. If we now look into the templates for gin_login we find this:

...
    <div class="layout-container">
      {{ page.pre_content|without('claro_primary_local_tasks', 'gin_primary_local_tasks') }}
      <main class="page-content clearfix" role="main">
        <div class="visually-hidden"><a id="main-content" tabindex="-1"></a></div>
        {% block title %}
          <h1 class="page-title user-form-page__page-title">{{ 'Log in'|t }}</h1>
        {% endblock %}
        {{ page.highlighted }}
        {% if page.help %}
          <div class="help">
            {{ page.help }}
          </div>
        {% endif %}
        {{ page.content|without('claro_primary_local_tasks', 'gin_primary_local_tasks') }}
      </main>

    </div>
...

We are interested in the line `{{ page.pre_content ..... }}`

So now we need to put our text into the render array for the page with a hook:

<?php

declare(strict_types=1);

/**
 * @file
 * Implements hooks for gin_login_text.
 */

use Drupal\Core\Url;

/**
 * Implements hook_preprocess_page__user__login().
 */
function gin_login_text_preprocess_page__user__login(&$variables) {
  $config = \Drupal::config('gin_login.settings');

  $variables['page']['pre_content']['login_text']['#tree'] = TRUE;
  $variables['page']['pre_content']['login_text']['text'] = [
    '#type' => 'markup',
    '#format' => $config->get('login_text.text')['format'] ?? 'full_html',
    '#markup' => $config->get('login_text.text')['value'] ?? 'Default Heading Text',
  ];
}

$variables['page']['pre_content']['login_text'][''#tree] = TRUE;

This line is used to enable the nested array structure in a form which we will use to alter the form of gin_login to enter a text.

Last but not least add the dependencies to the info.yml:

name: gin_login_text
type: module
description: Provides a login text for gin_login
package: Custom
core_version_requirement: ^10 || ^11
dependencies:
  - form_decorator:form_decorator
  - gin_login:gin_login

As you already see in the info.yml, we have a dependency on the form_decorator. Using this module, we can decorate the form of gin_login which you can find at `/admin/config/system/configuration/gin-login`. The clue is in the title. Follow the example on the module site at https://www.drupal.org/project/form_decorator.

Your file should look like this: 

<?php

declare(strict_types=1);

namespace Drupal\gin_login_text\FormDecorator;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\form_decorator\FormDecoratorBase;
use Drupal\multivalue_form_element\Element\MultiValue;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Decorates the gin login form.
 *
 * @FormDecorator(hook = "form_gin_login_form_alter")
 */
final class GinLoginFormDecorator extends FormDecoratorBase implements ContainerFactoryPluginInterface {
  use StringTranslationTrait;
  use DependencySerializationTrait;

  /**
   * Constructs a GinLoginFormDecorator object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The configuration factory.
   */
  public function __construct(
    array $configuration,
    string $plugin_id,
    mixed $plugin_definition,
    protected ConfigFactoryInterface $configFactory,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $configuration, $plugin_id, $plugin_definition) {
    return new self(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('config.factory'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, ...$args): array {
    $form = $this->inner->buildForm($form, $form_state);
    $config = $this->configFactory->get('gin_login.settings');

    $form['login_text'] = [
      '#type' => 'details',
      '#title' => $this->t('Text'),
      '#open' => TRUE,
      '#tree' => TRUE,
    ];
    $form['login_text']['text'] = [
      '#type' => 'text_format',
      '#title' => $this->t('Text to appear as the login page heading.'),
      '#description' => $this->t('If textfield is left empty no text will be displayed on the login page'),
      '#format' => $config->get('login_text.text')['format'] ?? 'full_html',
      '#default_value' => $config->get('login_text.text')['value'] ?? '',
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $this->inner->submitForm($form, $form_state);
    // Get it editable or else its immutable.
    $config = $this->configFactory->getEditable('gin_login.settings');
    $config->set('login_text.text', $form_state->getValue('login_text')['text']);

    $config->save();
  }

}

You should now be able to see this at the gin_login configuration under `/admin/config/system/configuration/gin-login`:

A fancy login_text for gin.

With the text added, even with some bold and italic modifications, let's observe the outcome:

An example what the result looks like when using gin_login_text.

On a site note, there is an issue to tackle that problem, but i wanted to elaborate the form_decorator usage here.
https://www.drupal.org/project/gin_login/issues/3252442 

Hopefully this article helped you alter your login site!

13.11.2025 | Lothar Ferreira Neumann

Use ternary statements in Twig

You want to conditionally pass values to components in Twig based on variable existence or content.

{# Checks if a URL exists and passes it to the component. Otherwise, sets it to false. Works with primitive data types. #}
{% set link = url ? { url: url } : false %}

{# Checks if the media object is not empty and passes it to the component. Otherwise, sets it to false. Suitable for complex data types like objects. #}
{% set media = media is not empty ? { media: media } : false %}

Issue:

You want to conditionally pass values to components in Twig based on variable existence or content.

Solution:

Uses Twig’s ternary operator to check variable conditions and pass either an object with the variable or false.

If you're working with more complex objects, make sure to handle the empty test according to the Twig documentation on empty, as its behavior may vary depending on the object's implementation.