You are here

public function FileEditForm::submitForm in File Entity (fieldable files) 8.2

This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state can be updated, this way the subsequently invoked handlers can retrieve a regular entity object to act on. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ContentEntityForm::submitForm

File

src/Form/FileEditForm.php, line 157

Class

FileEditForm
Form controller for file type forms.

Namespace

Drupal\file_entity\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Check if a replacement file has been uploaded.
  if ($form_state
    ->getValue('replace_upload')) {
    $replacement = $form_state
      ->getValue('replace_upload')[0];
    if ($replacement instanceof FileEntity) {
      $entity_replacement = $replacement;
    }
    else {
      $entity_replacement = File::load($replacement);
    }
    $log_args = array(
      '@old' => $this->entity
        ->getFilename(),
      '@new' => $entity_replacement
        ->getFileName(),
    );

    // Move file from temp to permanent home.
    if ($this->fileSystem
      ->copy($entity_replacement
      ->getFileUri(), $this->entity
      ->getFileUri(), FileSystemInterface::EXISTS_REPLACE)) {
      $entity_replacement
        ->delete();
      \Drupal::logger('file_entity')
        ->info('File @old was replaced by @new', $log_args);
    }
    else {
      \Drupal::logger('file_entity')
        ->notice('File @old failed to be replaced by @new', $log_args);
    }
  }
  parent::submitForm($form, $form_state);
}