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

Migrated issue

Reported by: fanton

Related to !2 (merged)

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.

Assignee Loading
Time tracking Loading