You are here

public static function ImageItem::validateResolution in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/image/src/Plugin/Field/FieldType/ImageItem.php \Drupal\image\Plugin\Field\FieldType\ImageItem::validateResolution()

Element validate function for resolution fields.

File

core/modules/image/src/Plugin/Field/FieldType/ImageItem.php, line 393

Class

ImageItem
Plugin implementation of the 'image' field type.

Namespace

Drupal\image\Plugin\Field\FieldType

Code

public static function validateResolution($element, FormStateInterface $form_state) {
  if (!empty($element['x']['#value']) || !empty($element['y']['#value'])) {
    foreach ([
      'x',
      'y',
    ] as $dimension) {
      if (!$element[$dimension]['#value']) {

        // We expect the field name placeholder value to be wrapped in t()
        // here, so it won't be escaped again as it's already marked safe.
        $form_state
          ->setError($element[$dimension], t('Both a height and width value must be specified in the @name field.', [
          '@name' => $element['#title'],
        ]));
        return;
      }
    }
    $form_state
      ->setValueForElement($element, $element['x']['#value'] . 'x' . $element['y']['#value']);
  }
  else {
    $form_state
      ->setValueForElement($element, '');
  }
}