You are here

public static function ImageCrop::validateHardLimit in Image Widget Crop 8

Same name and namespace in other branches
  1. 8.2 src/Element/ImageCrop.php \Drupal\image_widget_crop\Element\ImageCrop::validateHardLimit()

Form element validation handler for crop widget elements.

Parameters

array $element: All form elements without crop properties.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

See also

ImageCrop::getCropFormElement()

File

src/Element/ImageCrop.php, line 359

Class

ImageCrop
Provides a form element for crop.

Namespace

Drupal\image_widget_crop\Element

Code

public static function validateHardLimit(array $element, FormStateInterface $form_state) {

  /** @var \Drupal\crop\Entity\CropType $crop_type */
  $crop_type = \Drupal::entityTypeManager()
    ->getStorage('crop_type')
    ->load($element['#crop_type']);
  $parents = $element['#parents'];
  array_pop($parents);
  $crop_values = $form_state
    ->getValue($parents);
  $hard_limit = $crop_type
    ->getHardLimit();
  $action_button = $form_state
    ->getTriggeringElement()['#value'];

  // @todo We need to add this test in multilingual context because,
  // the "#value" element are a simple string in translate form,
  // and an TranslatableMarkup object in other cases.
  $operation = $action_button instanceof TranslatableMarkup ? $action_button
    ->getUntranslatedString() : $action_button;
  if ((int) $crop_values['crop_applied'] == 0 || $operation == 'Remove') {
    return;
  }
  $element_name = $element['#element_name'];
  if ($hard_limit[$element_name] !== 0 && !empty($hard_limit[$element_name])) {
    if ($hard_limit[$element_name] > (int) $crop_values[$element_name]) {
      $form_state
        ->setError($element, t('Crop @property is smaller then the allowed @hard_limitpx for @crop_name', [
        '@property' => $element_name,
        '@hard_limit' => $hard_limit[$element_name],
        '@crop_name' => $crop_type
          ->label(),
      ]));
    }
  }
}