You are here

public function ImageWidgetCropManager::updateCrop in Image Widget Crop 8.2

Same name and namespace in other branches
  1. 8 src/ImageWidgetCropManager.php \Drupal\image_widget_crop\ImageWidgetCropManager::updateCrop()

Update old crop with new properties choose in UI.

Parameters

array $properties: All properties returned by the crop plugin (js), and the size of thumbnail image.

array|mixed $field_value: An array of values contain properties of image_crop widget.

\Drupal\crop\Entity\CropType $crop_type: The entity CropType.

Overrides ImageWidgetCropInterface::updateCrop

2 calls to ImageWidgetCropManager::updateCrop()
ImageWidgetCropManager::buildCropToEntity in src/ImageWidgetCropManager.php
Fetch all fields FileField and use "image_crop" element on an entity.
ImageWidgetCropManager::buildCropToForm in src/ImageWidgetCropManager.php
Fetch all form elements using image_crop element.

File

src/ImageWidgetCropManager.php, line 105

Class

ImageWidgetCropManager
ImageWidgetCropManager calculation class.

Namespace

Drupal\image_widget_crop

Code

public function updateCrop(array $properties, $field_value, CropType $crop_type) {
  $crop_properties = $this
    ->getCropOriginalDimension($field_value, $properties);
  if (!empty($crop_properties)) {
    $image_styles = $this
      ->getImageStylesByCrop($crop_type
      ->id());
    if (!empty($image_styles)) {
      $crops = $this
        ->loadImageStyleByCrop($image_styles, $crop_type, $field_value['file-uri']);
    }
    if (empty($crops)) {
      $this
        ->saveCrop($crop_properties, $field_value, $crop_type, $this->imageWidgetCropSettings
        ->get('settings.notify_update'));
      return;
    }

    /** @var \Drupal\crop\Entity\Crop $crop */
    foreach ($crops as $crop) {
      if (!$this
        ->cropHasChanged($crop_properties, array_merge($crop
        ->position(), $crop
        ->size()))) {
        return;
      }
      $this
        ->updateCropProperties($crop, $crop_properties);
      $this
        ->messenger()
        ->addMessage($this
        ->t('The crop "@cropType" was successfully updated for image "@filename".', [
        '@cropType' => $crop_type
          ->label(),
        '@filename' => $this->fileStorage
          ->load($field_value['file-id'])
          ->getFilename(),
      ]));
    }
  }
}