Problem/Motivation

When uploading a CSV file via a managed_file form element in Drupal 11, This causes a fatal error during loading csv file.

The website encountered an unexpected error. Try again later.

Exception: Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed in serialize() (line 14 of core/lib/Drupal/Component/Serialization/PhpSerialize.php).

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Comments

saibher created an issue. See original summary.

saibher’s picture

StatusFileSize
new16.43 KB

The error occurs because the form is cached in Drupal 11 in a way that prevents serialization.
To correctly load the files, disable the cache on the form state as follows:


$form_state->disableCache();

quietone’s picture

Version: 11.2.x-dev » 11.x-dev
Component: forms system » file system
Issue summary: View changes
Issue tags: -Exception: Serialization, -CSV, -deprecation, -file upload, -validation, -Novice, -managed_file

Hi, in Drupal core changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies.

Changing tags per Issue tags field and Issue tags -- special tags

Thanks.

cilefen’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: +Needs steps to reproduce

This CSV import doesn't seem to be Drupal Core functionality. What are the modules needed to reproduce the bug?

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

matthiasm11’s picture

Status: Postponed (maintainer needs more info) » Needs work

I can confirm this bug.

I have a custom entity with:

$fields['logo'] = BaseFieldDefinition::create('file')
      ->setTranslatable(TRUE)
      ->setLabel(t('Logo'))
      ->setSettings([
        'uri_scheme' => 'public',
        'file_directory' => 'logos',
        'file_extensions' => 'jpg jpeg png svg',
      ])
      ->setDisplayOptions('form', [
        'type' => 'file_generic',
        'weight' => 5,
      ])

Adding the following lines fixes the issue, but this should probably be fixed in core. I haven't had the time to look into it.

class MyEntityForm extends ContentEntityForm {

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state): array {
    $form = parent::buildForm($form, $form_state);

    // Fixes an issue in core when uploading files (eg. the logo).
    // https://www.drupal.org/project/drupal/issues/3553499
    $form_state->disableCache();

    return $form;
  }

}
cilefen’s picture

Status: Needs work » Postponed (maintainer needs more info)
Issue tags: +Needs issue summary update

So, is this unrelated to CSVs? The issue title and summary specifies CSVs.

This needs proper steps to reproduce in the issue summary and a title update, or it will languish.