public function ImageWidgetCropManager::buildCropToForm in Image Widget Crop 8
Same name and namespace in other branches
- 8.2 src/ImageWidgetCropManager.php \Drupal\image_widget_crop\ImageWidgetCropManager::buildCropToForm()
Fetch all form elements using image_crop element.
Parameters
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
File
- src/
ImageWidgetCropManager.php, line 517
Class
- ImageWidgetCropManager
- ImageWidgetCropManager calculation class.
Namespace
Drupal\image_widget_cropCode
public function buildCropToForm(FormStateInterface $form_state) {
/** @var \Drupal\file_entity\Entity\FileEntity $entity */
$entity = $form_state
->getFormObject()
->getEntity();
$form_state_values = $form_state
->getValues();
if (is_array($form_state_values['image_crop']) && isset($form_state_values['image_crop']['crop_wrapper'])) {
// 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 = $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 (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']))) {
$this
->applyCrop($properties, $form_state_values['image_crop'], $crop_type);
}
}
else {
// Get all imagesStyle used this crop_type.
$image_styles = $this
->getImageStylesByCrop($crop_type_name);
$crops = $this
->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)) {
$this
->deleteCrop($entity
->getFileUri(), $crop_type, $entity
->id());
}
elseif (isset($properties) && (!empty($properties['width']) && !empty($properties['height']))) {
$this
->updateCrop($properties, [
'file-uri' => $entity
->getFileUri(),
'file-id' => $entity
->id(),
], $crop_type);
}
}
}
}
}
}