You are here

public function ImageWidgetCropManager::saveCrop in Image Widget Crop 8

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

Save the crop when this crop not exist.

Parameters

double[] $crop_properties: The properties of the crop applied to the original image (dimensions).

array|mixed $field_value: An array of values for the contained properties of image_crop widget.

array $image_styles: The list of imagesStyle available for this crop.

CropType $crop_type: The entity CropType.

bool $notify: Show notification after actions (default TRUE).

2 calls to ImageWidgetCropManager::saveCrop()
ImageWidgetCropManager::applyCrop in src/ImageWidgetCropManager.php
Create new crop entity with user properties.
ImageWidgetCropManager::updateCrop in src/ImageWidgetCropManager.php
Update old crop with new properties choose in UI.

File

src/ImageWidgetCropManager.php, line 145

Class

ImageWidgetCropManager
ImageWidgetCropManager calculation class.

Namespace

Drupal\image_widget_crop

Code

public function saveCrop(array $crop_properties, $field_value, array $image_styles, CropType $crop_type, $notify = TRUE) {
  $values = [
    'type' => $crop_type
      ->id(),
    'entity_id' => $field_value['file-id'],
    'entity_type' => 'file',
    'uri' => $field_value['file-uri'],
    'x' => $crop_properties['x'],
    'y' => $crop_properties['y'],
    'width' => $crop_properties['width'],
    'height' => $crop_properties['height'],
  ];

  // Save crop with previous values.

  /** @var \Drupal\crop\CropInterface $crop */
  $crop = $this->cropStorage
    ->create($values);
  $crop
    ->save();
  if ($notify) {
    drupal_set_message(t('The crop "@cropType" was successfully added for image "@filename".', [
      '@cropType' => $crop_type
        ->label(),
      '@filename' => $this->fileStorage
        ->load($field_value['file-id'])
        ->getFilename(),
    ]));
  }
}