Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / image_widget_crop / src / ImageWidgetCropManager.php
index 5f37ebeed469a4689c1c217e3a8a9a1898f1669e..b72489522fffa8cafc85dbb76a89097c3c5af282 100644 (file)
@@ -8,52 +8,51 @@ use Drupal\Core\Entity\FieldableEntityInterface;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\crop\Entity\Crop;
 use Drupal\crop\Entity\CropType;
-use Drupal\file\Entity\File;
 use Drupal\file\Plugin\Field\FieldType\FileFieldItemList;
 use Drupal\image\Entity\ImageStyle;
 
 /**
  * ImageWidgetCropManager calculation class.
  */
-class ImageWidgetCropManager {
+class ImageWidgetCropManager implements ImageWidgetCropInterface {
 
   /**
    * The entity type manager service.
    *
-   * @var \Drupal\Core\Entity\EntityTypeManagerInterface;
+   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
    */
   protected $entityTypeManager;
 
   /**
    * The crop storage.
    *
-   * @var \Drupal\crop\CropStorage.
+   * @var \Drupal\Core\Entity\EntityStorageInterface
    */
   protected $cropStorage;
 
   /**
    * The crop storage.
    *
-   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface.
+   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
    */
   protected $cropTypeStorage;
 
   /**
    * The image style storage.
    *
-   * @var \Drupal\image\ImageStyleStorageInterface
+   * @var \Drupal\Core\Entity\EntityStorageInterface
    */
   protected $imageStyleStorage;
 
   /**
    * The File storage.
    *
-   * @var \Drupal\Core\Config\Entity\ConfigEntityStorage.
+   * @var \Drupal\Core\Entity\EntityStorageInterface
    */
   protected $fileStorage;
 
   /**
-   * Constructs a ImageWidgetCropManager.
+   * Constructs a ImageWidgetCropManager object.
    *
    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
    *   Entity type manager service.
@@ -67,15 +66,7 @@ class ImageWidgetCropManager {
   }
 
   /**
-   * Create new crop entity with user properties.
-   *
-   * @param array $properties
-   *   All properties returned by the crop plugin (js),
-   *   and the size of thumbnail image.
-   * @param array|mixed $field_value
-   *   An array of values for the contained properties of image_crop widget.
-   * @param CropType $crop_type
-   *   The entity CropType.
+   * {@inheritdoc}
    */
   public function applyCrop(array $properties, $field_value, CropType $crop_type) {
     // Get Original sizes and position of crop zone.
@@ -83,19 +74,17 @@ class ImageWidgetCropManager {
     // Get all imagesStyle used this crop_type.
     $image_styles = $this->getImageStylesByCrop($crop_type->id());
 
-    $this->saveCrop($crop_properties, $field_value, $image_styles, $crop_type, FALSE);
+    $this->saveCrop(
+      $crop_properties,
+      $field_value,
+      $image_styles,
+      $crop_type,
+      FALSE
+    );
   }
 
   /**
-   * Update old crop with new properties choose in UI.
-   *
-   * @param array $properties
-   *   All properties returned by the crop plugin (js),
-   *   and the size of thumbnail image.
-   * @param array|mixed $field_value
-   *   An array of values contain properties of image_crop widget.
-   * @param CropType $crop_type
-   *   The entity CropType.
+   * {@inheritdoc}
    */
   public function updateCrop(array $properties, $field_value, CropType $crop_type) {
     // Get Original sizes and position of crop zone.
@@ -114,11 +103,8 @@ class ImageWidgetCropManager {
       return;
     }
 
-    foreach ($crops as $crop_element) {
-      // Get Only first crop entity @see https://www.drupal.org/node/2617818.
-      /** @var \Drupal\crop\Entity\Crop $crop */
-      $crop = $crop_element;
-
+    /** @var \Drupal\crop\Entity\Crop $crop */
+    foreach ($crops as $crop) {
       if (!$this->cropHasChanged($crop_properties, array_merge($crop->position(), $crop->size()))) {
         return;
       }
@@ -129,18 +115,7 @@ class ImageWidgetCropManager {
   }
 
   /**
-   * Save the crop when this crop not exist.
-   *
-   * @param double[] $crop_properties
-   *   The properties of the crop applied to the original image (dimensions).
-   * @param array|mixed $field_value
-   *   An array of values for the contained properties of image_crop widget.
-   * @param array $image_styles
-   *   The list of imagesStyle available for this crop.
-   * @param CropType $crop_type
-   *   The entity CropType.
-   * @param bool $notify
-   *   Show notification after actions (default TRUE).
+   * {@inheritdoc}
    */
   public function saveCrop(array $crop_properties, $field_value, array $image_styles, CropType $crop_type, $notify = TRUE) {
     $values = [
@@ -165,14 +140,7 @@ class ImageWidgetCropManager {
   }
 
   /**
-   * Delete the crop when user delete it.
-   *
-   * @param string $file_uri
-   *   Uri of image uploaded by user.
-   * @param \Drupal\crop\Entity\CropType $crop_type
-   *   The CropType object.
-   * @param int $file_id
-   *   Id of image uploaded by user.
+   * {@inheritdoc}
    */
   public function deleteCrop($file_uri, CropType $crop_type, $file_id) {
     $image_styles = $this->getImageStylesByCrop($crop_type->id());
@@ -189,15 +157,7 @@ class ImageWidgetCropManager {
   }
 
   /**
-   * Get center of crop selection.
-   *
-   * @param int[] $axis
-   *   Coordinates of x-axis & y-axis.
-   * @param array $crop_selection
-   *   Coordinates of crop selection (width & height).
-   *
-   * @return array<string,double>
-   *   Coordinates (x-axis & y-axis) of crop selection zone.
+   * {@inheritdoc}
    */
   public function getAxisCoordinates(array $axis, array $crop_selection) {
     return [
@@ -207,15 +167,7 @@ class ImageWidgetCropManager {
   }
 
   /**
-   * Get the size and position of the crop.
-   *
-   * @param array $field_values
-   *   The original values of image.
-   * @param array $properties
-   *   The original height of image.
-   *
-   * @return null|array
-   *   The data dimensions (width & height) into this ImageStyle.
+   * {@inheritdoc}
    */
   public function getCropOriginalDimension(array $field_values, array $properties) {
     $crop_coordinates = [];
@@ -245,15 +197,7 @@ class ImageWidgetCropManager {
   }
 
   /**
-   * Get one effect instead of ImageStyle.
-   *
-   * @param \Drupal\image\Entity\ImageStyle $image_style
-   *   The ImageStyle to get data.
-   * @param string $data_type
-   *   The type of data needed in current ImageStyle.
-   *
-   * @return mixed|null
-   *   The effect data in current ImageStyle.
+   * {@inheritdoc}
    */
   public function getEffectData(ImageStyle $image_style, $data_type) {
     $data = NULL;
@@ -269,13 +213,7 @@ class ImageWidgetCropManager {
   }
 
   /**
-   * Get the imageStyle using this crop_type.
-   *
-   * @param string $crop_type_name
-   *   The id of the current crop_type entity.
-   *
-   * @return array
-   *   All imageStyle used by this crop_type.
+   * {@inheritdoc}
    */
   public function getImageStylesByCrop($crop_type_name) {
     $styles = [];
@@ -293,14 +231,7 @@ class ImageWidgetCropManager {
   }
 
   /**
-   * Apply different operation on ImageStyles.
-   *
-   * @param array $image_styles
-   *   All ImageStyles used by this cropType.
-   * @param string $file_uri
-   *   Uri of image uploaded by user.
-   * @param bool $create_derivative
-   *   Boolean to create an derivative of the image uploaded.
+   * {@inheritdoc}
    */
   public function imageStylesOperations(array $image_styles, $file_uri, $create_derivative = FALSE) {
     /** @var \Drupal\image\Entity\ImageStyle $image_style */
@@ -318,12 +249,7 @@ class ImageWidgetCropManager {
   }
 
   /**
-   * Update existent crop entity properties.
-   *
-   * @param \Drupal\crop\Entity\Crop $crop
-   *   The crop object loaded.
-   * @param array $crop_properties
-   *   The machine name of ImageStyle.
+   * {@inheritdoc}
    */
   public function updateCropProperties(Crop $crop, array $crop_properties) {
     // Parse all properties if this crop have changed.
@@ -336,17 +262,7 @@ class ImageWidgetCropManager {
   }
 
   /**
-   * Load all crop using the ImageStyles.
-   *
-   * @param array $image_styles
-   *   All ImageStyle for this current CROP.
-   * @param CropType $crop_type
-   *   The entity CropType.
-   * @param string $file_uri
-   *   Uri of uploded file.
-   *
-   * @return array
-   *   All crop used this ImageStyle.
+   * {@inheritdoc}
    */
   public function loadImageStyleByCrop(array $image_styles, CropType $crop_type, $file_uri) {
     $crops = [];
@@ -363,16 +279,7 @@ class ImageWidgetCropManager {
   }
 
   /**
-   * Compare crop zone properties when user saved one crop.
-   *
-   * @param array $crop_properties
-   *   The crop properties after saved the form.
-   * @param array $old_crop
-   *   The crop properties save in this crop entity,
-   *   Only if this crop already exist.
-   *
-   * @return bool
-   *   Return true if properties is not identical.
+   * {@inheritdoc}
    */
   public function cropHasChanged(array $crop_properties, array $old_crop) {
     if (!empty(array_diff_assoc($crop_properties, $old_crop))) {
@@ -382,13 +289,7 @@ class ImageWidgetCropManager {
   }
 
   /**
-   * Verify if ImageStyle is correctly configured.
-   *
-   * @param array $styles
-   *   The list of available ImageStyle.
-   *
-   * @return array<integer>
-   *   The list of styles filtred.
+   * {@inheritdoc}
    */
   public function getAvailableCropImageStyle(array $styles) {
     $available_styles = [];
@@ -406,13 +307,7 @@ class ImageWidgetCropManager {
   }
 
   /**
-   * Verify if the crop is used by a ImageStyle.
-   *
-   * @param array $crop_list
-   *   The list of existent Crop Type.
-   *
-   * @return array<integer>
-   *   The list of Crop Type filtred.
+   * {@inheritdoc}
    */
   public function getAvailableCropType(array $crop_list) {
     $available_crop = [];
@@ -427,13 +322,7 @@ class ImageWidgetCropManager {
   }
 
   /**
-   * Get All sizes properties of the crops for an file.
-   *
-   * @param \Drupal\crop\Entity\Crop $crop
-   *   All crops attached to this file based on URI.
-   *
-   * @return array<array>
-   *   Get all crop zone properties (x, y, height, width),
+   * {@inheritdoc}
    */
   public static function getCropProperties(Crop $crop) {
     $anchor = $crop->anchor();
@@ -447,10 +336,7 @@ class ImageWidgetCropManager {
   }
 
   /**
-   * Fetch all fields FileField and use "image_crop" element on an entity.
-   *
-   * @param \Drupal\Core\Entity\EntityInterface $entity
-   *   The entity object.
+   * {@inheritdoc}
    */
   public function buildCropToEntity(EntityInterface $entity) {
     if (isset($entity) && $entity instanceof FieldableEntityInterface) {
@@ -463,7 +349,8 @@ class ImageWidgetCropManager {
           foreach ($entity_fields->getValue() as $crop_elements) {
             foreach ($crop_elements as $crop_element) {
               if (is_array($crop_element) && isset($crop_element['crop_wrapper'])) {
-                // Reload image since its URI could have been changed by other modules.
+                // Reload image since its URI could have been changed,
+                // by other modules.
                 /** @var \Drupal\file_entity\Entity\FileEntity $file */
                 $file = $this->fileStorage->load($crop_element['file-id']);
                 $crop_element['file-uri'] = $file->getFileUri();
@@ -509,54 +396,57 @@ class ImageWidgetCropManager {
   }
 
   /**
-   * Fetch all form elements using image_crop element.
-   *
-   * @param \Drupal\Core\Form\FormStateInterface $form_state
-   *   The current state of the form.
+   * {@inheritdoc}
    */
   public function buildCropToForm(FormStateInterface $form_state) {
     /** @var \Drupal\file_entity\Entity\FileEntity $entity */
-    $entity = $form_state->getFormObject()->getEntity();
-    $form_state_values = $form_state->getValues();
-    if (is_array($form_state_values['image_crop']) && isset($form_state_values['image_crop']['crop_wrapper'])) {
-      // Parse all values and get properties associate with the crop type.
-      foreach ($form_state_values['image_crop']['crop_wrapper'] as $crop_type_name => $properties) {
-        $properties = $properties['crop_container']['values'];
-        /** @var \Drupal\crop\Entity\CropType $crop_type */
-        $crop_type = $this->cropTypeStorage->load($crop_type_name);
-
-        // If the crop type needed is disabled or delete.
-        if (empty($crop_type) && $crop_type instanceof CropType) {
-          drupal_set_message(t("The CropType ('@cropType') is not active or not defined. Please verify configuration of image style or ImageWidgetCrop formatter configuration", ['@cropType' => $crop_type->id()]), 'error');
-          return;
-        }
-
-        if (is_array($properties) && isset($properties)) {
-          $crop_exists = Crop::cropExists($entity->getFileUri(), $crop_type_name);
-          if (!$crop_exists) {
-            if ($properties['crop_applied'] == '1' && isset($properties) && (!empty($properties['width']) && !empty($properties['height']))) {
-              $this->applyCrop($properties, $form_state_values['image_crop'], $crop_type);
-            }
+    if ($entity = $form_state->getFormObject()->getEntity()) {
+      $form_state_values = $form_state->getValues();
+      // @TODO Do not hardcode key of element to "image_crop" check #type of element instead like \Drupal\Core\Form\FormBuilder::doBuildForm.
+      if (is_array($form_state_values['image_crop']) && isset($form_state_values['image_crop']['crop_wrapper'])) {
+        // Parse all values and get properties associate with the crop type.
+        foreach ($form_state_values['image_crop']['crop_wrapper'] as $crop_type_name => $properties) {
+          $properties = $properties['crop_container']['values'];
+          /** @var \Drupal\crop\Entity\CropType $crop_type */
+          $crop_type = $this->cropTypeStorage->load($crop_type_name);
+
+          // If the crop type needed is disabled or delete.
+          if (empty($crop_type) && $crop_type instanceof CropType) {
+            drupal_set_message(t("The CropType ('@cropType') is not active or not defined. Please verify configuration of image style or ImageWidgetCrop formatter configuration", ['@cropType' => $crop_type->id()]), 'error');
+            return;
           }
-          else {
-            // Get all imagesStyle used this crop_type.
-            $image_styles = $this->getImageStylesByCrop($crop_type_name);
-            $crops = $this->loadImageStyleByCrop($image_styles, $crop_type, $entity->getFileUri());
-            // If the entity already exist & is not deleted by user update
-            // $crop_type_name crop entity.
-            if ($properties['crop_applied'] == '0' && !empty($crops)) {
-              $this->deleteCrop($entity->getFileUri(), $crop_type, $entity->id());
+
+          if (is_array($properties) && isset($properties)) {
+            $crop_exists = Crop::cropExists($entity->getFileUri(), $crop_type_name);
+            if (!$crop_exists) {
+              if ($properties['crop_applied'] == '1' && isset($properties) && (!empty($properties['width']) && !empty($properties['height']))) {
+                $this->applyCrop($properties, $form_state_values['image_crop'], $crop_type);
+              }
             }
-            elseif (isset($properties) && (!empty($properties['width']) && !empty($properties['height']))) {
-              $this->updateCrop($properties, [
-                'file-uri' => $entity->getFileUri(),
-                'file-id' => $entity->id(),
-              ], $crop_type);
+            else {
+              // Get all imagesStyle used this crop_type.
+              $image_styles = $this->getImageStylesByCrop($crop_type_name);
+              $crops = $this->loadImageStyleByCrop($image_styles, $crop_type, $entity->getFileUri());
+              // If the entity already exist & is not deleted by user update
+              // $crop_type_name crop entity.
+              if ($properties['crop_applied'] == '0' && !empty($crops)) {
+                $this->deleteCrop($entity->getFileUri(), $crop_type, $entity->id());
+              }
+              elseif (isset($properties) && (!empty($properties['width']) && !empty($properties['height']))) {
+                $this->updateCrop($properties, [
+                  'file-uri' => $entity->getFileUri(),
+                  'file-id' => $entity->id(),
+                ], $crop_type);
+              }
             }
           }
         }
       }
     }
+    else {
+      drupal_set_message(t('No File element found.'), 'error');
+      return;
+    }
   }
 
 }