[2.0.7] Fix callback name fallback

Created on 8 August 2025, 29 days ago

Problem/Motivation

Related to 🐛 TypeError: ltrim(): Argument #1 ($string) must be of type string, Active so @juc1 will be credited.

We have an issue with this ComponentPluginManager method:

   protected function processDefinitionCategory(&$definition): void {
     // Name is mandatory, so undefined should never happen.
     $definition['label'] = $definition['name'] ?? $this->t('Undefined');
     $definition['category'] = $definition['group'] ?? $this->t('Other');
   }

The machine name would be better than 'Undefined'
'Undefined' is not a string but a TranslatableMarkup so it breaks type checking
label is replaced but not name so it doesn't work properly when

Proposed resolution

  • use the machine name as a fallback instead of "Undefined"
  • cast TranslatableMarkup as a string
  • Remove label? Where is it used?
🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇫🇷France pdureau Paris

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @pdureau
  • 🇬🇧United Kingdom juc1

    pdureau credited juc1 .

  • @pdureau opened merge request.
  • 🇫🇷France pdureau Paris

    Hi Grimreaper,

    this issue is related to 🐛 Library: other group placed first Active which has introduced label & category properties.

    I changed the logic to fix the TypeError but it may not be enough.

    Are we comfortable with the addition of label & category properties to all definitions? We are adding them to a kind of public interface.

    For category, we may have no choice.

    But for label, why not use the $label_key = 'name' property instead?

      public function getSortedDefinitions(?array $definitions = NULL, $label_key = 'label');
      public function getSortedDefinitions(?array $definitions = NULL, $label_key = 'label');
    
  • 🇫🇷France Grimreaper France 🇫🇷

    why not use the $label_key = 'name' property instead?

    It was to minimize changes of 🐛 Library: other group placed first Active because default value for $label_key of CategorizingPluginManagerTrait is "label". So to avoid to edit all calls to getSortedDefinitions or getGroupedDefinitions, especially in case of usage in other contrib ui_patterns modules, or custom code.

    So to avoid a breaking change.

  • 🇫🇷France Grimreaper France 🇫🇷

    I changed the moment default name is set.

    Ok on my side, please retest on your side.

  • 🇫🇷France pdureau Paris

    automerge set

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • 🇫🇷France pdureau Paris

    The automerge didn't work. So merging it manually now.

    • pdureau committed f5d0ae83 on 2.0.x
      Issue #3540536 by pdureau, grimreaper, juc1: Fix component name fallback
      
  • 🇬🇧United Kingdom juc1

    I have tested and it seems that ui_patterns 2.0.x-dev fixes the php error caused by any sdc having a missing "name" key:

    =============================

    previous version of UI Patterns:

    /admin/appearance/ui/components is ok

    but /admin/structure/types/manage/page/display/default/display_builder =

    php error:

    ltrim(): Argument #1 ($string) must be of type string, Drupal\Core\StringTranslation\TranslatableMarkup given in ltrim() (line 569 of /var/www/html/web/modules/contrib/display_builder/src/Plugin/display_builder/Island/ComponentLibraryPanel.php).

    ===================

    ui_patterns 2.0.x-dev :

    /admin/structure/types/manage/page/display/default/display_builder loads normally

    Clear caches =

    Warning: Undefined array key "name" in Drupal\ui_patterns\ComponentPluginManager->processDefinitionCategory() (line 151 of modules/contrib/ui_patterns/src/ComponentPluginManager.php).

    ========================================

    By the way, with the current warning message I have to go hunting through numerous sdc's to find the culprit sdc with a missing name key - so it might be good if the warning message from ComponentPluginManager.php could include the culprit component ID.

Production build 0.71.5 2024