php

array_shuffle.php
$array = [
  'key_one' => 'one',
  'key_two' => 'two',
  'key_three' => 'three',
];
onlyKeys.php
// You will get a mixed array with only keys or values, depends if you use array_flip()

// Change keys to values and vice versa.
array_flip($array);

// Reduce the array to the keys and shuffle them.
array_rand($array);
shuffledArray.php
// Array will be shuffled with key => value staying a pair.

// Shuffle the key => value array, keeping both.
shuffle($array);
04.04.2024 | Lothar Ferreira Neumann

Mix values of an associated array

default_date_value.php
public static function getDefaultDate() {
  $end_date = new DrupalDateTime();
  $end_date->setTime(23, 59, 59);
  $timezone = new DateTimeZone('UTC');
  $end_date->setTimezone($timezone);
  return $end_date->format('Y-m-d\TH:i:s');
}

field.field.node.mycontenttype.field_date.yml
default_value_callback: '\Drupal\my_custom_module\Entity\DefaultDateValue::getDefaultDate'

downcastArray.php
$array = [
  'key_one' => 'one',
  'key_two' => 'two',
  'key_three' => 'three',
];

// Downcast associated array to indicated array with only keys.
array_keys($array);

// Downcast associated array to indicated array with only values.
array_values($array);
ExampleOutcome.txt
Outcome array_keys():
$array = [
  [0] = 'key_one',
  [1] = 'key_two',
  [2] = 'key_three',
];

Outcome array_values():
$array = [
  [0] = 'one',
  [1] = 'two',
  [2] = 'three',
];
04.04.2024 | Lothar Ferreira Neumann

Downcast an array.

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']);
  }

}
21.05.2026 | Michael Ebert

Do bulk updates as a batch job

drushCommand_php_cli.sh
drush php:cli

drushCommand_php_eval.sh
drush php:eval

drushCommand_php_script.sh
drush php:script

14.04.2026 | Michael Ebert, Lothar Ferreira Neumann

drush php commands

You have a private files folder. The path could be set in settings.php like this:

settings.php
$settings['file_private_path'] = $app_root . '/../private_files/files';


Git status tells you the private files folder is untracked and you want to prevent data loss. This could be achieved by ignoring the folder in gitignore.

.gitignore
# Ignore private_files
/private_files/
Breadcrumb_builder_custom.php
  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    return $route_match->getRouteName() == 'commerce_cart.page';
  }

  /**
   * {@inheritdoc}
   */
  public function build(RouteMatchInterface $route_match) {
    // Node ID from shop
    $node = $this->entityTypeManager->getStorage('node')->load(3337);

    $breadcrumb = new Breadcrumb();
    $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
    $breadcrumb->addLink(Link::fromTextAndUrl($node->getTitle(), $node->toUrl()));

    return $breadcrumb;
  }
10.07.2025 | Lothar Ferreira Neumann, Mathias Grab

Manipulate breadcrumbs with custom logic

clear_cache.php
<?php

apcu_clear_cache();
opcache_reset();
13.11.2025 | Nikolas Kopp

Clear APC & Opcache

PipelineWebhookEventResource.php


$event = new PipelineEvent($data, $project);
$this->eventDispatcher->dispatch($event, 'webhook');
$this->eventDispatcher->dispatch($event, 'webhook.' . $event->type());
$this->eventDispatcher->dispatch($event, 'webhook.' . $event->type() . '.' . $event->status());
01.04.2025 | Marc Hitscherich, Dominik Wille

Dispatch multiple pipeline events based on more specific contexts