Non-array values for #ajax

Created on 16 January 2016, about 9 years ago
Updated 7 May 2024, 10 months ago

Problem/Motivation

Our documentation specifies that #ajax items in render arrays must be arrays. RenderElement::preRenderAjaxForm() confirms this behavior. Yet in three places we set '#ajax' => TRUE in render arrays.

Error:

Cannot use a scalar value as an array in Drupal\Core\Render\Element\RenderElement::preRenderAjaxForm() (line 315 of /<redacted>/web/core/lib/Drupal/Core/Render/Element/RenderElement.php)

Steps to reproduce

Run MR's test without a fix.
Or try related issue's MR without this change/fix https://git.drupalcode.org/project/drupal/-/merge_requests/4053/diffs#01...

      // Workaround for https://www.drupal.org/project/drupal/issues/2651418.
      // @todo Remove the below when the referenced issue is fixed.
      foreach (Element::children($sub_handler_settings) as $key) {
        if (\array_key_exists('#ajax', $sub_handler_settings[$key]) &&
          !\is_array($sub_handler_settings[$key]['#ajax'])
        ) {
          $sub_handler_settings[$key]['#ajax'] = [];
        }

        foreach (Element::children($sub_handler_settings[$key]) as $sub_key) {
          if (\array_key_exists('#ajax', $sub_handler_settings[$key][$sub_key]) &&
            !\is_array($sub_handler_settings[$key][$sub_key]['#ajax'])
          ) {
            $sub_handler_settings[$key][$sub_key]['#ajax'] = [];
          }
        }
      }

Proposed resolution

- Update \Drupal\Core\Render\Element\Ajax documenation.
- Make sure '#ajax' => TRUE are accepted - transform into array in core/lib/Drupal/Core/Render/Element/RenderElement.php

Remaining tasks

- Update drupal.org and api.drupal.org documentations after commit.

User interface changes

None.

API changes

None.

Data model changes

None.

๐Ÿ› Bug report
Status

Fixed

Version

10.2 โœจ

Component
Ajaxย  โ†’

Last updated 4 days ago

Created by

๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom Xano Southampton

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024