Form API : Image file Plugin | Drupal 8

Form API : Image file Plugin

Submitted by editor on Tue, 08/31/2021 - 10:58

Managed image allow to select image file, preview and remove.

Step 1. Create acustom module and a @FormElement usin the ManagedImage.php file

Step 2. Example of managed image

$form['image'] = [
  '#type' => 'managed_image',
  '#upload_location' => 'public://media/',
  '#multiple' => FALSE,
  '#title' => $this->t('Image'),
  '#default_value' => $fid ? [$fid] : [],
  '#upload_validators' => [
    'file_validate_extensions' => ['png jpg jpeg svg'],
  ],
];

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.