summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2014-09-22 09:27:47 (GMT)
committerAlex Pott2014-09-22 09:27:47 (GMT)
commitbb3ee20742f2549b149ad39a0503d8c89d90daa4 (patch)
tree694f3a498b65b5959fc61fc898d16aa6e667be70
parent0096eb08399fff05e83a80e3752c16ebb25bc16a (diff)
Issue #2333395 by Jelle_S, attiks: Add sizes to template_preprocess_image.
-rw-r--r--core/includes/theme.inc6
-rw-r--r--core/modules/system/src/Tests/Theme/ImageTest.php47
2 files changed, 51 insertions, 2 deletions
diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index 3f6d623..d7387a2 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -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(
diff --git a/core/modules/system/src/Tests/Theme/ImageTest.php b/core/modules/system/src/Tests/Theme/ImageTest.php
new file mode 100644
index 0000000..f28e1f7
--- /dev/null
+++ b/core/modules/system/src/Tests/Theme/ImageTest.php
@@ -0,0 +1,47 @@
+<?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.');
+ }
+
+}