InvalidArgumentException: "attributes" is an invalid render array key

Created on 4 September 2024, 3 months ago
Updated 17 September 2024, 2 months 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

Merge Requests

Comments & Activities

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

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

  • Status changed to Postponed: needs info 2 months 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.

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

    I wish that I could readily recreate this issue. I made some minor changes but since I can't recreate the problem, I can't confirm this fixes anything.

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

    I have made the code changes that I had in this MR in the related issue. There is now a new release. As I said, since I can't recreate the error described, I'm not sure if this fixed it or not. Let me know. If it does not, the more information you can provide me on how to recreate it, the better.

  • πŸ‡¨πŸ‡¦Canada _randy

    @shelane
    I'll give this a go in the next few days. I appreciate the update and attempt.

  • πŸ‡ΊπŸ‡ΈUnited States shelane
  • πŸ‡¨πŸ‡¦Canada _randy

    For anyone else who runs into this problem with this module or any others that provide attributes during the render process:

    I found where, not necessarily why, this issue occurs.

    This issue manifests itself during rendering of a Twig template specifically when you are looping over view output rows.
    If you have a twig template for a specific view and display, you'll probably do something like this within it:

    <div class="wrapper">
        {% for row in rows %}
          <div class="output-card">
            {{ row }}
          </div>
        {% endfor %}
      </div>
    

    This worked up until ~Drupal 10.2.9. For whatever reason in D10.3+ in conjunction with this module, you'd get an error thrown about "attributes" being an object, not an array.

    The fix for this issue was to change the output in the Twig template to:

    <div class="wrapper">
        {% for row in rows %}
          <div class="output-card">
           <div{{ row.attributes }}>{{ row.content }}</div>
          </div>
        {% endfor %}
      </div>
    

    Where you now must output the row attributes separately from the row content.

    Thanks to @shelane for poking around enough until I was able to find a solution to the issue!

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

    Does this mean that I can mark this as fixed?

  • πŸ‡΅πŸ‡±Poland piotr pakulski Poland πŸ‡ͺπŸ‡Ί

    #20 helped me with solving this error - not for this module but for some custom implementation. Thanks - this is the path for fixing this error.

Production build 0.71.5 2024