Skip to content
Snippets Groups Projects

Fixed phpcs issues.

3 unresolved threads
Files
7
<?php
<?php
use Drupal\responsive_image\Entity\ResponsiveImageStyle;
/**
 
* @file
 
* Hook implementations for the ImageAPI Optimize WebP Responsive module.
 
*/
 
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\image\Entity\ImageStyle;
 
use Drupal\responsive_image\Entity\ResponsiveImageStyle;
/**
/**
* Implements hook_help().
* Implements hook_help().
@@ -15,6 +20,7 @@ function imageapi_optimize_webp_responsive_help($route_name, RouteMatchInterface
@@ -15,6 +20,7 @@ function imageapi_optimize_webp_responsive_help($route_name, RouteMatchInterface
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Implements WebP derivatives in core Responsive Images') . '</p>';
$output .= '<p>' . t('Implements WebP derivatives in core Responsive Images') . '</p>';
return $output;
return $output;
 
default:
default:
}
}
}
}
@@ -25,8 +31,9 @@ function imageapi_optimize_webp_responsive_help($route_name, RouteMatchInterface
@@ -25,8 +31,9 @@ function imageapi_optimize_webp_responsive_help($route_name, RouteMatchInterface
function imageapi_optimize_webp_responsive_preprocess_responsive_image(&$variables) {
function imageapi_optimize_webp_responsive_preprocess_responsive_image(&$variables) {
if (!empty($variables['sources'])) {
if (!empty($variables['sources'])) {
$webp_sources = [];
$webp_sources = [];
//@todo: Use cache to store ImageStyles that utilize a pipeine with WebP processor.
// @todo Use cache to store ImageStyles that utilize a pipeine with WebP processor.
// Load the ResponsiveImageStyle entity to access the ImageStyles for sources.
// Load the ResponsiveImageStyle entity to access the ImageStyles
 
// for sources.
$entity = ResponsiveImageStyle::load($variables['responsive_image_style_id']);
$entity = ResponsiveImageStyle::load($variables['responsive_image_style_id']);
$image_styles = ImageStyle::loadMultiple($entity->getImageStyleIds());
$image_styles = ImageStyle::loadMultiple($entity->getImageStyleIds());
$pipeline_webp_map = [];
$pipeline_webp_map = [];
@@ -46,7 +53,8 @@ function imageapi_optimize_webp_responsive_preprocess_responsive_image(&$variabl
@@ -46,7 +53,8 @@ function imageapi_optimize_webp_responsive_preprocess_responsive_image(&$variabl
}
}
}
}
}
}
// If WebP processor exits on pipeline, build another map for jpg => webp paths.
// If WebP processor exits on pipeline, build another map for
 
// jpg => webp paths.
if ($pipeline_webp_map[$image_style->getPipeline()]) {
if ($pipeline_webp_map[$image_style->getPipeline()]) {
$image_uri = $image_style->buildUri($variables['uri']);
$image_uri = $image_style->buildUri($variables['uri']);
$image_url = \Drupal::service('file_url_generator')->generateAbsoluteString($image_uri);
$image_url = \Drupal::service('file_url_generator')->generateAbsoluteString($image_uri);
@@ -59,11 +67,13 @@ function imageapi_optimize_webp_responsive_preprocess_responsive_image(&$variabl
@@ -59,11 +67,13 @@ function imageapi_optimize_webp_responsive_preprocess_responsive_image(&$variabl
}
}
}
}
}
}
// If WebP processors are found on any of the ImageStyles, add webp derivatives as sources.
// If WebP processors are found on any of the ImageStyles,
 
// add webp derivatives as sources.
if ($image_style_map) {
if ($image_style_map) {
foreach ($variables['sources'] as $source) {
foreach ($variables['sources'] as $source) {
$offset_key = NULL;
$offset_key = NULL;
// Check for instances where srcset and data-srcset used interchangeably.
// Check for instances where srcset and data-srcset are
 
// used interchangeably.
if ($source->offsetExists('srcset')) {
if ($source->offsetExists('srcset')) {
$offset_key = 'srcset';
$offset_key = 'srcset';
}
}
@@ -74,7 +84,7 @@ function imageapi_optimize_webp_responsive_preprocess_responsive_image(&$variabl
@@ -74,7 +84,7 @@ function imageapi_optimize_webp_responsive_preprocess_responsive_image(&$variabl
if ($offset_key) {
if ($offset_key) {
$source_srcset = $source->offsetGet($offset_key)->value();
$source_srcset = $source->offsetGet($offset_key)->value();
$cloned_srcset = strtr($source_srcset, $image_style_map);
$cloned_srcset = strtr($source_srcset, $image_style_map);
if($source_srcset != $cloned_srcset) {
if ($source_srcset != $cloned_srcset) {
$source_webp = clone $source;
$source_webp = clone $source;
$source_webp->offsetSet($offset_key, $cloned_srcset);
$source_webp->offsetSet($offset_key, $cloned_srcset);
$source_webp->offsetSet('type', 'image/webp');
$source_webp->offsetSet('type', 'image/webp');
Loading