diff --git a/core/modules/picture/picture.module b/core/modules/picture/picture.module index 9dc9a6f..1389d53 100644 --- a/core/modules/picture/picture.module +++ b/core/modules/picture/picture.module @@ -155,7 +155,7 @@ function picture_theme() { 'picture' => array( 'variables' => array( 'style_name' => NULL, - 'path' => NULL, + 'uri' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => '', @@ -163,6 +163,7 @@ function picture_theme() { 'attributes' => array(), 'breakpoints' => array(), ), + 'template' => 'picture', ), 'picture_formatter' => array( 'variables' => array( @@ -171,20 +172,24 @@ function picture_theme() { 'image_style' => NULL, 'breakpoints' => array(), ), + 'template' => 'picture-formatter', ), 'picture_source' => array( 'variables' => array( 'src' => NULL, 'srcset' => NULL, - 'dimension' => NULL, + 'dimensions' => NULL, 'media' => NULL, ), + 'template' => 'picture-source', ), ); } /** - * Returns HTML for a picture field formatter. + * Prepares variables for picture formatter templates. + * + * Default template: picture-formatter.html.twig. * * @param array $variables * An associative array containing: @@ -192,40 +197,53 @@ function picture_theme() { * - 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_picture_formatter($variables) { +function template_preprocess_picture_formatter(&$variables) { if (!isset($variables['breakpoints']) || empty($variables['breakpoints'])) { - return theme('image_formatter', $variables); + $variables['image'] = array( + '#theme' => 'image_formatter', + '#item' => $variables['item'], + '#image_style' => $variables['image_style'], + '#path' => $variables['path'], + ); } + else { + $item = $variables['item']; - $item = $variables['item']; - - // Do not output an empty 'title' attribute. - if (isset($item['title']) && drupal_strlen($item['title']) == 0) { - unset($item['title']); - } + // Do not output an empty 'title' attribute. + if (isset($item['title']) && drupal_strlen($item['title']) == 0) { + unset($item['title']); + } - $item['style_name'] = $variables['image_style']; - $item['breakpoints'] = $variables['breakpoints']; + $variables['image'] = array( + '#theme' => 'picture', + '#style_name' => $variables['image_style'], + '#uri' => $item['uri'], + '#width' => $item['width'], + '#height' => $item['height'], + '#breakpoints' => $variables['breakpoints'], + '#alt' => isset($item['alt']) ? $item['alt'] : NULL, + '#title' => isset($item['title']) ? $item['title'] : NULL, + '#attributes' => isset($item['attributes']) ? $item['attributes'] : NULL, + ); - if (!isset($item['path']) && isset($variables['uri'])) { - $item['path'] = $variables['uri']; - } - $output = theme('picture', $item); - - if (isset($variables['path']['path'])) { - $path = $variables['path']['path']; - $options = isset($variables['path']['options']) ? $variables['path']['options'] : array(); - $options['html'] = TRUE; - $output = l($output, $path, $options); + if (isset($variables['path']['path'])) { + if (isset($variables['path']['options']['attributes'])) { + $variables['path']['attributes'] = new Attribute($variables['path']['options']['attributes']); + } + else { + $variables['path']['attributes'] = new Attribute(); + } + $options = isset($variables['path']['options']) ? $variables['path']['options'] : array(); + $variables['path']['path'] = url($variables['path']['path'], $options); + } } - return $output; } /** - * Returns HTML for a picture. + * Prepares variables for picture templates. + * + * Default template: picture.html.twig. * * @param $variables * An associative array containing: @@ -235,10 +253,8 @@ function theme_picture_formatter($variables) { * - height: The height of the image (if known). * - alt: The alternative text for text-based browsers. * - breakpoints: An array containing breakpoints. - * - * @ingroup themeable */ -function theme_picture($variables) { +function template_preprocess_picture(&$variables) { // Make sure that width and height are proper values // If they exists we'll output them // @see http://www.w3.org/community/respimg/2012/06/18/florians-compromise/ @@ -251,13 +267,14 @@ function theme_picture($variables) { unset($variables['height']); } - $sources = array(); + $variables['sources'] = array(); $output = array(); // Fallback image, output as source with media query. - $sources[] = array( - 'src' => image_style_url($variables['style_name'], $variables['uri']), - 'dimensions' => picture_get_image_dimensions($variables), + $variables['sources'][] = array( + '#theme' => 'picture_source', + '#src' => image_style_url($variables['style_name'], $variables['uri']), + '#dimensions' => picture_get_image_dimensions($variables), ); // All breakpoints and multipliers. @@ -274,50 +291,54 @@ function theme_picture($variables) { // Only one image, use src. if (count($new_sources) == 1) { - $sources[] = array( - 'src' => image_style_url($new_sources[0]['style_name'], $new_sources[0]['uri']), - 'dimensions' => picture_get_image_dimensions($new_sources[0]), - 'media' => $breakpoint->mediaQuery, + $variables['sources'][] = array( + '#theme' => 'picture_source', + '#src' => image_style_url($new_sources[0]['style_name'], $new_sources[0]['uri']), + '#dimensions' => picture_get_image_dimensions($new_sources[0]), + '#media' => $breakpoint->mediaQuery, ); } else { - // Mutliple images, use srcset. + // Multiple images, use srcset. $srcset = array(); foreach ($new_sources as $new_source) { $srcset[] = image_style_url($new_source['style_name'], $new_source['uri']) . ' ' . $new_source['#multiplier']; } - $sources[] = array( - 'srcset' => implode(', ', $srcset), - 'dimensions' => picture_get_image_dimensions($new_sources[0]), - 'media' => $breakpoint->mediaQuery, + $variables['sources'][] = array( + '#theme' => 'picture_source', + '#srcset' => implode(', ', $srcset), + '#dimensions' => picture_get_image_dimensions($new_sources[0]), + '#media' => $breakpoint->mediaQuery, ); } } } - if (!empty($sources)) { - $attributes = array(); - foreach (array('alt', 'title') as $key) { - if (isset($variables[$key])) { - $attributes[$key] = $variables[$key]; - } - } - $output[] = ''; - - // Add source tags to the output. - foreach ($sources as $source) { - $output[] = theme('picture_source', $source); + // Prepare picture tag attributes. + $attributes = array(); + foreach (array('alt', 'title') as $key) { + if (isset($variables[$key])) { + $attributes[$key] = $variables[$key]; } - - // Output the fallback image. - $output[] = ''; - $output[] = ''; - return implode("\n", $output); } + $variables['attributes'] = new Attribute($attributes); + + // Prepare noscript fallback image. + $variables['fallback'] = array( + '#theme' => 'image_style', + '#uri' => $variables['uri'], + '#width' => $variables['width'], + '#height' => $variables['height'], + '#style_name' => $variables['style_name'], + '#alt' => isset($variables['alt']) ? $variables['alt'] : NULL, + '#title' => isset($variables['title']) ? $variables['title'] : NULL, + ); } /** - * Returns HTML for a source tag. + * Prepares variables for source tag templates. + * + * Default template: picture-source.html.twig. * * @param type $variables * An associative array containing: @@ -327,26 +348,9 @@ function theme_picture($variables) { * - src: Either the path of the image file (relative to base_path()) or a * full URL. * - dimensions: The width and height of the image (if known). - * - * @ingroup themeable */ -function theme_picture_source($variables) { - $output = array(); - if (isset($variables['media']) && !empty($variables['media'])) { - if (!isset($variables['srcset'])) { - $output[] = ''; - $output[] = ''; - } - elseif (!isset($variables['src'])) { - $output[] = ''; - $output[] = ''; - } - } - else { - $output[] = ''; - $output[] = ''; - } - return implode("\n", $output); +function template_preprocess_picture_source(&$variables) { + $variables['attributes'] = new Attribute($variables['dimensions']); } /** diff --git a/core/modules/picture/templates/picture-formatter.html.twig b/core/modules/picture/templates/picture-formatter.html.twig new file mode 100644 index 0000000..bf7810d --- /dev/null +++ b/core/modules/picture/templates/picture-formatter.html.twig @@ -0,0 +1,23 @@ +{# +/** + * @file + * Default theme implementation to display a picture field formatter. + * + * Available variables: + * - image: Picture element, if picture has breakpoint sources, otherwise img + * element. + * - path: (optional) Contains information about the link. + * - path.path: The URL the image links to. + * - path.attributes: HTML attributes for the link tag. + * + * @see template_preprocess() + * @see template_preprocess_picture_formatter() + * + * @ingroup themeable + */ +#} +{% if path %} + {{ image }} +{% else %} + {{ image }} +{% endif %} diff --git a/core/modules/picture/templates/picture-source.html.twig b/core/modules/picture/templates/picture-source.html.twig new file mode 100644 index 0000000..5acbe66 --- /dev/null +++ b/core/modules/picture/templates/picture-source.html.twig @@ -0,0 +1,40 @@ +{# +/** + * @file + * Default theme implementation to display a picture source tag. + * + * Available variables: + * - media: The media query to use. + * - dimensions: The width and height of the image (if known). + * - attributes: Additional HTML attributes for source tag. + * + * One of the following two variables will be available: + * - srcset: A group of image sources/resolutions used to display different + * image resolutions at different breakpoints. + * - src: Either the path of the image file (relative to base_path()) or a + * full URL. + * + * @see template_preprocess() + * @see template_preprocess_picture_source() + * + * @ingroup themeable + */ +#} +{# + The Attribute object keeps track of which attributes have been printed, and + will only an attribute once. We create a new variable here to allow printing + the entire set of attributes as many times as we'd like. +#} +{% set src_attributes %}{{ attributes }}{% endset %} +{% if media %} + {% if src %} + + + {% elseif srcset %} + + + {% endif %} +{% else %} + + +{% endif %} diff --git a/core/modules/picture/templates/picture.html.twig b/core/modules/picture/templates/picture.html.twig new file mode 100644 index 0000000..7a3fcb9 --- /dev/null +++ b/core/modules/picture/templates/picture.html.twig @@ -0,0 +1,24 @@ +{# +/** + * @file + * Default theme implementation to display a picture tag. + * + * Available variables: + * - sources: Source elements. + * - attributes: HTML attributes for picture element. + * - fallback: Fallback image for non-JS browsers. + * + * @see template_preprocess() + * @see template_preprocess_picture() + * + * @ingroup themeable + */ +#} +{% if sources %} + + {% for source in sources %} + {{ source }} + {% endfor %} + + +{% endif %}