3 namespace Drupal\entity\Form;
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Session\AccountInterface;
10 use Drupal\user\PrivateTempStoreFactory;
11 use Symfony\Component\HttpFoundation\RedirectResponse;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Provides an entities deletion confirmation form.
17 class DeleteMultiple extends ConfirmFormBase {
22 * @var \Drupal\Core\Session\AccountInterface
24 protected $currentUser;
27 * The entity type manager.
29 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
31 protected $entityTypeManager;
36 * @var \Drupal\user\SharedTempStore
45 protected $entityTypeId;
48 * The selection, in the entity_id => langcodes format.
52 protected $selection = [];
55 * Constructs a new DeleteMultiple object.
57 * @param \Drupal\Core\Session\AccountInterface $current_user
59 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
60 * The entity type manager.
61 * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
62 * The tempstore factory.
64 public function __construct(AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, PrivateTempStoreFactory $temp_store_factory) {
65 $this->currentUser = $current_user;
66 $this->entityTypeManager = $entity_type_manager;
67 $this->tempStore = $temp_store_factory->get('entity_delete_multiple_confirm');
73 public static function create(ContainerInterface $container) {
75 $container->get('current_user'),
76 $container->get('entity_type.manager'),
77 $container->get('user.private_tempstore')
84 public function getFormId() {
85 return 'entity_delete_multiple_confirm';
91 public function getQuestion() {
92 return $this->formatPlural(count($this->selection), 'Are you sure you want to delete this item?', 'Are you sure you want to delete these items?');
98 public function getCancelUrl() {
99 return new Url('entity.' . $this->entityTypeId . '.collection');
105 public function getConfirmText() {
106 return $this->t('Delete');
112 * @param string $entity_type_id
113 * The entity type id.
115 public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) {
116 $this->entityTypeId = $entity_type_id;
117 $this->selection = $this->tempStore->get($this->currentUser->id());
118 if (empty($this->entityTypeId) || empty($this->selection)) {
119 return new RedirectResponse($this->getCancelUrl()->setAbsolute()->toString());
122 $storage = $this->entityTypeManager->getStorage($this->entityTypeId);
123 /** @var \Drupal\Core\Entity\ContentEntityInterface[] $entities */
124 $entities = $storage->loadMultiple(array_keys($this->selection));
126 foreach ($this->selection as $id => $langcodes) {
127 foreach ($langcodes as $langcode) {
128 $entity = $entities[$id]->getTranslation($langcode);
129 $key = $id . ':' . $langcode;
130 $default_key = $id . ':' . $entity->getUntranslated()->language()->getId();
132 // If we have a translated entity we build a nested list of translations
133 // that will be deleted.
134 $languages = $entity->getTranslationLanguages();
135 if (count($languages) > 1 && $entity->isDefaultTranslation()) {
137 foreach ($languages as $translation_langcode => $language) {
138 $names[] = $language->getName();
139 unset($items[$id . ':' . $translation_langcode]);
141 $items[$default_key] = [
143 '#markup' => $this->t('@label (Original translation) - <em>The following translations will be deleted:</em>', ['@label' => $entity->label()]),
145 'deleted_translations' => [
146 '#theme' => 'item_list',
151 elseif (!isset($items[$default_key])) {
152 $items[$key] = $entity->label();
157 $form['entities'] = [
158 '#theme' => 'item_list',
161 $form = parent::buildForm($form, $form_state);
169 public function submitForm(array &$form, FormStateInterface $form_state) {
171 $delete_entities = [];
172 $delete_translations = [];
173 $storage = $this->entityTypeManager->getStorage($this->entityTypeId);
174 /** @var \Drupal\Core\Entity\ContentEntityInterface[] $entities */
175 $entities = $storage->loadMultiple(array_keys($this->selection));
177 foreach ($this->selection as $id => $langcodes) {
178 foreach ($langcodes as $langcode) {
179 $entity = $entities[$id]->getTranslation($langcode);
180 if ($entity->isDefaultTranslation()) {
181 $delete_entities[$id] = $entity;
182 unset($delete_translations[$id]);
183 $total_count += count($entity->getTranslationLanguages());
185 elseif (!isset($delete_entities[$id])) {
186 $delete_translations[$id][] = $entity;
191 if ($delete_entities) {
192 $storage->delete($delete_entities);
193 $this->logger('content')->notice('Deleted @count @entity_type items.', [
194 '@count' => count($delete_entities),
195 '@entity_type' => $this->entityTypeId,
199 if ($delete_translations) {
201 /** @var \Drupal\Core\Entity\ContentEntityInterface[][] $delete_translations */
202 foreach ($delete_translations as $id => $translations) {
203 $entity = $entities[$id]->getUntranslated();
204 foreach ($translations as $translation) {
205 $entity->removeTranslation($translation->language()->getId());
208 $count += count($translations);
211 $total_count += $count;
212 $this->logger('content')->notice('Deleted @count @entity_type translations.', [
214 '@entity_type' => $this->entityTypeId,
220 drupal_set_message($this->formatPlural($total_count, 'Deleted 1 item.', 'Deleted @count items.'));
222 $this->tempStore->delete($this->currentUser->id());
223 $form_state->setRedirectUrl($this->getCancelUrl());