I'm creating a image upload input field in blockForm which will output to Twig later. But got stuck due to some stuff.

public function blockForm($form, FormStateInterface $form_state){
        $form['image_file'] = [
            '#type' => 'managed_file',
            '#title' => $this->t('Upload Your Image'),
            '#default_value' => (isset($this->configuration['image_file']) ? $this->configuration['image_file'] : ""),
            '#weight' => '1',
            '#upload_location' => 'public://',
            '#upload_validators' => [
                'file_validate_extensions' => ['gif png jpg jpeg'],

       return $form;

First thing is the '#upload_location', there are a lot of different answer online and the only difference is the path after public://, so I want to know what should I actually deal with it.

Second is the use, is there something missing or it is enough for achieve my goal.

The code I have now:

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormBuilder;
use Drupal\Core\Link;
use Drupal\image\Entity\ImageStyle;
use Drupal\paragraphs\Entity\Paragraph;

Hope someone can answer and help. Many Thanks.


As i understand you could use something like this '#type' => 'file'

    '#type' => 'file',
   '#title' => t('Upload YourImage'),
    '#upload_validators' => array(
    'file_validate_extensions' => array('gif'),),

return $form;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.