diff --cc core/modules/responsive_image/lib/Drupal/responsive_image/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php index 8f935c0,3b86835..0000000 --- a/core/modules/responsive_image/lib/Drupal/responsive_image/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php +++ b/core/modules/responsive_image/lib/Drupal/responsive_image/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php @@@ -114,22 -114,15 +114,15 @@@ class ResponsiveImageFormatter extends */ public function viewElements(FieldItemListInterface $items) { $elements = array(); -- // Check if the formatter involves a link. - if ($this->getSetting('image_link') == 'content') { - $uri = $items->getEntity()->urlInfo(); - // @todo Remove when theme_responsive_image_formatter() has support for route name. - $uri['path'] = $items->getEntity()->getSystemPath(); - } - elseif ($this->getSetting('image_link') == 'file') { - $link_file = TRUE; - } ++ // Check if the formatter involves a link. $breakpoint_styles = array(); - $fallback_image_style = ''; + $image_styles = array_keys(image_style_options(FALSE)); + $fallback_image_style = reset($image_styles); - $picture_mapping = entity_load('picture_mapping', $this->getSetting('picture_mapping')); - if ($picture_mapping) { - foreach ($picture_mapping->mappings as $breakpoint_name => $multipliers) { + $responsive_image_mapping = entity_load('responsive_image_mapping', $this->getSetting('responsive_image_mapping')); + if ($responsive_image_mapping) { + foreach ($responsive_image_mapping->mappings as $breakpoint_name => $multipliers) { // Make sure there are multipliers. if (!empty($multipliers)) { // Make sure that the breakpoint exists and is enabled. @@@ -163,22 -156,52 +156,53 @@@ } foreach ($items as $delta => $item) { - if (isset($link_file)) { - $uri = array( - 'path' => file_create_url($item->entity->getFileUri()), - 'options' => array(), - ); + $item = $item->getValue(TRUE); + if (!isset($item['uri']) && isset($item['entity'])) { + $item['uri'] = $item['entity']->getFileUri(); + } ++ + if (isset($item['title']) && drupal_strlen($item['title']) == 0) { + unset($item['title']); } - $elements[$delta] = array( - '#theme' => 'responsive_image_formatter', + $picture = array( + '#theme' => 'picture', + '#style_name' => $fallback_image_style, + '#uri' => $item['uri'], + '#width' => $item['width'], + '#height' => $item['height'], + '#breakpoints' => $breakpoint_styles, + '#alt' => isset($item['alt']) || array_key_exists('alt', $item) ? $item['alt'] : NULL, + '#title' => isset($item['title']) ? $item['title'] : NULL, + '#attributes' => isset($item['attributes']) ? $item['attributes'] : NULL, '#attached' => array('library' => array( - array('picture', 'picturefill'), + 'core/picturefill', )), - '#item' => $item, - '#image_style' => $fallback_image_style, - '#breakpoints' => $breakpoint_styles, - '#path' => isset($uri) ? $uri : '', ); + + // Check if the formatter involves a link. + if ($this->getSetting('image_link') == 'content') { + $uri = $entity->uri(); + } + elseif ($this->getSetting('image_link') == 'file') { + $uri = array( + 'path' => file_create_url($item['uri']), + 'options' => array(), + ); + } + if (isset($uri)) { + $elements[$delta] = array( + '#type' => 'link', + '#href' => $uri['path'], + '#title' => $picture, + '#options' => array( + 'html' => TRUE, + 'attributes' => isset($uri['attributes']) ? $uri['attributes'] : array(), + ), + ); + } + else { + $elements[$delta] = $picture; + } } return $elements; diff --cc core/modules/responsive_image/responsive_image.module index 5113afc,331c6d0..0000000 --- a/core/modules/responsive_image/responsive_image.module +++ b/core/modules/responsive_image/responsive_image.module @@@ -138,60 -124,9 +123,10 @@@ function responsive_image_theme() } /** - * Returns HTML for a responsive image field formatter. - * - * @param array $variables - * An associative array containing: - * - item: An ImageItem object. - * - image_style: An optional image style. - * - path: An optional array containing the link 'path' and link 'options'. - * - breakpoints: An array containing breakpoints. - * - * @ingroup themeable - */ - function theme_responsive_image_formatter($variables) { - $item = $variables['item']; - if (!isset($variables['breakpoints']) || empty($variables['breakpoints'])) { - $image_formatter = array( - '#theme' => 'image_formatter', - '#item' => $item, - '#image_style' => $variables['image_style'], - '#path' => $variables['path'], - ); - return drupal_render($image_formatter); - } - - $responsive_image = array( - '#theme' => 'responsive_image', - '#width' => $item->width, - '#height' => $item->height, - '#style_name' => $variables['image_style'], - '#breakpoints' => $variables['breakpoints'], - ); - if (isset($item->uri)) { - $responsive_image['#uri'] = $item->uri; - } - elseif ($entity = $item->entity) { - $responsive_image['#uri'] = $entity->getFileUri(); - $responsive_image['#entity'] = $entity; - } - $responsive_image['#alt'] = $item->alt; - if (drupal_strlen($item->title) != 0) { - $responsive_image['#title'] = $item->title; - } - // @todo Add support for route names. - if (isset($variables['path']['path'])) { - $path = $variables['path']['path']; - $options = isset($variables['path']['options']) ? $variables['path']['options'] : array(); - $options['html'] = TRUE; - return l($responsive_image, $path, $options); - } - - return drupal_render($responsive_image); - } - - /** + * Returns HTML for a responsive image. + * Prepares variables for picture templates. + * Returns HTML for a picture. + * Default template: picture.html.twig. * * @param $variables * An associative array containing: @@@ -353,7 -265,9 +265,9 @@@ function responsive_image_get_image_dim 'height' => $variables['height'], ); - entity_load('image_style', $variables['style_name'])->transformDimensions($dimensions); + if ($image_style = entity_load('image_style', $variables['style_name'])) { + $image_style->transformDimensions($dimensions); + } return $dimensions; -} +} diff --git a/core/modules/picture/lib/Drupal/picture/Tests/PictureThemeFunctionsTest.php b/core/modules/picture/lib/Drupal/picture/Tests/PictureThemeFunctionsTest.php deleted file mode 100644 index 494e6f4..0000000 --- a/core/modules/picture/lib/Drupal/picture/Tests/PictureThemeFunctionsTest.php +++ /dev/null @@ -1,115 +0,0 @@ - 'Picture theme functions', - 'description' => 'Tests the picture theme functions.', - 'group' => 'Picture', - ); - } - - /** - * Tests usage of the theme_picture() function. - */ - function testPictureTheme() { - // Create an image. - $files = $this->drupalGetTestFiles('image'); - $file = reset($files); - $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME); - - // Create a style. - $style = entity_create('image_style', array('name' => 'test', 'label' => 'Test')); - $style->save(); - $url = $style->buildUrl($original_uri); - - // Test using theme_picture() with a NULL value for the alt option. - $path = $this->randomName(); - $element = array( - '#theme' => 'picture', - '#uri' => $original_uri, - '#alt' => NULL, - '#style_name' => 'test', - ); - $rendered_element = render($element); - $expected_result = ''; - $this->assertTrue(strpos($rendered_element, '') !== FALSE, 'picture element in theme_picture() correctly renders with a NULL value for the alt option.'); - - // Test using theme_picture() without passing a value for the alt option. - unset($element['#alt']); - $rendered_element = render($element); - $expected_result = ''; - $this->assertTrue(strpos($rendered_element, $expected_result) !== FALSE, 'img element in theme_picture() correctly renders the alt option.'); - $this->assertTrue(strpos($rendered_element, '') !== FALSE, 'picture element in theme_picture() correctly renders the alt option.'); - } - - /** - * Tests usage of the theme_picture_formatter() function. - */ - function testPictureFormatterTheme() { - // Create an image. - $files = $this->drupalGetTestFiles('image'); - $file = reset($files); - $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME); - - // Create a style. - $style = entity_create('image_style', array('name' => 'test', 'label' => 'Test')); - $style->save(); - $url = $style->buildUrl($original_uri); - - // Test using theme_picture_formatter() without breakpoints and with a NULL value for the alt option. - $path = $this->randomName(); - $element = array( - '#theme' => 'picture_formatter', - '#item' => array( - 'uri' => $original_uri, - 'alt' => NULL, - ), - '#image_style' => 'test', - '#path' => array( - 'path' => $path, - ), - ); - $rendered_element = render($element); - $expected_result = ''; - $this->assertEqual($expected_result, $rendered_element, 'theme_picture_formatter() correctly renders without breakpoints and with a NULL value for the alt option.'); - - // Test using theme_picture_formatter() without an image title, alt text, or - // link options. - unset($element['#item']['alt']); - $rendered_element = render($element); - $expected_result = ''; - $this->assertEqual($expected_result, $rendered_element, 'theme_picture_formatter() correctly renders without title, alt, or path options.'); - - // Link the image to a fragment on the page, and not a full URL. - $fragment = $this->randomName(); - $element['#path']['path'] = ''; - $element['#path']['options'] = array( - 'external' => TRUE, - 'fragment' => $fragment, - ); - $rendered_element = render($element); - $expected_result = ''; - $this->assertEqual($expected_result, $rendered_element, 'theme_picture_formatter() correctly renders a link fragment.'); - } -} diff --git a/core/modules/picture/templates/picture.html.twig b/core/modules/picture/templates/picture.html.twig deleted file mode 100644 index bbd8ef9..0000000 --- a/core/modules/picture/templates/picture.html.twig +++ /dev/null @@ -1,26 +0,0 @@ -{# -/** - * @file - * Default theme implementation to display a picture tag. - * - * Available variables: - * - attributes: HTML attributes for the picture tag. - * - sources: A set of source data. - * - source.attributes: HTML attributes for each source tag. - * - fallback: Fallback image tag for non-JS browsers. - * - * @see template_preprocess_picture() - * - * @ingroup themeable - */ -#} -{% if sources %} - - {% for source in sources %} - - {% endfor %} - - -{% else %} - {{- fallback -}} -{% endif %}