Chris@0: tempStoreFactory = $temp_store_factory; Chris@0: $this->storage = $manager->getStorage('node'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@14: $container->get('tempstore.private'), Chris@0: $container->get('entity.manager') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getFormId() { Chris@0: return 'node_multiple_delete_confirm'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getQuestion() { Chris@0: 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: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getCancelUrl() { Chris@0: return new Url('system.admin_content'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getConfirmText() { Chris@0: return t('Delete'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function buildForm(array $form, FormStateInterface $form_state) { Chris@0: $this->nodeInfo = $this->tempStoreFactory->get('node_multiple_delete_confirm')->get(\Drupal::currentUser()->id()); Chris@0: if (empty($this->nodeInfo)) { Chris@0: return new RedirectResponse($this->getCancelUrl()->setAbsolute()->toString()); Chris@0: } Chris@0: /** @var \Drupal\node\NodeInterface[] $nodes */ Chris@0: $nodes = $this->storage->loadMultiple(array_keys($this->nodeInfo)); Chris@0: Chris@0: $items = []; Chris@0: foreach ($this->nodeInfo as $id => $langcodes) { Chris@0: foreach ($langcodes as $langcode) { Chris@0: $node = $nodes[$id]->getTranslation($langcode); Chris@0: $key = $id . ':' . $langcode; Chris@0: $default_key = $id . ':' . $node->getUntranslated()->language()->getId(); Chris@0: Chris@0: // If we have a translated entity we build a nested list of translations Chris@0: // that will be deleted. Chris@0: $languages = $node->getTranslationLanguages(); Chris@0: if (count($languages) > 1 && $node->isDefaultTranslation()) { Chris@0: $names = []; Chris@0: foreach ($languages as $translation_langcode => $language) { Chris@0: $names[] = $language->getName(); Chris@0: unset($items[$id . ':' . $translation_langcode]); Chris@0: } Chris@0: $items[$default_key] = [ Chris@0: 'label' => [ Chris@0: '#markup' => $this->t('@label (Original translation) - The following content translations will be deleted:', ['@label' => $node->label()]), Chris@0: ], Chris@0: 'deleted_translations' => [ Chris@0: '#theme' => 'item_list', Chris@0: '#items' => $names, Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: elseif (!isset($items[$default_key])) { Chris@0: $items[$key] = $node->label(); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: $form['nodes'] = [ Chris@0: '#theme' => 'item_list', Chris@0: '#items' => $items, Chris@0: ]; Chris@0: $form = parent::buildForm($form, $form_state); Chris@0: Chris@0: return $form; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function submitForm(array &$form, FormStateInterface $form_state) { Chris@0: if ($form_state->getValue('confirm') && !empty($this->nodeInfo)) { Chris@0: $total_count = 0; Chris@0: $delete_nodes = []; Chris@0: /** @var \Drupal\Core\Entity\ContentEntityInterface[][] $delete_translations */ Chris@0: $delete_translations = []; Chris@0: /** @var \Drupal\node\NodeInterface[] $nodes */ Chris@0: $nodes = $this->storage->loadMultiple(array_keys($this->nodeInfo)); Chris@0: Chris@0: foreach ($this->nodeInfo as $id => $langcodes) { Chris@0: foreach ($langcodes as $langcode) { Chris@0: $node = $nodes[$id]->getTranslation($langcode); Chris@0: if ($node->isDefaultTranslation()) { Chris@0: $delete_nodes[$id] = $node; Chris@0: unset($delete_translations[$id]); Chris@0: $total_count += count($node->getTranslationLanguages()); Chris@0: } Chris@0: elseif (!isset($delete_nodes[$id])) { Chris@0: $delete_translations[$id][] = $node; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if ($delete_nodes) { Chris@0: $this->storage->delete($delete_nodes); Chris@0: $this->logger('content')->notice('Deleted @count posts.', ['@count' => count($delete_nodes)]); Chris@0: } Chris@0: Chris@0: if ($delete_translations) { Chris@0: $count = 0; Chris@0: foreach ($delete_translations as $id => $translations) { Chris@0: $node = $nodes[$id]->getUntranslated(); Chris@0: foreach ($translations as $translation) { Chris@0: $node->removeTranslation($translation->language()->getId()); Chris@0: } Chris@0: $node->save(); Chris@0: $count += count($translations); Chris@0: } Chris@0: if ($count) { Chris@0: $total_count += $count; Chris@0: $this->logger('content')->notice('Deleted @count content translations.', ['@count' => $count]); Chris@0: } Chris@0: } Chris@0: Chris@0: if ($total_count) { Chris@0: drupal_set_message($this->formatPlural($total_count, 'Deleted 1 post.', 'Deleted @count posts.')); Chris@0: } Chris@0: Chris@0: $this->tempStoreFactory->get('node_multiple_delete_confirm')->delete(\Drupal::currentUser()->id()); Chris@0: } Chris@0: Chris@0: $form_state->setRedirect('system.admin_content'); Chris@0: } Chris@0: Chris@0: }