File upload becomes empty after form submit for anonymous users
I've build a form in Drupal 9 with Form API. When logged in as admin, all is working fine. Selecting a file as anonymous works, and the validations are checked. But when I submit the form, the managed_file becomes empty. Is this a permission issue? Which one?
/** * @param array $form * @param \Drupal\Core\Form\FormStateInterface $form_state * * @return array */ public function buildForm(array $form, FormStateInterface $form_state): array { $form['logo'] = [ '#type' => 'managed_file', '#title' => $this->t('Logo'), '#upload_validators' => [ 'file_validate_extensions' => ['png'], 'file_validate_size' => [2 * 1024 * 1024], 'custom_validate_image_resolution' => [1500], ], '#upload_location' => 'public://logos/', '#required' => TRUE, ]; $form['submit'] = [ '#type' => 'container', '#prefix' => '<div class="col-12">', '#suffix' => '</div>', ]; $form['submit']['submit'] = [ '#type' => 'submit', '#value' => $this->t('Submit'), ]; return $form; } /** * Custom validator for image dimensions where either width or height should * meet the minimum size. * * @param File $file * The file ID to validate. * @param int $minimum_dimension * The minimum dimension size. * * @return array * An array of error messages, empty if no errors. */ function custom_validate_image_resolution(File $file, int $minimum_dimension = 1500): array { $errors = []; $uri = $file->getFileUri(); // Load the file as an image. $image = \Drupal::service('image.factory')->get($uri); if ($image->isValid()) { $width = $image->getWidth(); $height = $image->getHeight(); // Check if either dimension is below the minimum size. if ($width < $minimum_dimension && $height < $minimum_dimension) { $errors[] = t('The image dimensions must have at least a width or height of %dimensions pixels.', ['%dimensions' => $minimum_dimension]); } } else { $errors[] = t('Unable to load the image file.'); } return $errors; } /** * @throws \Drupal\Core\Entity\EntityStorageException */ public function validateForm(array &$form, FormStateInterface $form_state) { // on choose file, $fids is populated, on submit it becomes empty :/ $fids = $form_state->getValue('logo'); if (!empty($fids) && !empty($fids[0])) { $fid = reset($fids); // Get the first element (file ID) $file = \Drupal\file\Entity\File::load($fid); if ($file) { $errors = $this->custom_validate_image_resolution($file, 1500); if (!empty($errors)) { // Log errors and set form errors foreach ($errors as $error) { $form_state->setErrorByName('logo', $error); } // Delete the file if it's temporary if ($file->isTemporary()) { $file->delete(); } } } else { \Drupal::logger('my_module') ->error('Failed to load file with ID: ' . $fid); } } else { $form_state->setErrorByName('logo', t('No valid file uploaded.')); } } /** * @throws \Drupal\Core\Entity\EntityStorageException */ public function submitForm(array &$form, FormStateInterface $form_state) { $node = Node::create([ 'title' => $form_state->getValue(['title' => 'textfield']), 'type' => 'my-node', 'status' => 0, 'field_logo' => [ 'target_id' => reset($form_state->getValue('logo')), 'alt' => $form_state->getValue(['title' => 'textfield']), 'title' => $form_state->getValue(['title' => 'textfield']), ], ]); $node->save(); } }