FileDeleteForm.php in Panopoly 8.2
File
modules/panopoly/panopoly_media/src/Form/FileDeleteForm.php
View source
<?php
namespace Drupal\panopoly_media\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\file\FileUsage\FileUsageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FileDeleteForm extends ContentEntityDeleteForm {
protected $fileUsage;
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, FileUsageInterface $fileUsage) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->fileUsage = $fileUsage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('file.usage'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
if ($this->fileUsage
->listUsage($this->entity)) {
$form['warning'] = [
'#theme' => 'status_messages',
'#message_list' => [
'warning' => [
$this
->t('This file has usages recorded. Deleting it may affect content that attempts to reference it.'),
],
],
'#status_headings' => [
'status' => $this
->t('Status message'),
'error' => $this
->t('Error message'),
'warning' => $this
->t('Warning message'),
],
];
}
return $form;
}
public function getCancelUrl() {
return Url::fromRoute('<front>');
}
}