Type error with callback

Created on 2 August 2024, 4 months ago
Updated 15 September 2024, 2 months ago

Problem/Motivation

So I am testing UI Patterns 2 with UI Styles for 📌 [2.0.0-beta2] Drag & drop in layout builder Postponed .

When adding styles in Layout Builder, I obtained the following fatal error:

TypeError: Drupal\ui_styles\Render\TrustedCallbackWrapper::doTrustedCallback(): Argument #1 ($callback) must be of type callable, string given, called in /project/app/modules/contrib/ui_styles/src/Render/Element.php on line 268 in Drupal\ui_styles\Render\TrustedCallbackWrapper->doTrustedCallback() (line 57 of core/lib/Drupal/Core/Security/DoTrustedCallbackTrait.php).
Drupal\ui_styles\Render\Element::doCallback('#pre_render', 'ui_patterns_library.component_element_alter:alter', Array) (Line: 196)
Drupal\ui_styles\Render\Element::isRenderElementAcceptingAttributes(Array) (Line: 135)
Drupal\ui_styles\Render\Element::isAcceptingAttributes(Array) (Line: 88)
Drupal\ui_styles\Render\Element::wrapElementIfNotAcceptingAttributes(Array) (Line: 326)
Drupal\ui_styles\StylePluginManager->addClasses(Array, Array, Array) (Line: 115)
Drupal\ui_styles_layout_builder\HookHandler\EntityViewAlter->addStylesToSection(Array, Object, 0) (Line: 96)
Drupal\ui_styles_layout_builder\HookHandler\EntityViewAlter->alter(Array, Object, Object) (Line: 36)
ui_styles_layout_builder_entity_view_alter(Array, Object, Object) (Line: 552)
Drupal\Core\Extension\ModuleHandler->alter('node_view', Array, Object, Object) (Line: 305)
Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array) (Line: 239)
Drupal\Core\Entity\EntityViewBuilder->build(Array)
call_user_func_array(Array, Array) (Line: 113)
Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 870)
Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 432)

Steps to reproduce

Proposed resolution

Transform pre_render insertion into proper callable syntax.

Remaining tasks

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇫🇷France Grimreaper France 🇫🇷

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

Merge Requests

