nerdfisch: DevBits

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

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
13.11.2025 | Michael Ebert

Warm your Drupal caches after a cache clear

my_module.services.yml
tags:
  - { name: cache_prewarmable }

MyPlugin.php
/**
  * Implements \Drupal\Core\PreWarm\PreWarmableInterface.
  */
public function preWarm(): void {
  $this->getDefinitions();
}

services
drush
caching
API
13.11.2025 | Michael Ebert

How to alter the langcode of a field for accessibility reasons

my_module.module
function my_module_preprocess_field(&$variables): void {
  if ($variables['element']['#bundle'] == 'my_bundle') {
    if (in_array($variables['element']['#field_name'], ['my_field'])) {
      $variables['attributes']['lang'] = 'en';
    }
  }
}

module
accessibility
multi-language
field management
13.11.2025 | Michael Ebert

How to translate menu link URIs in Drupal

menus
translation
multi-language
18.06.2024 | Dominik Wille

Prevent destination URLs from being indexed by search engines

robots.txt
Disallow: /*?destination=
Disallow: /*&destination=
robots.txt
SEO
copy-paste