I've implemented Preprocess plugin for page and trying to run this code in proprocessVariables() method:

if (array_key_exists('newsmenu', $variables['page']['header'])) {
      $variables->addClass('with-section-menu', DrupalAttributes::CONTENT);
}

but this code fails with

Warning: array_unique() expects parameter 1 to be array, object given in Drupal\bootstrap\Utility\Attributes->getClasses() (line 63 of themes/bootstrap/src/Utility/Attributes.php).

Here is method code

public function &getClasses() {
    $classes = &$this->offsetGet('class', []);
    $classes = array_unique($classes);
    return $classes;
  }

and in case there is no defined classes yet $this->offsetGet('class', []) returns instance of \Drupal\Core\Template\AttributeArray instead of just array.

Is this a bug or I'm doing something wrong?

CommentFileSizeAuthor
#2 2882648-2.patch429 byteststoeckler
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

berdyshev created an issue. See original summary.

tstoeckler’s picture

Category: Support request » Task
Status: Active » Needs review
FileSize
429 bytes

I've hit a similar issue where custom code was incorrectly setting $element['#attributes']['class'] to a string instead of an array. That is easy to fix, but I'm still wondering whether Bootstrap should be a bit more forgiving here, especially since there are also still instances in core of this.

Attached patch should fix this.

@berdyshev could you try it out whether this fixes your issue as well?

markhalliwell’s picture

Category: Task » Support request
Status: Needs review » Closed (works as designed)
Related issues: +#2269653: [bootstrap][policy][7.x-3.x] CSS Classes (Fatal error: [] operator not supported for strings)

Classes should be an array of strings. Find offending code and fix it.