Comments & Activities

  • Issue created by @Grimreaper
  • 🇫🇷France Grimreaper France 🇫🇷

    If a service is injected then, there is a serialization problem.

    Can the alter method of modules/ui_patterns_library/src/Element/ComponentElementAlter.php be transformed into static. Then I guess no more need that it to be a service.

  • 🇫🇷France pdureau Paris

    Christian is the one who built the ComponentElementAlter architecture:

    So, let's check with him what can be done, and it would be better to keep the 3 with similar structure & mechanisms .

    By the way, it is OK to modify modules/ui_patterns_library/src/Element/ComponentElementAlter.php because it is stable, but most of the ui_patterns_library will be totally revamped for beta2.

  • Assigned to Christian.wiedemann
  • 🇫🇷France pdureau Paris

    Christian, can you answer to Florent?

  • Assigned to Grimreaper
  • 🇫🇷France pdureau Paris

    One month after Florent has created this ticket, I am doing the same tests.

    I don't meet the TypeError: Argument #1 ($callback) must be of type callable, string given or any other fatal error.
    But the styles are not applied.

    For example, if I create a section with Bootstrap alert component as a layout and I apply the bg-danger style utility to the heading slot, I see the style inside the region (so a temporary place sued by the layout PAI) and not inside the component slot (what will be rendered using SDC):

    array:9 [
      "#type" => "component"
      "#component" => "ui_suite_bootstrap:alert"
      "#layout" => Drupal\ui_patterns_layouts\Plugin\Layout\ComponentLayout {#6647
        #pluginId: "ui_patterns:ui_suite_bootstrap:alert"
        ...
      }
      "#slots" => array:1 [
        "heading" => array:1 [
          "7562fca6-6168-41e6-aaa1-ee490bdff420" => array:10 [
            "#theme" => "block"
            "#plugin_id" => "field_block:node:article:title"
            "content" => array:2 [...]
            "#attributes" => []
          ]
        ]
      ]
      "heading" => array:2 [
        "7562fca6-6168-41e6-aaa1-ee490bdff420" => array:10 [
          "#theme" => "block"
          "content" => array:2 [...]
          "#attributes" => []
        ]
        "#attributes" => array:1 [
          "class" => array:1 [
            "colors_background_color" => "bg-danger"
          ]
        ]
      ]
      "message" => array:1 []
    ]
      

    But this fix (which is the same 📌 [2.0.0-beta2] Drag & drop in layout builder Postponed , with only one line more) seems to work:

    --- a/modules/ui_patterns_layouts/src/Element/ComponentAlterer.php
    +++ b/modules/ui_patterns_layouts/src/Element/ComponentAlterer.php
    @@ -39,8 +39,14 @@ class ComponentAlterer implements TrustedCallbackInterface {
           foreach (Element::children($element[$region_id]) as $block_id) {
             $element["#slots"][$region_id][$block_id] = $element[$region_id][$block_id];
           }
    -      if (isset($element[$region_id]['#attributes'])) {
    +      if (isset($element[$region_id]['#attributes']) && isset($element["#slots"][$region_id])) {
             $element['#region_attributes'][$region_id] = new Attribute($element[$region_id]['#attributes']);
    +        $element["#slots"][$region_id] = [
    +          "#type" => "html_tag",
    +          "#tag" => "div",
    +          "#attributes" => $element[$region_id]['#attributes'],
    +          "content" => $element["#slots"][$region_id],
    +        ];
           }
           unset($element[$region_id]);
         }
    

    Florent, I give the ticket back to you to check my analysis.

  • Assigned to pdureau
  • 🇫🇷France Grimreaper France 🇫🇷

    Hello,

    As seen together, I still reproduce the problem on my env with Core 10.3.3 and latest dev version of UI Patterns 2.

  • 🇫🇷France pdureau Paris

    I am not reproducing with drupal 11.0

    I will test with drupal 10.3

  • Assigned to Grimreaper
  • 🇫🇷France pdureau Paris

    I am still not reproducing.

    Here is my 10.2 install

    $ vendor/bin/drush st
    Drupal version   : 10.3.3                                                        
    Site URI         : http://default                                                
    DB driver        : sqlite                                                        
    DB port          :                                                               
    DB username      :                                                               
    DB name          : db.sqlite                                                     
    Database         : Connected                                                     
    Drupal bootstrap : Successful                                                    
    Default theme    : ui_suite_bootstrap                                            
    Admin theme      : claro                                                         
    PHP binary       : /usr/bin/php                                                  
    PHP config       : /etc/php.ini                                                  
    PHP OS           : Linux                                                         
    PHP version      : 8.3.11                                                        
    Drush script     : /home/pierre/Projects/Drupal/d10/vendor/bin/drush             
    Drush version    : 12.5.3.0                                                      
    Drush temp       : /tmp                                                          
    Drush configs    : /home/pierre/Projects/Drupal/d10/vendor/drush/drush/drush.yml 
    Install profile  : standard                                                      
    Drupal root      : /home/pierre/Projects/Drupal/d10/web                          
    Site path        : sites/default                                                 
    Files, Public    : sites/default/files                                           
    Files, Temp      : /tmp                      
    

    With up-to-date UI Patterns 2

    commit 701bb9b423c19ea8ceaa838b0376cfb70db3fa31 (HEAD -> 2.0.x, origin/2.0.x)
    Author: christian.wiedemann <7688-Christian.wiedemann@users.noreply.drupalcode.org>
    Date:   Fri Sep 6 19:53:45 2024 +0000
    
        Issue #3469814 by christian.wiedemann, just_like_good_vibes: [2.0.0-beta2] Kernel Source Tests
    
  • 🇫🇷France Grimreaper France 🇫🇷

    We investigated with @pdureau.

    Core is calling pre_renders with special handling which make it work.

    The light way UI Styles is using to trigger pre_renders should reproduce this handling or call directly Core services/methods.

  • 🇫🇷France Grimreaper France 🇫🇷
    TypeError: Drupal\Core\Render\Element\ComponentElement::generateComponentTemplate(): Argument #3 ($slots_alter_callbacks) must be of type array, null given, called in /project/app/core/lib/Drupal/Core/Render/Element/ComponentElement.php on line 66 in Drupal\Core\Render\Element\ComponentElement->generateComponentTemplate() (line 98 of core/lib/Drupal/Core/Render/Element/ComponentElement.php).
    Drupal\Core\Render\Element\ComponentElement->preRenderComponent(Array)
    call_user_func_array(Array, Array) (Line: 113)
    Drupal\ui_styles\Render\TrustedCallbackWrapper->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 395)
    
  • Issue was unassigned.
  • Status changed to Needs review 2 months ago
  • 🇫🇷France Grimreaper France 🇫🇷

    Merged for UI Styles, I will do a release.

    In needs review for UI Patterns.

    • grimreaper committed 6f6bc5ed on 8.x-1.x
      Issue #3465497 by grimreaper, pdureau: Type error with callback
      
  • Assigned to pdureau
  • Assigned to Grimreaper
  • Status changed to RTBC 2 months ago
  • 🇫🇷France pdureau Paris

    UI Patterns MR is OK: https://git.drupalcode.org/project/ui_patterns/-/merge_requests/208/diffs

        $element = $this->buildProps($element, $component, $configuration, $contexts);
        $element = $this->buildSlots($element, $component, $configuration, $contexts);
        $element['#propsAlter'] = [];
        $element['#slotsAlter'] = [];
        return $element;
      }
    
  • Issue was unassigned.
  • Status changed to Fixed 2 months ago
  • 🇫🇷France Grimreaper France 🇫🇷
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024