You are here

public function ImageWidgetCropManager::saveCrop in Image Widget Crop 8.2

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

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

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

Overrides ImageWidgetCropInterface::saveCrop

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 134

Class

ImageWidgetCropManager
ImageWidgetCropManager calculation class.

Namespace

Drupal\image_widget_crop

Code

public function saveCrop(array $crop_properties, $field_value, 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'],
  ];

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