' . t('The Responsive Image module provides an image formatter and breakpoint mappings to output responsive images using the HTML5 picture tag. For more information, see the online documentation for the Responsive Image module.', array( '!responsive_image' => 'https://www.drupal.org/documentation/modules/responsive_image')) . '
';
$output .= '
' . t('Uses') . '
';
$output .= '
';
$output .= '
' . t('Defining responsive image styles') . '
';
$output .= '
' . t('By creating responsive image styles you define the image styles that are being used to output images at certain breakpoints. On the Responsive image styles page, click Add responsive image style to create a new style. First chose a label and a breakpoint group and click Save. After that you can choose the image styles that will be used for each breakpoint. Image styles can be defined on the Image styles page that is provided by the Image module. Breakpoints are defined in the configuration files of the theme. See the help page of the Breakpoint module for more information.', array('!responsive_image_style' => \Drupal::url('entity.responsive_image_style.collection'), '!image_styles' => \Drupal::url('entity.image_style.collection'),'!image_help' => \Drupal::url('help.page', array('name' => 'image')), '!breakpoint_help' => \Drupal::url('help.page', array('name' => 'breakpoint')))) . '
' . t('After defining responsive image styles, you can use them in the display settings for your Image fields, so that the site displays responsive images using the HTML5 picture tag. Open the Manage display page for the entity type (content type, taxonomy vocabulary, etc.) that the Image field is attached to. Choose the format Responsive image, click the Edit icon, and select one of the responsive image styles that you have created. For general information on how to manage fields and their display see the Field UI module help page. For background information about entities and fields see the Field module help page.', array('!field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', array('name' => 'field_ui')) : '#','!field_help' => \Drupal::url('help.page', array('name' => 'field')))) . '
';
$output .= '
';
return $output;
case 'entity.responsive_image_style.collection':
return '
' . t('A responsive image style associates an image style with each breakpoint defined by your theme.') . '
';
}
}
/**
* Implements hook_menu().
*/
function responsive_image_menu() {
$items = array();
$items['admin/config/media/responsive-image-style'] = array(
'title' => 'Responsive image styles',
'description' => 'Manage responsive image styles',
'weight' => 10,
'route_name' => 'entity.responsive_image_style.collection',
);
$items['admin/config/media/responsive-image-style/%responsive_image_style'] = array(
'title' => 'Edit responsive image style',
'route_name' => 'entity.responsive_image_style.edit_form',
);
$items['admin/config/media/responsive-image-style/%responsive_image_style/duplicate'] = array(
'title' => 'Duplicate responsive image style',
'route_name' => 'entity.responsive_image_style.duplicate_form',
);
return $items;
}
/**
* Implements hook_theme().
*/
function responsive_image_theme() {
return array(
'responsive_image' => array(
'variables' => array(
'uri' => NULL,
'attributes' => array(),
'responsive_image_style_id' => array(),
),
),
'responsive_image_formatter' => array(
'variables' => array(
'item' => NULL,
'item_attributes' => NULL,
'url' => NULL,
'responsive_image_style_id' => NULL,
),
),
);
}
/**
* Prepares variables for responsive image formatter templates.
*
* Default template: responsive-image-formatter.html.twig.
*
* @param array $variables
* An associative array containing:
* - item: An ImageItem object.
* - item_attributes: An optional associative array of HTML attributes to be
* placed in the img tag.
* - responsive_image_style_id: A responsive image style.
* - url: An optional \Drupal\Core\Url object.
*/
function template_preprocess_responsive_image_formatter(&$variables) {
$variables['responsive_image'] = array(
'#type' => 'responsive_image',
'#responsive_image_style_id' => $variables['responsive_image_style_id'],
);
$item = $variables['item'];
$attributes = array();
// Do not output an empty 'title' attribute.
if (Unicode::strlen($item->title) != 0) {
$attributes['title'] = $item->title;
}
$attributes['alt'] = $item->alt;
$attributes += $variables['item_attributes'];
if (($entity = $item->entity) && empty($item->uri)) {
$variables['responsive_image']['#uri'] = $entity->getFileUri();
}
else {
$variables['responsive_image']['#uri'] = $item->uri;
}
foreach (array('width', 'height') as $key) {
$variables['responsive_image']["#$key"] = $item->$key;
}
$variables['responsive_image']['#attributes'] = $attributes;
}
/**
* Prepares variables for a responsive image.
*
* Default template: responsive-image.html.twig.
*
* @param $variables
* An associative array containing:
* - uri: The URI of the image.
* - width: The width of the image (if known).
* - height: The height of the image (if known).
* - attributes: Associative array of attributes to be placed in the img tag.
* - responsive_image_style_id: The ID of the responsive image style.
*/
function template_preprocess_responsive_image(&$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/
if (isset($variables['width']) && empty($variables['width'])) {
unset($variables['width']);
unset($variables['height']);
}
elseif (isset($variables['height']) && empty($variables['height'])) {
unset($variables['width']);
unset($variables['height']);
}
$image = \Drupal::service('image.factory')->get($variables['uri']);
$responsive_image_style = ResponsiveImageStyle::load($variables['responsive_image_style_id']);
// All breakpoints and multipliers.
$breakpoints = \Drupal::service('breakpoint.manager')->getBreakpointsByGroup($responsive_image_style->getBreakpointGroup());
foreach ($responsive_image_style->getKeyedImageStyleMappings() as $breakpoint_id => $multipliers) {
if (isset($breakpoints[$breakpoint_id])) {
$variables['sources'][] = responsive_image_build_source_attributes($image, $variables, $breakpoints[$breakpoint_id], $multipliers);
}
}
// Prepare the fallback image. Use srcset in the fallback image to avoid
// unnecessary preloading of images in older browsers. See
// http://scottjehl.github.io/picturefill/#using-picture and
// http://scottjehl.github.io/picturefill/#gotchas for more information.
$variables['img_element'] = array(
'#theme' => 'image',
'#srcset' => array(
array(
'uri' => _responsive_image_image_style_url($responsive_image_style->getFallbackImageStyle(), $image->getSource()),
),
),
);
if (isset($variables['attributes'])) {
if (isset($variables['attributes']['alt'])) {
$variables['img_element']['#alt'] = $variables['attributes']['alt'];
unset($variables['attributes']['alt']);
}
if (isset($variables['attributes']['title'])) {
$variables['img_element']['#title'] = $variables['attributes']['title'];
unset($variables['attributes']['title']);
}
$variables['img_element']['#attributes'] = $variables['attributes'];
}
}
/**
* Helper function for template_preprocess_responsive_image().
*
* Builds an array of attributes for