Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / crop / src / Entity / Crop.php
index 1cee3eeb977be90401930e2903567e1721f1e0da..563c213515f54be2c3b562196f225014b44317fe 100644 (file)
@@ -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.
@@ -41,6 +42,11 @@ use Drupal\crop\EntityProviderNotFoundException;
  *     "langcode" = "langcode",
  *     "uuid" = "uuid"
  *   },
+ *   revision_metadata_keys = {
+ *     "revision_timestamp" = "revision_timestamp",
+ *     "revision_uid" = "revision_uid",
+ *     "revision_log" = "revision_log"
+ *   },
  *   bundle_entity_type = "crop_type",
  *   permission_granularity = "entity_type",
  *   admin_permission = "administer crop",
@@ -129,16 +135,41 @@ class Crop extends ContentEntityBase implements CropInterface {
    * {@inheritdoc}
    */
   public static function findCrop($uri, $type) {
-    $query = \Drupal::entityQuery('crop')
-      ->condition('uri', $uri);
-    if ($type) {
-      $query->condition('type', $type);
+    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);
+      }
     }
-    $crop = $query->sort('cid')
-      ->range(0, 1)
-      ->execute();
 
-    return $crop ? \Drupal::entityTypeManager()->getStorage('crop')->load(current($crop)) : NULL;
+    return $crop;
   }
 
   /**
@@ -297,14 +328,12 @@ class Crop extends ContentEntityBase implements CropInterface {
     $fields['revision_timestamp'] = BaseFieldDefinition::create('created')
       ->setLabel(t('Revision timestamp'))
       ->setDescription(t('The time that the current revision was created.'))
-      ->setQueryable(FALSE)
       ->setRevisionable(TRUE);
 
     $fields['revision_uid'] = BaseFieldDefinition::create('entity_reference')
       ->setLabel(t('Revision author ID'))
       ->setDescription(t('The user ID of the author of the current revision.'))
       ->setSetting('target_type', 'user')
-      ->setQueryable(FALSE)
       ->setRevisionable(TRUE);
 
     $fields['revision_log'] = BaseFieldDefinition::create('string_long')