I'm writing a block plugin that uses the form API to allow users to edit its settings, and then output them into a template.
I'm currently using
<?php
/**
* @file
* Contains \Drupal\my_plugins\Plugin\Block\InfoWithSideImage.
*/
namespace Drupal\my_plugins\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
/**
* @Block(
* id = "infowithsideimage",
* admin_label = @Translation("Info block beside an image")
* )
*/
class InfoWithSideImage extends BlockBase {
public function blockForm($form, FormStateInterface $form_state) {
$form['image'] = array(
'#type' => 'file',
'#title' => $this->t('Image'),
'#default_value' => $this->configuration['image_submit'],
);
$form['test'] = array(
'#type' => 'textfield',
'#title' => $this->t('Test text field'),
'#default_value' => $this->configuration['test_submit'],
);
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['image_submit'] = $form_state->getValue('image');
$this->configuration['test_submit'] = $form_state->getValue('test');
}
public function build() {
return array(
'#theme' => 'infowithsideimage',
'#image' => $this->configuration['image_submit'],
'#test' => $this->configuration['test_submit'],
);
}
}
The text field works fine, but whenever I try to submit with the file field I get the following error:
A fatal error occurred: Serialization of Symfony\Component\HttpFoundation\File\UploadedFile is not allowed
Is there something I'm missing? Is it possible to use the form API to allow users to upload files?
$form_state->disableCache();
. Have a look here drupal.org/node/2647812#comment-11683961