You are here

function photos_image_edit_submit_crops in Album Photos 8.4

Form submission handler for image_widget_crop.

1 string reference to 'photos_image_edit_submit_crops'
photos_form_photos_image_edit_alter in ./photos.module
Implements hook_form_FORM_ID_alter().

File

./photos.module, line 602
Implementation of photos.module.

Code

function photos_image_edit_submit_crops(array &$form, FormStateInterface $form_state) {
  $form_state_values = $form_state
    ->getValues();

  // Skip if image is being deleted.
  if (empty($form_state_values['del'])) {
    $fid = $form_state_values['fid'];

    /** @var \Drupal\file\FileInterface $entity. */
    $entity = \Drupal::entityTypeManager()
      ->getStorage('file')
      ->load($fid);

    /** @var \Drupal\image_widget_crop\ImageWidgetCropManager $image_widget_crop_manager. */
    $image_widget_crop_manager = \Drupal::service('image_widget_crop.manager');

    // 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 = \Drupal::entityTypeManager()
        ->getStorage('crop_type')
        ->load($crop_type_name);

      // If the crop type needed is disabled or delete.
      if (empty($crop_type) && $crop_type instanceof CropType) {
        \Drupal::messenger()
          ->addError(t("The CropType ('@cropType') is not active or not defined. Please verify configuration of image style or ImageWidgetCrop formatter configuration", [
          '@cropType' => $crop_type
            ->id(),
        ]));
        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']))) {
            $image_widget_crop_manager
              ->applyCrop($properties, $form_state_values['image_crop'], $crop_type);
          }
        }
        else {

          // Get all imagesStyle used this crop_type.
          $image_styles = $image_widget_crop_manager
            ->getImageStylesByCrop($crop_type_name);
          $crops = $image_widget_crop_manager
            ->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)) {
            $image_widget_crop_manager
              ->deleteCrop($entity
              ->getFileUri(), $crop_type, $entity
              ->id());
          }
          elseif (isset($properties) && (!empty($properties['width']) && !empty($properties['height']))) {
            $image_widget_crop_manager
              ->updateCrop($properties, [
              'file-uri' => $entity
                ->getFileUri(),
              'file-id' => $entity
                ->id(),
            ], $crop_type);
          }
        }
      }
    }
  }
}