By joachim on
Change record status:
Draft (View all draft change records)
Project:
Introduced in branch:
11.1.x
Introduced in version:
11.1.0
Issue links:
Description:
The InsertArray class provides two helper methods for inserting items into an array at a specific location. This is intended to help developers working with render arrays or form arrays.
public static function insertBefore(array &$array, mixed $key, array $insert_array): void
public static function insertAfter(array &$array, mixed $key, array $insert_array): void
Example:
use Drupal\Component\Utility\InsertArray;
$array = [
'first' => 'first item',
'third' => 'third item',
];
InsertArray::insertBefore($array, 'third', ['second' => 'second item']);
// $array is now:
// [
// 'first' => 'first item',
// 'second' => 'second item',
// 'third' => 'third item',
// ],
- Any number of items can be in the array being inserted.
- Keys can be numeric, but must not be duplicated.
Impacts:
Module developers