3 namespace Drupal\node\Form;
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\user\PrivateTempStoreFactory;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Symfony\Component\HttpFoundation\RedirectResponse;
14 * Provides a node deletion confirmation form.
16 class DeleteMultiple extends ConfirmFormBase {
19 * The array of nodes to delete.
23 protected $nodeInfo = [];
26 * The tempstore factory.
28 * @var \Drupal\user\PrivateTempStoreFactory
30 protected $tempStoreFactory;
35 * @var \Drupal\Core\Entity\EntityStorageInterface
40 * Constructs a DeleteMultiple form object.
42 * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
43 * The tempstore factory.
44 * @param \Drupal\Core\Entity\EntityManagerInterface $manager
47 public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityManagerInterface $manager) {
48 $this->tempStoreFactory = $temp_store_factory;
49 $this->storage = $manager->getStorage('node');
55 public static function create(ContainerInterface $container) {
57 $container->get('user.private_tempstore'),
58 $container->get('entity.manager')
65 public function getFormId() {
66 return 'node_multiple_delete_confirm';
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?');
79 public function getCancelUrl() {
80 return new Url('system.admin_content');
86 public function getConfirmText() {
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());
98 /** @var \Drupal\node\NodeInterface[] $nodes */
99 $nodes = $this->storage->loadMultiple(array_keys($this->nodeInfo));
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();
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()) {
113 foreach ($languages as $translation_langcode => $language) {
114 $names[] = $language->getName();
115 unset($items[$id . ':' . $translation_langcode]);
117 $items[$default_key] = [
119 '#markup' => $this->t('@label (Original translation) - <em>The following content translations will be deleted:</em>', ['@label' => $node->label()]),
121 'deleted_translations' => [
122 '#theme' => 'item_list',
127 elseif (!isset($items[$default_key])) {
128 $items[$key] = $node->label();
134 '#theme' => 'item_list',
137 $form = parent::buildForm($form, $form_state);
145 public function submitForm(array &$form, FormStateInterface $form_state) {
146 if ($form_state->getValue('confirm') && !empty($this->nodeInfo)) {
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));
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());
162 elseif (!isset($delete_nodes[$id])) {
163 $delete_translations[$id][] = $node;
169 $this->storage->delete($delete_nodes);
170 $this->logger('content')->notice('Deleted @count posts.', ['@count' => count($delete_nodes)]);
173 if ($delete_translations) {
175 foreach ($delete_translations as $id => $translations) {
176 $node = $nodes[$id]->getUntranslated();
177 foreach ($translations as $translation) {
178 $node->removeTranslation($translation->language()->getId());
181 $count += count($translations);
184 $total_count += $count;
185 $this->logger('content')->notice('Deleted @count content translations.', ['@count' => $count]);
190 drupal_set_message($this->formatPlural($total_count, 'Deleted 1 post.', 'Deleted @count posts.'));
193 $this->tempStoreFactory->get('node_multiple_delete_confirm')->delete(\Drupal::currentUser()->id());
196 $form_state->setRedirect('system.admin_content');