function ImageItem::defaultImageForm

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

Builds the default_image details element.

Parameters

array $element: The form associative array passed by reference.

array $settings: The field settings array.

File

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

Class

ImageItem
Plugin implementation of the 'image' field type.

Namespace

Drupal\image\Plugin\Field\FieldType

Code

protected function defaultImageForm(array &$element, array $settings) {
  $element['default_image'] = [
    '#type' => 'details',
    '#title' => $this->t('Default image'),
    '#open' => TRUE,
  ];
  // Convert the stored UUID to a FID.
  $fids = [];
  $uuid = $settings['default_image']['uuid'];
  if ($uuid && $file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid)) {
    $fids[0] = $file->id();
  }
  $element['default_image']['uuid'] = [
    '#type' => 'managed_file',
    '#title' => $this->t('Image'),
    '#description' => $this->t('Image to be shown if no image is uploaded.'),
    '#default_value' => $fids,
    '#upload_location' => $settings['uri_scheme'] . '://default_images/',
    '#element_validate' => [
      '\\Drupal\\file\\Element\\ManagedFile::validateManagedFile',
      [
        static::class,
        'validateDefaultImageForm',
      ],
    ],
    '#upload_validators' => $this->getUploadValidators(),
  ];
  $element['default_image']['alt'] = [
    '#type' => 'textfield',
    '#title' => $this->t('Alternative text'),
    '#description' => $this->t('Short description of the image used by screen readers and displayed when the image is not loaded. This is important for accessibility.'),
    '#default_value' => $settings['default_image']['alt'],
    '#maxlength' => 512,
  ];
  $element['default_image']['title'] = [
    '#type' => 'textfield',
    '#title' => $this->t('Title'),
    '#description' => $this->t('The title attribute is used as a tooltip when the mouse hovers over the image.'),
    '#default_value' => $settings['default_image']['title'],
    '#maxlength' => 1024,
  ];
  $element['default_image']['width'] = [
    '#type' => 'value',
    '#value' => $settings['default_image']['width'],
  ];
  $element['default_image']['height'] = [
    '#type' => 'value',
    '#value' => $settings['default_image']['height'],
  ];
}

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