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