Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / media / src / Form / MediaDeleteMultipleConfirmForm.php
1 <?php
2
3 namespace Drupal\media\Form;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Url;
9 use Drupal\Core\TempStore\PrivateTempStoreFactory;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Symfony\Component\HttpFoundation\RedirectResponse;
12
13 /**
14  * Provides a confirmation form to delete multiple media items at once.
15  *
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
20  *   module.
21  *
22  * @internal
23  */
24 class MediaDeleteMultipleConfirmForm extends ConfirmFormBase {
25
26   /**
27    * The array of media items to delete, indexed by ID and language.
28    *
29    * @var string[][]
30    */
31   protected $mediaItems = [];
32
33   /**
34    * The tempstore factory.
35    *
36    * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
37    */
38   protected $tempStoreFactory;
39
40   /**
41    * The entity storage.
42    *
43    * @var \Drupal\Core\Entity\EntityStorageInterface
44    */
45   protected $storage;
46
47   /**
48    * Constructs a MediaDeleteMultipleConfirmForm form object.
49    *
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.
54    */
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');
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public static function create(ContainerInterface $container) {
65     return new static(
66       $container->get('tempstore.private'),
67       $container->get('entity_type.manager')
68     );
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function getFormId() {
75     return 'media_multiple_delete_confirm';
76   }
77
78   /**
79    * {@inheritdoc}
80    */
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?');
83   }
84
85   /**
86    * {@inheritdoc}
87    */
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');
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function getConfirmText() {
98     return $this->t('Delete');
99   }
100
101   /**
102    * {@inheritdoc}
103    *
104    * @todo Change to trait or base class when #2843395 is done.
105    * @see https://www.drupal.org/node/2843395
106    */
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());
111     }
112     /** @var \Drupal\media\MediaInterface[] $entities */
113     $entities = $this->storage->loadMultiple(array_keys($this->mediaItems));
114
115     $items = [];
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();
121
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()) {
126           $names = [];
127           foreach ($languages as $translation_langcode => $language) {
128             $names[] = $language->getName();
129             unset($items[$id . ':' . $translation_langcode]);
130           }
131           $items[$default_key] = [
132             'label' => [
133               '#markup' => $this->t('@label (Original translation) - <em>The following translations will be deleted:</em>', ['@label' => $entity->label()]),
134             ],
135             'deleted_translations' => [
136               '#theme' => 'item_list',
137               '#items' => $names,
138             ],
139           ];
140         }
141         elseif (!isset($items[$default_key])) {
142           $items[$key] = $entity->label();
143         }
144       }
145     }
146
147     $form['entities'] = [
148       '#theme' => 'item_list',
149       '#items' => $items,
150     ];
151     return parent::buildForm($form, $form_state);
152   }
153
154   /**
155    * {@inheritdoc}
156    *
157    * @todo Change to trait or base class when #2843395 is done.
158    * @see https://www.drupal.org/node/2843395
159    */
160   public function submitForm(array &$form, FormStateInterface $form_state) {
161     if ($form_state->getValue('confirm') && !empty($this->mediaItems)) {
162       $total_count = 0;
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));
168
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());
176           }
177           elseif (!isset($delete_entities[$id])) {
178             $delete_translations[$id][] = $entity;
179           }
180         }
181       }
182
183       if ($delete_entities) {
184         $this->storage->delete($delete_entities);
185         $this->logger('media')->notice('Deleted @count media items.', ['@count' => count($delete_entities)]);
186       }
187
188       if ($delete_translations) {
189         $count = 0;
190         foreach ($delete_translations as $id => $translations) {
191           $entity = $entities[$id]->getUntranslated();
192           foreach ($translations as $translation) {
193             $entity->removeTranslation($translation->language()->getId());
194           }
195           $entity->save();
196           $count += count($translations);
197         }
198         if ($count) {
199           $total_count += $count;
200           $this->logger('media')->notice('Deleted @count media translations.', ['@count' => $count]);
201         }
202       }
203
204       if ($total_count) {
205         $this->messenger()->addStatus($this->formatPlural($total_count, 'Deleted 1 media item.', 'Deleted @count media items.'));
206       }
207
208       $this->tempStoreFactory->get('media_multiple_delete_confirm')->delete(\Drupal::currentUser()->id());
209     }
210
211     // @todo Change to media library when #2834729 is done.
212     // https://www.drupal.org/node/2834729.
213     $form_state->setRedirect('system.admin_content');
214   }
215
216 }