I have a form built with Drupal 8 FAPI. It's a sophisticated "contact us" type thing which includes the ability to upload files.

Among other things it includes several managed_file elements.

The elements are defined in buildForm() like this.

$form['myfile'] = [
    '#type' => 'managed_file',
    '#upload_location' => 'public://' . date('Y-m')

In submitForm() I look at $form_state->get('myfile'). If I'm logged in as admin (haven't tried other roles) this contains an array with one element who's value is the id of the created file entity. That all works well.

If I am not logged in, $form_state->get('myfile') is an empty array in submitForm().

With anonymous, the file is successfully uploaded because it physically appears in /sites/default/files/2016-05. The file entity is created because I can see it in /admin/content/files.

I must be missing something related to permissions. I tried adding '#access' => true to my form element definition but that did not help.

I have a content entity which includes a file field. The add entity form derived from ContentEntityForm is basically the same as my form and works correctly even for anonymous. I probably could use that instead of my form but I prefer to use my own form. This is partially a learning exercise.

Thanks for any help.

  • Which core version? There was a regression due to a security fix that was fixed a few days ago: drupal.org/node/2678822#comment-10926613. Make sure you update to 8.1.1 – Berdir May 10 '16 at 18:39
  • Thanks! That was it. I was running 8.1.0. I should have thought about updating earlier. Working really nicely now :-) – tetranz May 10 '16 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.