You are here

public function FileDeleteForm::submitForm in File Delete (D8/D9) 8

Same name and namespace in other branches
  1. 2.1.x src/Form/FileDeleteForm.php \Drupal\file_delete\Form\FileDeleteForm::submitForm()

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/FileDeleteForm.php, line 81

Class

FileDeleteForm
Provides a form for deleting a File.

Namespace

Drupal\file_delete\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $usages = $this->fileUsage
    ->listUsage($this->entity);
  if ($usages) {
    $url = new Url('view.files.page_2', [
      'arg_0' => $this->entity
        ->id(),
    ]);
    $this
      ->messenger()
      ->addError($this
      ->t('The file %file_name cannot be deleted because it is in use by the following modules: %modules.<br>Click <a href=":link_to_usages">here</a> to see its usages.', [
      '%file_name' => $this->entity
        ->getFilename(),
      '%modules' => implode(', ', array_keys($usages)),
      ':link_to_usages' => $url
        ->toString(),
    ]));
    return;
  }

  // Mark the file for removal by file_cron().
  $this->entity
    ->setTemporary();
  $this->entity
    ->save();
  $this
    ->messenger()
    ->addMessage($this
    ->t('The file %file_name has been marked for deletion.', [
    '%file_name' => $this->entity
      ->getFilename(),
  ]));
  $form_state
    ->setRedirect('view.files.page_1');
}