diff --git a/core/includes/theme.inc b/core/includes/theme.inc index f8febe712b..e01732ad3a 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -815,13 +815,23 @@ function template_preprocess_image(&$variables) { $variables['attributes']['srcset'] = implode(', ', $srcset); } + // Ensure that attributes can be accessed as an array, for PHP 7.4 + // compatibility. + if ($variables['attributes'] instanceof Attribute) { + $attributes_array = $variables['attributes']->toArray(); + } + elseif (is_array($variables['attributes'])) { + $attributes_array = $variables['attributes']; + } + else { + $attributes_array = []; + } + foreach (['width', 'height', 'alt', 'title', 'sizes'] as $key) { if (isset($variables[$key])) { // If the property has already been defined in the attributes, do not // override, including NULL. - // @todo the cast to array below is needed to get tests pass on PHP 7.4, - // but requires a separate issue to fix. - if (array_key_exists($key, (array) $variables['attributes'])) { + if (array_key_exists($key, $attributes_array)) { continue; } $variables['attributes'][$key] = $variables[$key];