nerdfisch: DevBits

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

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';
php
drupal 9
controller
menus and navigation
arrays
05.03.2026 | Lothar Ferreira Neumann

Update a diverged local branch from remote to be on track again

stagePush.sh
git fetch --all
git checkout stage
# This resets your local stage branch to the version of remote.
# The origin/stage could diverge from the local due to resetting the branch.
git reset --hard origin/stage
git merge origin/BRANCHNAME
git push origin stage
git checkout BRANCHNAME
sh
git
stage environment
git branching
05.03.2026 | Michael Ebert

How to output the processed value of a WYSIWYG text field

EntityFieldValue.php
$raw_value = $entity->my_text_field->value;
echo $raw_value;  // Outputs the raw data
EntityFieldProcessed.php
$processed_value = $entity->my_text_field->processed;
echo $processed_value;  // Outputs the processed and safe-to-display data
drupal
fields
WYSIWYG
26.02.2026 | Pascal Crott

Make Gin node edit form available for all content entities

Issue:

You want to use the Gin edit form for all content entities, including custom entities.

Solution:

The Gin Everywhere module was developed to solve this specific issue.

theme
gin
Gin backend theme
backend
26.02.2026 | Holger Weischenberg

Using CSS media queries and CSS variables in JavaScript

cssMediaQuery.js
// Retrieve all computed styles from the root HTML element (:root)
const rootStyles = getComputedStyle(document.documentElement);

// Get the value of the CSS variable --Breakpoint.
// Use a fallback (e.g., 768px) if the variable is not defined.
const breakpoint = rootStyles.getPropertyValue('--Breakpoint').trim() || '768px';

// Create a MediaQueryList based on the breakpoint value
const mediaQuery = window.matchMedia(`(min-width: ${breakpoint})`);

// Function that runs when the viewport is at or above the breakpoint
function handleDesktopLayout() {
    console.log("Viewport is at or above the breakpoint.");
}

// Function that runs when the viewport drops below the breakpoint
function handleMobileLayout() {
    console.log("Viewport is below the breakpoint.");
}

// Early return for the initial state:
// If the viewport does NOT meet the breakpoint requirement, run mobile logic and exit
if (!mediaQuery.matches) {
    handleMobileLayout();
    // No desktop logic needed at this point
} else {
    handleDesktopLayout();
}

// Listen for changes in viewport size relative to the breakpoint
mediaQuery.addEventListener('change', (event) => {
    if (event.matches) {
        // The viewport just became equal to or larger than the breakpoint
        handleDesktopLayout();
    } else {
        // The viewport just became smaller than the breakpoint
        handleMobileLayout();
    }
});
js