Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\node\Form;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityManagerInterface;
|
Chris@0
|
6 use Drupal\Core\Form\ConfirmFormBase;
|
Chris@0
|
7 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
8 use Drupal\Core\Url;
|
Chris@14
|
9 use Drupal\Core\TempStore\PrivateTempStoreFactory;
|
Chris@0
|
10 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
11 use Symfony\Component\HttpFoundation\RedirectResponse;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Provides a node deletion confirmation form.
|
Chris@14
|
15 *
|
Chris@14
|
16 * @internal
|
Chris@0
|
17 */
|
Chris@0
|
18 class DeleteMultiple extends ConfirmFormBase {
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * The array of nodes to delete.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var string[][]
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $nodeInfo = [];
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The tempstore factory.
|
Chris@0
|
29 *
|
Chris@14
|
30 * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $tempStoreFactory;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * The node storage.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var \Drupal\Core\Entity\EntityStorageInterface
|
Chris@0
|
38 */
|
Chris@14
|
39 protected $storage;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * Constructs a DeleteMultiple form object.
|
Chris@0
|
43 *
|
Chris@14
|
44 * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
|
Chris@0
|
45 * The tempstore factory.
|
Chris@0
|
46 * @param \Drupal\Core\Entity\EntityManagerInterface $manager
|
Chris@0
|
47 * The entity manager.
|
Chris@0
|
48 */
|
Chris@0
|
49 public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityManagerInterface $manager) {
|
Chris@0
|
50 $this->tempStoreFactory = $temp_store_factory;
|
Chris@0
|
51 $this->storage = $manager->getStorage('node');
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * {@inheritdoc}
|
Chris@0
|
56 */
|
Chris@0
|
57 public static function create(ContainerInterface $container) {
|
Chris@0
|
58 return new static(
|
Chris@14
|
59 $container->get('tempstore.private'),
|
Chris@0
|
60 $container->get('entity.manager')
|
Chris@0
|
61 );
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * {@inheritdoc}
|
Chris@0
|
66 */
|
Chris@0
|
67 public function getFormId() {
|
Chris@0
|
68 return 'node_multiple_delete_confirm';
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * {@inheritdoc}
|
Chris@0
|
73 */
|
Chris@0
|
74 public function getQuestion() {
|
Chris@0
|
75 return $this->formatPlural(count($this->nodeInfo), 'Are you sure you want to delete this item?', 'Are you sure you want to delete these items?');
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 /**
|
Chris@0
|
79 * {@inheritdoc}
|
Chris@0
|
80 */
|
Chris@0
|
81 public function getCancelUrl() {
|
Chris@0
|
82 return new Url('system.admin_content');
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 /**
|
Chris@0
|
86 * {@inheritdoc}
|
Chris@0
|
87 */
|
Chris@0
|
88 public function getConfirmText() {
|
Chris@0
|
89 return t('Delete');
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * {@inheritdoc}
|
Chris@0
|
94 */
|
Chris@0
|
95 public function buildForm(array $form, FormStateInterface $form_state) {
|
Chris@0
|
96 $this->nodeInfo = $this->tempStoreFactory->get('node_multiple_delete_confirm')->get(\Drupal::currentUser()->id());
|
Chris@0
|
97 if (empty($this->nodeInfo)) {
|
Chris@0
|
98 return new RedirectResponse($this->getCancelUrl()->setAbsolute()->toString());
|
Chris@0
|
99 }
|
Chris@0
|
100 /** @var \Drupal\node\NodeInterface[] $nodes */
|
Chris@0
|
101 $nodes = $this->storage->loadMultiple(array_keys($this->nodeInfo));
|
Chris@0
|
102
|
Chris@0
|
103 $items = [];
|
Chris@0
|
104 foreach ($this->nodeInfo as $id => $langcodes) {
|
Chris@0
|
105 foreach ($langcodes as $langcode) {
|
Chris@0
|
106 $node = $nodes[$id]->getTranslation($langcode);
|
Chris@0
|
107 $key = $id . ':' . $langcode;
|
Chris@0
|
108 $default_key = $id . ':' . $node->getUntranslated()->language()->getId();
|
Chris@0
|
109
|
Chris@0
|
110 // If we have a translated entity we build a nested list of translations
|
Chris@0
|
111 // that will be deleted.
|
Chris@0
|
112 $languages = $node->getTranslationLanguages();
|
Chris@0
|
113 if (count($languages) > 1 && $node->isDefaultTranslation()) {
|
Chris@0
|
114 $names = [];
|
Chris@0
|
115 foreach ($languages as $translation_langcode => $language) {
|
Chris@0
|
116 $names[] = $language->getName();
|
Chris@0
|
117 unset($items[$id . ':' . $translation_langcode]);
|
Chris@0
|
118 }
|
Chris@0
|
119 $items[$default_key] = [
|
Chris@0
|
120 'label' => [
|
Chris@0
|
121 '#markup' => $this->t('@label (Original translation) - <em>The following content translations will be deleted:</em>', ['@label' => $node->label()]),
|
Chris@0
|
122 ],
|
Chris@0
|
123 'deleted_translations' => [
|
Chris@0
|
124 '#theme' => 'item_list',
|
Chris@0
|
125 '#items' => $names,
|
Chris@0
|
126 ],
|
Chris@0
|
127 ];
|
Chris@0
|
128 }
|
Chris@0
|
129 elseif (!isset($items[$default_key])) {
|
Chris@0
|
130 $items[$key] = $node->label();
|
Chris@0
|
131 }
|
Chris@0
|
132 }
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 $form['nodes'] = [
|
Chris@0
|
136 '#theme' => 'item_list',
|
Chris@0
|
137 '#items' => $items,
|
Chris@0
|
138 ];
|
Chris@0
|
139 $form = parent::buildForm($form, $form_state);
|
Chris@0
|
140
|
Chris@0
|
141 return $form;
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 /**
|
Chris@0
|
145 * {@inheritdoc}
|
Chris@0
|
146 */
|
Chris@0
|
147 public function submitForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
148 if ($form_state->getValue('confirm') && !empty($this->nodeInfo)) {
|
Chris@0
|
149 $total_count = 0;
|
Chris@0
|
150 $delete_nodes = [];
|
Chris@0
|
151 /** @var \Drupal\Core\Entity\ContentEntityInterface[][] $delete_translations */
|
Chris@0
|
152 $delete_translations = [];
|
Chris@0
|
153 /** @var \Drupal\node\NodeInterface[] $nodes */
|
Chris@0
|
154 $nodes = $this->storage->loadMultiple(array_keys($this->nodeInfo));
|
Chris@0
|
155
|
Chris@0
|
156 foreach ($this->nodeInfo as $id => $langcodes) {
|
Chris@0
|
157 foreach ($langcodes as $langcode) {
|
Chris@0
|
158 $node = $nodes[$id]->getTranslation($langcode);
|
Chris@0
|
159 if ($node->isDefaultTranslation()) {
|
Chris@0
|
160 $delete_nodes[$id] = $node;
|
Chris@0
|
161 unset($delete_translations[$id]);
|
Chris@0
|
162 $total_count += count($node->getTranslationLanguages());
|
Chris@0
|
163 }
|
Chris@0
|
164 elseif (!isset($delete_nodes[$id])) {
|
Chris@0
|
165 $delete_translations[$id][] = $node;
|
Chris@0
|
166 }
|
Chris@0
|
167 }
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 if ($delete_nodes) {
|
Chris@0
|
171 $this->storage->delete($delete_nodes);
|
Chris@0
|
172 $this->logger('content')->notice('Deleted @count posts.', ['@count' => count($delete_nodes)]);
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 if ($delete_translations) {
|
Chris@0
|
176 $count = 0;
|
Chris@0
|
177 foreach ($delete_translations as $id => $translations) {
|
Chris@0
|
178 $node = $nodes[$id]->getUntranslated();
|
Chris@0
|
179 foreach ($translations as $translation) {
|
Chris@0
|
180 $node->removeTranslation($translation->language()->getId());
|
Chris@0
|
181 }
|
Chris@0
|
182 $node->save();
|
Chris@0
|
183 $count += count($translations);
|
Chris@0
|
184 }
|
Chris@0
|
185 if ($count) {
|
Chris@0
|
186 $total_count += $count;
|
Chris@0
|
187 $this->logger('content')->notice('Deleted @count content translations.', ['@count' => $count]);
|
Chris@0
|
188 }
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 if ($total_count) {
|
Chris@0
|
192 drupal_set_message($this->formatPlural($total_count, 'Deleted 1 post.', 'Deleted @count posts.'));
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 $this->tempStoreFactory->get('node_multiple_delete_confirm')->delete(\Drupal::currentUser()->id());
|
Chris@0
|
196 }
|
Chris@0
|
197
|
Chris@0
|
198 $form_state->setRedirect('system.admin_content');
|
Chris@0
|
199 }
|
Chris@0
|
200
|
Chris@0
|
201 }
|