panopoly_media-8.x-2.x-dev/src/Form/FileDeleteForm.php
src/Form/FileDeleteForm.php
<?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;
/**
* Provides a form for deleting a file.
*/
class FileDeleteForm extends ContentEntityDeleteForm {
/**
* The file usage service.
*
* @var \Drupal\file\FileUsage\FileUsageInterface
*/
protected $fileUsage;
/**
* Constructs a FileDeleteForm object.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity manager.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
* @param \Drupal\file\FileUsage\FileUsageInterface $fileUsage
* The file usage service.
*/
public function __construct(EntityRepositoryInterface $entity_repository, ?EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, ?TimeInterface $time = NULL, ?FileUsageInterface $fileUsage = NULL) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->fileUsage = $fileUsage;
}
/**
* {@inheritdoc}
*/
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')
);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
/** @var \Drupal\file\FileInterface $file */
if ($this->fileUsage->listUsage($this->entity)) {
$form['warning'] = [
'#theme' => 'status_messages',
// @todo Improve when https://www.drupal.org/node/2278383 lands.
'#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;
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
// Needed because there is no canonical link for file entities.
return Url::fromRoute('<front>');
}
}
