You are here

public function WebformCompositeBase::postSave in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformCompositeBase.php \Drupal\webform\Plugin\WebformElement\WebformCompositeBase::postSave()

Acts on a saved webform submission element before the insert or update hook is invoked.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

bool $update: TRUE if the entity has been updated, or FALSE if it has been inserted.

Overrides WebformElementBase::postSave

File

src/Plugin/WebformElement/WebformCompositeBase.php, line 1369

Class

WebformCompositeBase
Provides a base for composite elements.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function postSave(array &$element, WebformSubmissionInterface $webform_submission, $update = TRUE) {
  $webform = $webform_submission
    ->getWebform();
  if ($webform
    ->isResultsDisabled() || !$this
    ->hasManagedFiles($element)) {
    return;
  }
  $original_data = $webform_submission
    ->getOriginalData();
  $data = $webform_submission
    ->getData();
  $composite_elements_managed_files = $this
    ->getManagedFiles($element);
  foreach ($composite_elements_managed_files as $composite_key) {
    $original_fids = $this
      ->getManagedFileIdsFromData($element, $original_data, $composite_key);
    $fids = $this
      ->getManagedFileIdsFromData($element, $data, $composite_key);

    // Delete the old file uploads.
    $delete_fids = array_diff($original_fids, $fids);
    WebformManagedFileBase::deleteFiles($webform_submission, $delete_fids);

    // Add new files.
    if ($fids) {
      $composite_element = $this
        ->getInitializedCompositeElement($element, $composite_key);

      /** @var \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase $composite_element_plugin */
      $composite_element_plugin = $this->elementManager
        ->getElementInstance($composite_element);
      $composite_element_plugin
        ->addFiles($composite_element, $webform_submission, $fids);
    }
  }
}