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