You are here

public function FileDeleteMultiple::buildForm in Panopoly 8.2

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides ConfirmFormBase::buildForm

File

modules/panopoly/panopoly_media/src/Form/FileDeleteMultiple.php, line 117

Class

FileDeleteMultiple
Provides a file deletion confirmation form.

Namespace

Drupal\panopoly_media\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $this->fileInfo = $this->tempStoreFactory
    ->get('panopoly_media_file_multiple_delete_confirm')
    ->get($this
    ->getCurrentUser()
    ->id());
  if (empty($this->fileInfo)) {
    return new RedirectResponse($this
      ->getCancelUrl()
      ->setAbsolute()
      ->toString());
  }

  /** @var \Drupal\node\NodeInterface[] $nodes */
  $files = $this->storage
    ->loadMultiple($this->fileInfo);
  if ($this
    ->filesHaveUsage($files)) {
    $form['warning'] = [
      '#theme' => 'status_messages',
      // @todo Improve when https://www.drupal.org/node/2278383 lands.
      '#message_list' => [
        'warning' => [
          $this
            ->t('One or more of these files have usages recorded. Deleting may affect content that attempts to reference these files.'),
        ],
      ],
      '#status_headings' => [
        'status' => $this
          ->t('Status message'),
        'error' => $this
          ->t('Error message'),
        'warning' => $this
          ->t('Warning message'),
      ],
    ];
  }
  $items = [];
  foreach ($this->fileInfo as $fid) {
    if (!empty($files[$fid])) {
      $items[$fid] = $files[$fid]
        ->label();
    }
  }
  $form['files'] = [
    '#theme' => 'item_list',
    '#items' => $items,
  ];
  $form = parent::buildForm($form, $form_state);
  return $form;
}