public function ImageWidget::formElement

Overrides \Drupal\file\Plugin\field\widget\FileWidget::formElement().

Overrides FileWidget::formElement

File

drupal/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php, line 78
Contains \Drupal\image\Plugin\field\widget\ImageWidget.

Class

ImageWidget
Plugin implementation of the 'image_image' widget.

Namespace

Drupal\image\Plugin\field\widget

Code

public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
  $element = parent::formElement($items, $delta, $element, $langcode, $form, $form_state);
  $settings = $this->instance['settings'];

  // Add upload resolution validation.
  if ($settings['max_resolution'] || $settings['min_resolution']) {
    $element['#upload_validators']['file_validate_image_resolution'] = array(
      $settings['max_resolution'],
      $settings['min_resolution'],
    );
  }

  // If not using custom extension validation, ensure this is an image.
  $supported_extensions = array(
    'png',
    'gif',
    'jpg',
    'jpeg',
  );
  $extensions = isset($element['#upload_validators']['file_validate_extensions'][0]) ? $element['#upload_validators']['file_validate_extensions'][0] : implode(' ', $supported_extensions);
  $extensions = array_intersect(explode(' ', $extensions), $supported_extensions);
  $element['#upload_validators']['file_validate_extensions'][0] = implode(' ', $extensions);

  // Add all extra functionality provided by the image widget.
  $element['#process'][] = 'image_field_widget_process';

  // Add properties needed by image_field_widget_process().
  $element['#preview_image_style'] = $this
    ->getSetting('preview_image_style');
  $element['#title_field'] = $settings['title_field'];
  $element['#alt_field'] = $settings['alt_field'];
  return $element;
}