12.03.2026 | Peter Gerken

How to build a menu render array programmatically

UserDashboardController.php
    $menu_name = 'my_custom_menu_machine_name';
  
    $menu_tree = \Drupal::menuTree();
    // Build the typical default set of menu tree parameters.
    $parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
    // Load the tree based on this set of parameters.
    $tree = $menu_tree->load($menu_name, $parameters);
    // Transform the tree using the manipulators you want.
    $manipulators = [
      // Only show links that are accessible for the current user.
      ['callable' => 'menu.default_tree_manipulators:checkAccess'],
      // Use the default sorting of menu links.
      ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
    ];
    
    $tree = $menu_tree->transform($tree, $manipulators);

    $menu = $menu_tree->build($tree);

     // Set theme for custom styling.
    $menu['#theme'] = 'my_custom_theme';

Issue:

Sometimes you might need to load and render a custom menu inside a controller – for example on a user dashboard – with access checking, default sorting, and a custom theme hook applied.

Solution:

This is a small but useful snippet for generating menu tree render arrays for any menu from a custom controller.

Just change $menu_name to the machine name of your menu.

Extra: For similar use cases, you might want to use the Drupal Twig Tweak module. You can also use the {{ drupal_menu('my_menu') }} function directly in your template.

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