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