Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
improved formatting; edited tags
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

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 receiveget the following error:

StatusText: Internal Server Error
ResponseText: {"message":"A fatal error occurred: Serialization of \u0027Symfony\\Component\\HttpFoundation\\File\\UploadedFile\u0027 is not allowed"}

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 apiAPI to allow users to upload files?

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 receive

StatusText: Internal Server Error
ResponseText: {"message":"A fatal error occurred: Serialization of \u0027Symfony\\Component\\HttpFoundation\\File\\UploadedFile\u0027 is not allowed"}

Is there something I'm missing. Is it possible to use the form api to allow users to upload files?

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?

Source Link
Amy
  • 5.8k
  • 13
  • 66
  • 133

Add file field with Form API

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 receive

StatusText: Internal Server Error
ResponseText: {"message":"A fatal error occurred: Serialization of \u0027Symfony\\Component\\HttpFoundation\\File\\UploadedFile\u0027 is not allowed"}

Is there something I'm missing. Is it possible to use the form api to allow users to upload files?