Mixed usage of arrays and Attribute objects in rendering causes issues due to the fact that Array objects are not correctly working with all array_* functions and NestedArray methods.
In order to manage properly checking the existence of an attribute, and merging attributes a new Drupal\Core\Template\AttributeHelper class is introduced, with two static methods:
::attributeExists::mergeCollections
In addition, two methods are added to the Drupal\Core\Template\Attribute class, that allow to manage the case when it's definite that we are dealing with Attribute objects:
::hasAttribute::merge
Themers can leverage on the AttributeHelper methods instead of array functions or NestedArray, without having to predetermine the type of the argument,
for checking existence of an attribute:
before:
if (array_key_exists($key, $variables['attributes'])) {
continue;
}
after
if (AttributeHelper::attributeExists($key, $variables['attributes'])) {
continue;
}
for merging two collections of attributes:
before:
$variables['attributes'] = NestedArray::mergeDeep($variables['attributes'], $variables[$key]['#attributes']);
after
$variables['attributes'] = AttributeHelper::mergeCollections($variables['attributes'], $variables[$key]['#attributes']);