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).
 

Issue:

You would like to order elements of an array for rendering based on a custom weight value.

Solution:

The code assigns a custom weight property to each element in an array and then sorts the array using a custom sorting function based on these weights. Here is the link to the documentation of the function: https://www.php.net/manual/en/function.uasort.php

The Drupal SortArray class is documented here: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Component%21Utility%21SortArray.php/class/SortArray/11.x

Weitere DevBits

21.11.2025 | Peter Majmesku

PHP 8.5 New Features: Pipe Operator and URI Extension

The New URI Extension

php_8.5_examples_uri_extension.php
<?php

##### URI Extension #####

// PHP 8.4 way

$components = parse_url('https://php.net/releases/8.4/en.php');

var_dump($components['host']);
// string(7) "php.net"

// PHP 8.5 URI Extension

use Uri\Rfc3986\Uri;

$uri = new Uri('https://php.net/releases/8.5/en.php');

var_dump($uri->getHost());
// string(7) "php.net"

The New Pipe Operator

php_8.5_pipe_operator.php
<?php

##### Pipe Operator #####

// PHP 8.4 way

$title = ' PHP 8.5 Released ';

$slug = strtolower(
    str_replace('.', '',
        str_replace(' ', '-',
            trim($title)
        )
    )
);

var_dump($slug);
// string(15) "php-85-released"

// PHP 8.5 Pipe Operator 

$title = ' PHP 8.5 Released ';

$slug = $title
    |> trim(...)
    |> (fn($str) => str_replace(' ', '-', $str))
    |> (fn($str) => str_replace('.', '', $str))
    |> strtolower(...);

var_dump($slug);
// string(15) "php-85-released"
php
13.11.2025 | Dominik Wille

Don't list a widget for a field type.

CustomWidget.php
  public static function isApplicable(FieldDefinitionInterface $field_definition) {
    if (isset($field_definition->getDisplayOptions('form')['type'])) {
      return $field_definition->getDisplayOptions('form')['type'] == 'slot_content_weights';
    }

    return FALSE;
  }

php
widgets
fields