nerdfisch: DevBits

Kleine, aber feine Code-Snippets, nützliche Tweaks und elegante Lösungsansätze aus dem Entwickler-Alltag

11.06.2026 | Michael Ebert

Change the order of local tasks

my_module.module
function my_module_local_tasks_alter(&$local_tasks) {
  if (isset($local_tasks['entity.section_library_template.collection'])) {
    $local_tasks['entity.section_library_template.collection']['weight'] = 110;
  }
    
  if (isset($local_tasks['feeds.admin'])) {
    $local_tasks['feeds.admin']['weight'] = 120;
  }

}

php
routing
local task links
weights
11.06.2026 | Nikolas Kopp

List enabled view modes (view displays) for a bundle of an entity type

getBundlesWithViewmode.php
  private function getBundlesWithViewmode(string $entity_type, string $view_mode): array {
    $bundles = [];

    foreach (\Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type) as $bundle => $info) {
      $view_modes = \Drupal::service('entity_display.repository')->getViewModeOptionsByBundle($entity_type, $bundle);
      if (isset($view_modes[$view_mode])) {
        $bundles[] = $bundle;
      }
    }

    return $bundles;
  }
  
php
entities
view modes
bundles
21.05.2026 | Lothar Ferreira Neumann

Aggregate heterogeneous media fields for structured Drupal migration

migration_field_merge.yml
  field_media:
    - plugin: merge
      source:
        - field_image
        - field_images
    - plugin: sub_process
      process:
        target_id:
          - plugin: migration_lookup
            source: fid
            migration:
              - my_migration_media_image
yml
Migration
media
21.05.2026 | Michael Ebert

Do bulk updates as a batch job

custom_module.install
<?php

/**
 * Update node title of all nodes.
 */
function custom_module_update_10001(&$sandbox) {
  // Define you entity type.
  $entity_type = 'node';

  // Load the entity type manager service.
  $entity_type_manager = \Drupal::service('entity_type.manager');

  // Get the storage for the entity type.
  $entity_storage = $entity_type_manager->getStorage($entity_type);

  if (!isset($sandbox['total'])) {
    $all_entity_ids = $entity_storage->getQuery()
      ->accessCheck()
      ->execute();
    $sandbox['total'] = count($all_entity_ids);
    $sandbox['current'] = 0;

    if (empty($sandbox['total'])) {
      $sandbox['#finished'] = 1;
      return;
    }
  }

  $entities_per_batch = 25;
  $entity_ids = $entity_storage->getQuery()
    ->accessCheck()
    ->range($sandbox['current'], $entities_per_batch)
    ->execute();
  if (empty($entity_ids)) {
    $sandbox['#finished'] = 1;
    return;
  }

  // Optionally, perform operations with the loaded entities.
  // For example, load and modify each node and set a new title.
  foreach ($entity_ids as $entity_id) {
    $entity = $entity_storage->load($entity_id);

    if ($entity->hasField('title')) {
      $entity->setTitle('New Title');
    }

    $entity->save();
    $sandbox['current']++;
  }

  \Drupal::messenger()
    ->addMessage($sandbox['current'] . ' users processed.');

  if ($sandbox['current'] >= $sandbox['total']) {
    $sandbox['#finished'] = 1;
  }
  else {
    $sandbox['#finished'] = ($sandbox['current'] / $sandbox['total']);
  }

}
php
copy-paste
update hooks
bulk edit
07.05.2026 | Peter Gerken

Run missing entity Schema updates

⚠️ WARNING: Potential data loss. Use with caution.

scp_base.module
/**
 * Run missing database schema updates.
 */
function mymodule_update_10001(&$sandbox) {
  $entity_type_manager = \Drupal::entityTypeManager();
  $entity_type_manager->clearCachedDefinitions();
  $change_summary = \Drupal::service('entity.definition_update_manager')->getChangeSummary();
  foreach ($change_summary as $entity_type_id => $change_list) {
    $entity_type = $entity_type_manager->getDefinition($entity_type_id);
    \Drupal::entityDefinitionUpdateManager()->installEntityType($entity_type);
  }
}
module
update hooks
drupal
entities