function ImageItem::validateResolution

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()
  2. 8.9.x core/modules/image/src/Plugin/Field/FieldType/ImageItem.php \Drupal\image\Plugin\Field\FieldType\ImageItem::validateResolution()
  3. 11.x core/modules/image/src/Plugin/Field/FieldType/ImageItem.php \Drupal\image\Plugin\Field\FieldType\ImageItem::validateResolution()

Element validate function for dimensions fields.

File

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

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 $this->t()
        // here, so it won't be escaped again as it's already marked safe.
        $form_state->setError($element[$dimension], new TranslatableMarkup('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, '');
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.