You are here

public function ImageWidgetCropManager::buildCropToEntity in Image Widget Crop 8

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

Fetch all fields FileField and use "image_crop" element on an entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity object.

File

src/ImageWidgetCropManager.php, line 455

Class

ImageWidgetCropManager
ImageWidgetCropManager calculation class.

Namespace

Drupal\image_widget_crop

Code

public function buildCropToEntity(EntityInterface $entity) {
  if (isset($entity) && $entity instanceof FieldableEntityInterface) {

    // Loop all fields of the saved entity.
    foreach ($entity
      ->getFields() as $entity_fields) {

      // If current field is FileField and use imageWidgetCrop.
      if ($entity_fields instanceof FileFieldItemList) {

        /* First loop to get each elements independently in the field values.
           Required if the image field cardinality > 1. */
        foreach ($entity_fields
          ->getValue() as $crop_elements) {
          foreach ($crop_elements as $crop_element) {
            if (is_array($crop_element) && isset($crop_element['crop_wrapper'])) {

              // Reload image since its URI could have been changed by other modules.

              /** @var \Drupal\file_entity\Entity\FileEntity $file */
              $file = $this->fileStorage
                ->load($crop_element['file-id']);
              $crop_element['file-uri'] = $file
                ->getFileUri();

              // Parse all value of a crop_wrapper element and get properties
              // associate with her CropType.
              foreach ($crop_element['crop_wrapper'] as $crop_type_name => $properties) {
                $properties = $properties['crop_container']['values'];

                /** @var \Drupal\crop\Entity\CropType $crop_type */
                $crop_type = $this->cropTypeStorage
                  ->load($crop_type_name);

                // If the crop type needed is disabled or delete.
                if (empty($crop_type) && $crop_type instanceof CropType) {
                  drupal_set_message(t("The CropType ('@cropType') is not active or not defined. Please verify configuration of image style or ImageWidgetCrop formatter configuration", [
                    '@cropType' => $crop_type
                      ->id(),
                  ]), 'error');
                  return;
                }

                // If this crop is available to create an crop entity.
                if ($entity
                  ->isNew()) {
                  if ($properties['crop_applied'] == '1' && isset($properties) && (!empty($properties['width']) && !empty($properties['height']))) {
                    $this
                      ->applyCrop($properties, $crop_element, $crop_type);
                  }
                }
                else {

                  // Get all imagesStyle used this crop_type.
                  $image_styles = $this
                    ->getImageStylesByCrop($crop_type_name);
                  $crops = $this
                    ->loadImageStyleByCrop($image_styles, $crop_type, $crop_element['file-uri']);

                  // If the entity already exist & is not deleted by user
                  // update $crop_type_name crop entity.
                  if ($properties['crop_applied'] == '0' && !empty($crops)) {
                    $this
                      ->deleteCrop($crop_element['file-uri'], $crop_type, $crop_element['file-id']);
                  }
                  elseif (isset($properties) && (!empty($properties['width']) && !empty($properties['height']))) {
                    $this
                      ->updateCrop($properties, $crop_element, $crop_type);
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}