nerdfisch: DevBits

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

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
07.05.2026 | Dominik Wille

Exclude already-displayed entities from a view

Use the views_exclude_previous module. See below or open the module documentation for further information.

views
modules
data filtering
30.04.2026 | Pascal Crott

Prevent empty views blocks from getting rendered even if they are empty.

Issue: https://www.drupal.org/project/drupal/issues/953034

my_module.module
<?php

use Drupal\views\Plugin\Block\ViewsBlock;

/**
 * Implements hook_block_alter().
 */
function hook_block_alter(array &$definitions) {
  foreach ($definitions as $block_id => &$block_definition) {
    if ($block_definition['class'] === ViewsBlock::class) {
      $block_definition['class'] = 'Drupal\my_module\Plugin\Block\ViewsBlock';
    }
  }
}

ViewsBlock.php
<?php

namespace Drupal\my_module\Plugin\Block;

use Drupal\views\Plugin\Block\ViewsBlock as ViewsBlockCore;

/**
 * Replaces the generic Views block.
 */
class ViewsBlock extends ViewsBlockCore {

  /**
   * {@inheritdoc}
   */
  public function build() {
    if (empty($this->view->result) && empty($this->view->empty)) {
      // Without caching.
      // return ['#cache' => ['max-age' => 0]];
      // With hard caching.
      return [];
    }

    return parent::build();
  }

}

module
php
Frontend
30.04.2026 | Mathias Grab

How to get a list of all available contexts by context providers

get_all_available_contexts.php
<?php

$all_available_contexts = \Drupal::service('context.repository')->getAvailableContexts();
php
contexts
services
23.04.2026 | Lothar Ferreira Neumann

Sort render array by custom weight property

sort_by_weight.php
// ...

$build['content_1'] = [
    '#markup' => "content 1",
    '#custom_weight_property' => 10,
];

$build['content_2'] = [
    '#markup' => "content 2",
    '#custom_weight_property' => 5,
];

usort($build, function ($a, $b) {
    return SortArray::sortByKeyInt($a, $b, '#custom_weight_property');
});

The output looks like this:

content 2
content 1

Just a hint:

The custom weight system controls the display order of elements, where lower weight values appear earlier, as shown in the example: content_2 (with weight 5) is rendered before content_1 (with weight 10).
 

php
render arrays