Skip to content
Snippets Groups Projects
Commit bb3ee207 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2333395 by Jelle_S, attiks: Add sizes to template_preprocess_image.

parent 0096eb08
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -1073,11 +1073,13 @@ function template_preprocess_links(&$variables) {
* - title: The title text is displayed when the image is hovered in some
* popular browsers.
* - attributes: Associative array of attributes to be placed in the img tag.
* - sizes: The sizes attribute for viewport-based selection of images.
* - http://www.whatwg.org/specs/web-apps/current-work/multipage/embedded-content.html#introduction-3:viewport-based-selection-2
*/
function template_preprocess_image(&$variables) {
$variables['attributes']['src'] = file_create_url($variables['uri']);
foreach (array('width', 'height', 'alt', 'title') as $key) {
foreach (array('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.
......@@ -2266,7 +2268,7 @@ function drupal_common_theme() {
// - http://dev.w3.org/html5/spec/Overview.html#alt
// The title attribute is optional in all cases, so it is omitted by
// default.
'variables' => array('uri' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => '', 'title' => NULL, 'attributes' => array()),
'variables' => array('uri' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => '', 'title' => NULL, 'attributes' => array(), 'sizes' => NULL),
'template' => 'image',
),
'breadcrumb' => array(
......
<?php
/**
* @file
* Definition of Drupal\system\Tests\Theme\ImageTest.
*/
namespace Drupal\system\Tests\Theme;
use Drupal\simpletest\KernelTestBase;
/**
* Tests built-in image theme functions.
*
* @group Theme
*/
class ImageTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('system');
/**
* Tests that an image with the sizes attribute is output correctly.
*/
function testThemeImageWithSizes() {
// Test with multipliers.
$sizes = '(max-width: ' . rand(10, 30) . 'em) 100vw, (max-width: ' . rand(30, 50) . 'em) 50vw, 30vw';
$image = array(
'#theme' => 'image',
'#sizes' => $sizes,
'#uri' => '/core/misc/druplicon.png',
'#width' => rand(0, 1000) . 'px',
'#height' => rand(0, 500) . 'px',
'#alt' => $this->randomMachineName(),
'#title' => $this->randomMachineName(),
);
$this->render($image);
// Make sure sizes is set.
$this->assertRaw($sizes, 'Sizes is set correctly.');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment