0

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?

4
  • Have you checked $form_state->getValue('image'), if it's saving only the "fid" of the image?
    – ssibal
    Commented Jul 26, 2016 at 7:56
  • When I submit, I automatically get that error, even if I ignore it completely in blockSubmit() and build(). This error appears before it gets to those functions.
    – Amy
    Commented Jul 26, 2016 at 13:15
  • Turns out I am only getting the fid of the image. Is there a way to turn that into a url so I can use it in <img src>?
    – Amy
    Commented Jul 26, 2016 at 15:06
  • Try adding $form_state->disableCache();. Have a look here drupal.org/node/2647812#comment-11683961
    – milkovsky
    Commented Nov 18, 2016 at 15:16

1 Answer 1

0

I discovered that the error only occurred when using the in-place panel editor. When adding the block in the panel page editor there were no errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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