Commit 64470907 authored by woprrr's avatar woprrr Committed by woprrr

Issue #2899173 by blake.thompson, woprrr: crop_file_url_alter doesn't work...

Issue #2899173 by blake.thompson, woprrr: crop_file_url_alter doesn't work with module provided crop types
parent 06624302
......@@ -136,19 +136,7 @@ function crop_file_url_alter(&$uri) {
return;
}
$crop_type = NULL;
// Find whether matched image style uses "crop_type" effect.
/* @var \Drupal\image\ImageEffectInterface $effect */
foreach ($image_style->getEffects() as $uuid => $effect) {
$data_effect = $image_style->getEffect($uuid)->getConfiguration()['data'];
if (isset($data_effect['crop_type'])) {
$crop_type = $data_effect['crop_type'];
break;
}
}
// In case the image style uses "crop_type" effect, load the crop entity.
if ($crop_type && $crop = Crop::findCrop($file_uri, $crop_type)) {
if ($crop = Crop::getCropFromImageStyle($file_uri, $image_style)) {
// Found a crop for this image, append a hash of it to the URL,
// so that browsers reload the image and CDNs and proxies can be bypassed.
$shortened_hash = substr(md5(implode($crop->position()) . implode($crop->anchor())), 0, 8);
......
......@@ -3,6 +3,7 @@
namespace Drupal\crop;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\image\ImageStyleInterface;
/**
* Provides an interface defining the crop entity.
......@@ -97,4 +98,17 @@ interface CropInterface extends ContentEntityInterface {
*/
public static function findCrop($uri, $type);
/**
* Retrieve crop from given image style.
*
* @param string $uri
* URI of the image.
* @param \Drupal\image\ImageStyleInterface $image_style
* The image style.
*
* @return \Drupal\crop\CropInterface|null
* Crop entity used by effect 'crop_crop' or NULL if crop doesn't exist.
*/
public static function getCropFromImageStyle($uri, ImageStyleInterface $image_style);
}
......@@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\crop\CropInterface;
use Drupal\crop\EntityProviderNotFoundException;
use Drupal\image\ImageStyleInterface;
/**
* Defines the crop entity class.
......@@ -132,6 +133,40 @@ class Crop extends ContentEntityBase implements CropInterface {
return \Drupal::entityTypeManager()->getStorage('crop')->getCrop($uri, $type);
}
/**
* {@inheritdoc}
*/
public static function getCropFromImageStyle($uri, ImageStyleInterface $image_style) {
$effects = [];
$crop = FALSE;
foreach ($image_style->getEffects() as $uuid => $effect) {
// Store the effects parameters for later use.
$effects[$effect->getPluginId()] = [
'uuid' => $uuid,
'provider' => $effect->getPluginDefinition()['provider'],
];
}
if (isset($effects['crop_crop']) && $image_style->getEffects()
->has($effects['crop_crop']['uuid'])) {
$type = $image_style->getEffect($effects['crop_crop']['uuid'])
->getConfiguration()['data']['crop_type'];
$crop = self::findCrop($uri, $type);
}
// Fallback to use the provider as a fallback to check if provider name,
// match with crop types for modules non-based on "manual crop" effects.
if (!$crop) {
foreach ($effects as $effect) {
$provider = $effect['provider'];
$crop = self::findCrop($uri, $provider);
}
}
return $crop;
}
/**
* {@inheritdoc}
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment