InvalidArgumentException: "attributes" is an invalid render array key

Created on 4 September 2024, 14 days ago
Updated 17 September 2024, 1 day ago

Drupal 10.3.2, using Bootstrap 5 theme (theme version 3.0.14), we have a view for which the Bootstrap attributes are being added as a Drupal\Core\Template\Attribute which then causes the site to crash with the following error:

InvalidArgumentException: "attributes" is an invalid render array key. Value should be an array but got a object. in Drupal\Core\Render\Element::children() (line 97 of ...\core\lib\Drupal\Core\Render\Element.php).

The only way to resolve this issue was to back-level the Views Bootstrap module to 5.5.0-alpha1.

Posting this in the event someone else has run into this issue and if they've found a solution other than altering the theme.inc file for the module.

EDIT: This is using the Bootstrap Grid display format.

πŸ› Bug report
Status

Postponed: needs info

Version

5.5

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada _randy

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

Comments & Activities

  • Issue created by @_randy
  • πŸ‡¨πŸ‡¦Canada _randy
  • πŸ‡©πŸ‡ͺGermany marc.bau

    Have seen the same issue and not found a solution yet.

  • Status changed to Postponed: needs info 8 days ago
  • πŸ‡ΊπŸ‡ΈUnited States shelane

    Can you give me more info on steps to reproduce this issue? What style did you have it set to? What are the options? What is the data type in the view?

  • πŸ‡¨πŸ‡¦Canada _randy

    @shelane
    The data in the view is two content types (just regular old nodes).
    Configured to show rendered entity content, not fields, using a specific display mode.

    Format: Bootstrap Grid - Settings were straight out of the box - no changes.

    As noted, this worked fine prior to the Beta and RC releases.

    Issue is in template_preprocess_views_bootstrap_grid function in views_bootstrap.theme.inc file.
    In the alpha version only $vars['row_attributes'] was set to "new Attribute()".
    In the latest versions, $vars['attributes'] is set to "new Attribute(['class' => 'grid']);"

    This creates the issue with Drupal core.

  • πŸ‡©πŸ‡ͺGermany marc.bau

    Same here - "Format: Bootstrap Grid - Settings were straight out of the box - no changes."

  • πŸ‡ΊπŸ‡ΈUnited States shelane

    Interesting. I still haven't been able to recreate the issue. Looking right at the Drupal core views module and their views.theme.inc file uses the new Attribute method. For instance:

      foreach ($variables['rows'] as $num => $row) {
        $variables['rows'][$num]['attributes'] = [];
        if ($row_class = $handler->getRowClass($num)) {
          $variables['rows'][$num]['attributes']['class'][] = $row_class;
        }
        $variables['rows'][$num]['attributes'] = new Attribute($variables['rows'][$num]['attributes']);
      }
    
      if (empty($variables['rows']) && !empty($options['empty_table'])) {
        $build = $view->display_handler->renderArea('empty');
        $variables['rows'][0]['columns'][0]['content'][0]['field_output'] = $build;
        $variables['rows'][0]['attributes'] = new Attribute(['class' => ['odd']]);
        // Calculate the amounts of rows with output.
        $variables['rows'][0]['columns'][0]['attributes'] = new Attribute([
          'colspan' => count($variables['header']),
          'class' => ['views-empty'],
        ]);
      }
    

    I also see it in the 9.5.x branch code base.

  • πŸ‡¨πŸ‡¦Canada _randy

    Specifically in my case, it's the following that is the issue:

        $vars['rows'][$id]['attributes'] = new Attribute();
        if ($row_class = $view->style_plugin->getRowClass($id)) {
          $vars['rows'][$id]['attributes']->addClass($row_class);
        }
        $vars['rows'][$id]['attributes']->addClass($col_classes);
      }

    If I comment out the $vars['rows'][$id]['attributes'] = new Attribute(); and the $vars['rows'][$id]['attributes']->addClass($col_classes); lines, it works just fine.

    I haven't had a chance to dig further why it's the row attributes. The only thing I can think of right now is that we're not using fields? We're using display modes on rendered entities. Not sure.

  • πŸ‡ΊπŸ‡ΈUnited States shelane

    Can you post a screenshot of your views settings for me?

  • πŸ‡¨πŸ‡¦Canada _randy

    I've added 2 screenshots. But I don't think they'll help.

    You're 100% right in that this works out of the box (I tried on simplytest.me)

    Perhaps this is a Bootstrap5 theme related issue or something in Layout Builder ( https://www.drupal.org/project/bootstrap5 β†’ ).

    But it manifests itself first here. I'll have to dig some more.

  • πŸ‡ΊπŸ‡ΈUnited States shelane

    I installed the Bootstrap5 theme (previously used Barrio for testing) and it worked fine with a similar setup. I don't use Layout Builder, so I'm not sure how I would set that up to test to see if that's associated.

    Do you see this happening on any type other than grid?

    Out of curiosity, have you tried default responsive grid option? I think it works great and even with Bootstrap, it's my preferred grid.

  • πŸ‡©πŸ‡ͺGermany marc.bau

    I think I had this issue when I tried to added a css class via a preprocess function to something. The bad - I deleted the code and cannot remember it exactly.

Production build 0.71.5 2024