I want to add a block containing an image field to Drupal to add it to the page layout.
I can see the image gets uploaded (because it appears in my file system), but the block forgets the image; next time I configure the block, the image I just uploaded has disappeared.
What I've got so far is the following code.
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
$build['image']['#markup'] = '';
return $build;
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form['image'] = array(
'#type' => 'managed_file',
'#upload_location' => 'public://images/',
'#title' => $this->t('Image'),
'#description' => t("Image to show on the contact form"),
'#default_value' => $this->configuration['image']['value'],
'#upload_validators' => array(
'file_validate_extensions' => array('gif png jpg jpeg'),
'file_validate_size' => array(25600000),
),
'#states' => array(
'visible' => array(
':input[name="image_type"]' => array('value' => t('Upload New Image(s)')),
)
)
);
return $form;
}
public function blockValidate($form, FormStateInterface $form_state) {}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$image = $form_state->getValue('image');
$this->configuration['image'] = $image;
$file = \Drupal\file\Entity\File::load($image[0]);
$file->setPermanent();
$file->save();
}
How do I display a user-uploaded image?
How do I make the block remember this picture?