You are here

public static function Crop::getCropFromImageStyle in Crop API 8

Same name and namespace in other branches
  1. 8.2 src/Entity/Crop.php \Drupal\crop\Entity\Crop::getCropFromImageStyle()

Retrieve crop from given image style.

Parameters

string $uri: URI of the image.

\Drupal\image\ImageStyleInterface $image_style: The image style.

Return value

\Drupal\crop\CropInterface|null Crop entity used by effect 'crop_crop' or NULL if crop doesn't exist.

Overrides CropInterface::getCropFromImageStyle

1 call to Crop::getCropFromImageStyle()
crop_file_url_alter in ./crop.module
Implements hook_file_url_alter().

File

src/Entity/Crop.php, line 144

Class

Crop
Defines the crop entity class.

Namespace

Drupal\crop\Entity

Code

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;
}