You are here

public function ImageWidgetCropManager::updateCrop in Image Widget Crop 8

Same name and namespace in other branches
  1. 8.2 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.

CropType $crop_type: The entity CropType.

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 100

Class

ImageWidgetCropManager
ImageWidgetCropManager calculation class.

Namespace

Drupal\image_widget_crop

Code

public function updateCrop(array $properties, $field_value, CropType $crop_type) {

  // Get Original sizes and position of crop zone.
  $crop_properties = $this
    ->getCropOriginalDimension($field_value, $properties);

  // Get all imagesStyle used this crop_type.
  $image_styles = $this
    ->getImageStylesByCrop($crop_type
    ->id());
  if (!empty($image_styles)) {
    $crops = $this
      ->loadImageStyleByCrop($image_styles, $crop_type, $field_value['file-uri']);
  }

  // If any crop exist add new crop.
  if (empty($crops)) {
    $this
      ->saveCrop($crop_properties, $field_value, $image_styles, $crop_type);
    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;
    if (!$this
      ->cropHasChanged($crop_properties, array_merge($crop
      ->position(), $crop
      ->size()))) {
      return;
    }
    $this
      ->updateCropProperties($crop, $crop_properties);
    drupal_set_message(t('The crop "@cropType" were successfully updated for image "@filename".', [
      '@cropType' => $crop_type
        ->label(),
      '@filename' => $this->fileStorage
        ->load($field_value['file-id'])
        ->getFilename(),
    ]));
  }
}