0

I am using Drupal 9.3 and Webform 6.1.7.

I have a Webform that contains a file element and am building a custom form to update the submission data.

I have found that when I submit my custom form I can load the Webform submission and set the file IDs and it will save the data fine with no issues.

However when it comes to removing a file after saving, the file I remove just remains attached.

I know that using the Webform interface the file upload / remove is working fine. However it seems to be a problem with removing files when updating the submission programmatically.

Is there any extra step I am missing to remove the files?

This is really simple version of the form I am building:

<?php

namespace Drupal\example\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides an example form.
 */
class WebformFileUpdateForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'example_webform_file_update_form';
  }

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

    // Load all webform submissions for the node.
    $nid = 86552;
    $query = $em->getStorage('webform_submission')
      ->getQuery()
      ->condition('webform_id', 'tutorial', '=')
      ->condition('entity_id', $nid)
      ->pager(10);

    $submission_ids = $query->execute();
    $webform_submissions = $em->getStorage('webform_submission')
      ->loadMultiple($submission_ids);

    $form['#tree'] = TRUE;

    $form['files'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Files'),
    ];

    foreach ($webform_submissions as $submission) {
      $form['files'][$submission->id()]['file'] = [
        '#type' => 'managed_file',
        '#title' => $this->t('File'),
        '#default_value' => $submission->getElementData('attached_files'),
        '#upload_location' => 'private://tutorials/' . $submission->id() . '/',
        '#multiple' => TRUE,
      ];
    }

    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Update'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $em = \Drupal::entityTypeManager();
    $values = $form_state->getValues();
    foreach ($values['files'] as $sid => $data) {
      $em->getStorage('webform_submission')
        ->load($sid)
        ->setElementData('attached_files', $data['file'])
        ->save();
    }
  }

}

1 Answer 1

1

This issue replicable for anonymous users only, kind of caching issue so if you add following cache API function code in custom form , then latest submission data 'll be load that 'll be added/removed by user while interacting with custom form.

 $form['#cache'] = [
   'contexts' => ['session']
 ];
4
  • That's fixed it perfectly thank you! Is there any specific documention you derived the answer from I could look at, or was it just experience you had?
    – inertiahz
    Jan 26 at 17:55
  • Well there is not any documentation for it , I used debugging skills to replicate and trace issue & found the root cause of issue & got the solution ;) Jan 27 at 7:37
  • You did a lot better than me! I debugged the webform post save file handler, saw that it was calling the file delete but never actually deleting the file or reference and would never of thought of the cache.
    – inertiahz
    Jan 27 at 16:03
  • no worries, mine pleasure to contribute to the community :) Jan 28 at 8:31

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.