Error: [] operator not supported for strings in HOOK_form_alter()

Created on 1 March 2023, about 2 years ago
Updated 2 March 2023, about 2 years ago

Problem/Motivation

When accessing to view the documentation generated for a project that includes _form_alter that adds a class, it crashes the site:

The website encountered an unexpected error. Please try again later.
Error: [] operator not supported for strings in MYTHEME_form_alter() (line ... of themes/custom/MYTHEME/MYTHEME.theme).

Steps to reproduce

Have a custom theme or module that uses HOOK_form_alter and adds a CSS class to the forms, for example:

function MYTHEME_form_alter(&$form, &$form_state, $form_id) {
if (array_key_exists('#attributes', $form)) {
    $form['#attributes']['class'][] = 'my-custom-class';
  }
}

Proposed resolution

Change:

$form['#attributes']['class'] = 'api-search-form';

To:

$form['#attributes']['class'] = ['api-search-form'];

in SearchForm.php, buildForm function, line 34.

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΎUruguay fanton Uruguay

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

Comments & Activities

Production build 0.71.5 2024