3 namespace Drupal\media\Form;
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\TempStore\PrivateTempStoreFactory;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Symfony\Component\HttpFoundation\RedirectResponse;
14 * Provides a confirmation form to delete multiple media items at once.
16 * @deprecated in Drupal 8.6.x, to be removed before Drupal 9.0.0.
17 * This route is not used in Drupal core. As an internal API, it may also be
18 * removed in a minor release. If you are using it, copy the class
19 * and the related "entity.media.multiple_delete_confirm" route to your
24 class MediaDeleteMultipleConfirmForm extends ConfirmFormBase {
27 * The array of media items to delete, indexed by ID and language.
31 protected $mediaItems = [];
34 * The tempstore factory.
36 * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
38 protected $tempStoreFactory;
43 * @var \Drupal\Core\Entity\EntityStorageInterface
48 * Constructs a MediaDeleteMultipleConfirmForm form object.
50 * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
51 * The tempstore factory.
52 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $manager
53 * The entity type manager.
55 public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $manager) {
56 @trigger_error(__CLASS__ . ' is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. It is not used in Drupal core. As an internal API, it may also be removed in a minor release. If you are using it, copy the class and the related "entity.media.multiple_delete_confirm" route to your module.', E_USER_DEPRECATED);
57 $this->tempStoreFactory = $temp_store_factory;
58 $this->storage = $manager->getStorage('media');
64 public static function create(ContainerInterface $container) {
66 $container->get('tempstore.private'),
67 $container->get('entity_type.manager')
74 public function getFormId() {
75 return 'media_multiple_delete_confirm';
81 public function getQuestion() {
82 return $this->formatPlural(count($this->mediaItems), 'Are you sure you want to delete this item?', 'Are you sure you want to delete these items?');
88 public function getCancelUrl() {
89 // @todo Change to media library when #2834729 is done.
90 // https://www.drupal.org/node/2834729.
91 return new Url('system.admin_content');
97 public function getConfirmText() {
98 return $this->t('Delete');
104 * @todo Change to trait or base class when #2843395 is done.
105 * @see https://www.drupal.org/node/2843395
107 public function buildForm(array $form, FormStateInterface $form_state) {
108 $this->mediaItems = $this->tempStoreFactory->get('media_multiple_delete_confirm')->get($this->currentUser()->id());
109 if (empty($this->mediaItems)) {
110 return new RedirectResponse($this->getCancelUrl()->setAbsolute()->toString());
112 /** @var \Drupal\media\MediaInterface[] $entities */
113 $entities = $this->storage->loadMultiple(array_keys($this->mediaItems));
116 foreach ($this->mediaItems as $id => $langcodes) {
117 foreach ($langcodes as $langcode) {
118 $entity = $entities[$id]->getTranslation($langcode);
119 $key = $id . ':' . $langcode;
120 $default_key = $id . ':' . $entity->getUntranslated()->language()->getId();
122 // If we have a translated entity we build a nested list of translations
123 // that will be deleted.
124 $languages = $entity->getTranslationLanguages();
125 if (count($languages) > 1 && $entity->isDefaultTranslation()) {
127 foreach ($languages as $translation_langcode => $language) {
128 $names[] = $language->getName();
129 unset($items[$id . ':' . $translation_langcode]);
131 $items[$default_key] = [
133 '#markup' => $this->t('@label (Original translation) - <em>The following translations will be deleted:</em>', ['@label' => $entity->label()]),
135 'deleted_translations' => [
136 '#theme' => 'item_list',
141 elseif (!isset($items[$default_key])) {
142 $items[$key] = $entity->label();
147 $form['entities'] = [
148 '#theme' => 'item_list',
151 return parent::buildForm($form, $form_state);
157 * @todo Change to trait or base class when #2843395 is done.
158 * @see https://www.drupal.org/node/2843395
160 public function submitForm(array &$form, FormStateInterface $form_state) {
161 if ($form_state->getValue('confirm') && !empty($this->mediaItems)) {
163 $delete_entities = [];
164 /** @var \Drupal\Core\Entity\ContentEntityInterface[][] $delete_translations */
165 $delete_translations = [];
166 /** @var \Drupal\media\MediaInterface[] $entities */
167 $entities = $this->storage->loadMultiple(array_keys($this->mediaItems));
169 foreach ($this->mediaItems as $id => $langcodes) {
170 foreach ($langcodes as $langcode) {
171 $entity = $entities[$id]->getTranslation($langcode);
172 if ($entity->isDefaultTranslation()) {
173 $delete_entities[$id] = $entity;
174 unset($delete_translations[$id]);
175 $total_count += count($entity->getTranslationLanguages());
177 elseif (!isset($delete_entities[$id])) {
178 $delete_translations[$id][] = $entity;
183 if ($delete_entities) {
184 $this->storage->delete($delete_entities);
185 $this->logger('media')->notice('Deleted @count media items.', ['@count' => count($delete_entities)]);
188 if ($delete_translations) {
190 foreach ($delete_translations as $id => $translations) {
191 $entity = $entities[$id]->getUntranslated();
192 foreach ($translations as $translation) {
193 $entity->removeTranslation($translation->language()->getId());
196 $count += count($translations);
199 $total_count += $count;
200 $this->logger('media')->notice('Deleted @count media translations.', ['@count' => $count]);
205 $this->messenger()->addStatus($this->formatPlural($total_count, 'Deleted 1 media item.', 'Deleted @count media items.'));
208 $this->tempStoreFactory->get('media_multiple_delete_confirm')->delete(\Drupal::currentUser()->id());
211 // @todo Change to media library when #2834729 is done.
212 // https://www.drupal.org/node/2834729.
213 $form_state->setRedirect('system.admin_content');