3 namespace Drupal\entity\Form;
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Form\BaseFormIdInterface;
7 use Drupal\Core\Form\ConfirmFormBase;
8 use Drupal\Core\Messenger\MessengerInterface;
9 use Drupal\Core\TypedData\TranslatableInterface;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\Core\Session\AccountInterface;
13 use Drupal\Core\TempStore\PrivateTempStoreFactory;
14 use Symfony\Component\HttpFoundation\RedirectResponse;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
18 * Provides an entities deletion confirmation form.
20 class DeleteMultipleForm extends ConfirmFormBase implements BaseFormIdInterface {
25 * @var \Drupal\Core\Session\AccountInterface
27 protected $currentUser;
30 * The entity type manager.
32 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
34 protected $entityTypeManager;
39 * @var \Drupal\Core\TempStore\SharedTempStore
44 * The messenger service.
46 * @var \Drupal\Core\Messenger\MessengerInterface
55 protected $entityTypeId;
58 * The selection, in the entity_id => langcodes format.
62 protected $selection = [];
65 * The entity type definition.
67 * @var \Drupal\Core\Entity\EntityTypeInterface
69 protected $entityType;
72 * Constructs a new DeleteMultiple object.
74 * @param \Drupal\Core\Session\AccountInterface $current_user
76 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
77 * The entity type manager.
78 * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
79 * The tempstore factory.
80 * @param \Drupal\Core\Messenger\MessengerInterface $messenger
81 * The messenger service.
83 public function __construct(AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, PrivateTempStoreFactory $temp_store_factory, MessengerInterface $messenger) {
84 $this->currentUser = $current_user;
85 $this->entityTypeManager = $entity_type_manager;
86 $this->tempStore = $temp_store_factory->get('entity_delete_multiple_confirm');
87 $this->messenger = $messenger;
93 public static function create(ContainerInterface $container) {
95 $container->get('current_user'),
96 $container->get('entity_type.manager'),
97 $container->get('tempstore.private'),
98 $container->get('messenger')
105 public function getBaseFormId() {
106 return 'entity_delete_multiple_confirm_form';
112 public function getFormId() {
113 // Get entity type ID from the route because ::buildForm has not yet been
115 $entity_type_id = $this->getRouteMatch()->getParameter('entity_type_id');
116 return $entity_type_id . '_delete_multiple_confirm_form';
122 public function getQuestion() {
123 return $this->formatPlural(count($this->selection), 'Are you sure you want to delete this @item?', 'Are you sure you want to delete these @items?', [
124 '@item' => $this->entityType->getSingularLabel(),
125 '@items' => $this->entityType->getPluralLabel(),
132 public function getCancelUrl() {
133 if ($this->entityType->hasLinkTemplate('collection')) {
134 return new Url('entity.' . $this->entityTypeId . '.collection');
137 return new Url('<front>');
144 public function getConfirmText() {
145 return $this->t('Delete');
151 public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) {
152 $this->entityTypeId = $entity_type_id;
153 $this->entityType = $this->entityTypeManager->getDefinition($this->entityTypeId);
154 $this->selection = $this->tempStore->get($this->currentUser->id() . ':' . $entity_type_id);
155 if (empty($this->entityTypeId) || empty($this->selection)) {
156 return new RedirectResponse($this->getCancelUrl()
162 $entities = $this->entityTypeManager->getStorage($entity_type_id)->loadMultiple(array_keys($this->selection));
163 foreach ($this->selection as $id => $selected_langcodes) {
164 $entity = $entities[$id];
165 foreach ($selected_langcodes as $langcode) {
166 $key = $id . ':' . $langcode;
167 if ($entity instanceof TranslatableInterface) {
168 $entity = $entity->getTranslation($langcode);
169 $default_key = $id . ':' . $entity->getUntranslated()->language()->getId();
171 // Build a nested list of translations that will be deleted if the
172 // entity has multiple translations.
173 $entity_languages = $entity->getTranslationLanguages();
174 if (count($entity_languages) > 1 && $entity->isDefaultTranslation()) {
176 foreach ($entity_languages as $translation_langcode => $language) {
177 $names[] = $language->getName();
178 unset($items[$id . ':' . $translation_langcode]);
180 $items[$default_key] = [
182 '#markup' => $this->t('@label (Original translation) - <em>The following @entity_type translations will be deleted:</em>',
184 '@label' => $entity->label(),
185 '@entity_type' => $this->entityType->getSingularLabel(),
188 'deleted_translations' => [
189 '#theme' => 'item_list',
194 elseif (!isset($items[$default_key])) {
195 $items[$key] = $entity->label();
198 elseif (!isset($items[$key])) {
199 $items[$key] = $entity->label();
204 $form['entities'] = [
205 '#theme' => 'item_list',
208 $form = parent::buildForm($form, $form_state);
216 public function submitForm(array &$form, FormStateInterface $form_state) {
218 $delete_entities = [];
219 $delete_translations = [];
220 $inaccessible_entities = [];
221 $storage = $this->entityTypeManager->getStorage($this->entityTypeId);
223 $entities = $storage->loadMultiple(array_keys($this->selection));
224 foreach ($this->selection as $id => $selected_langcodes) {
225 $entity = $entities[$id];
226 if (!$entity->access('delete', $this->currentUser)) {
227 $inaccessible_entities[] = $entity;
230 foreach ($selected_langcodes as $langcode) {
231 if ($entity instanceof TranslatableInterface) {
232 $entity = $entity->getTranslation($langcode);
233 // If the entity is the default translation then deleting it will
234 // delete all the translations.
235 if ($entity->isDefaultTranslation()) {
236 $delete_entities[$id] = $entity;
237 // If there are translations already marked for deletion then remove
238 // them as they will be deleted anyway.
239 unset($delete_translations[$id]);
240 // Update the total count. Since a single delete will delete all
241 // translations, we need to add the number of translations to the
243 $total_count += count($entity->getTranslationLanguages());
245 // Add the translation to the list of translations to be deleted
246 // unless the default translation is being deleted.
247 elseif (!isset($delete_entities[$id])) {
248 $delete_translations[$id][] = $entity;
251 elseif (!isset($delete_entities[$id])) {
252 $delete_entities[$id] = $entity;
258 if ($delete_entities) {
259 $storage->delete($delete_entities);
260 foreach ($delete_entities as $entity) {
261 $this->logger($entity->getEntityType()->getProvider())->notice('The @entity-type %label has been deleted.', [
262 '@entity-type' => $entity->getEntityType()->getLowercaseLabel(),
263 '%label' => $entity->label(),
268 if ($delete_translations) {
269 /** @var \Drupal\Core\Entity\TranslatableInterface[][] $delete_translations */
270 foreach ($delete_translations as $id => $translations) {
271 $entity = $entities[$id]->getUntranslated();
272 foreach ($translations as $translation) {
273 $entity->removeTranslation($translation->language()->getId());
276 foreach ($translations as $translation) {
277 $this->logger($entity->getEntityType()->getProvider())->notice('The @entity-type %label @language translation has been deleted.', [
278 '@entity-type' => $entity->getEntityType()->getLowercaseLabel(),
279 '%label' => $entity->label(),
280 '@language' => $translation->language()->getName(),
283 $total_count += count($translations);
288 $this->messenger->addStatus($this->getDeletedMessage($total_count));
290 if ($inaccessible_entities) {
291 $this->messenger->addWarning($this->getInaccessibleMessage(count($inaccessible_entities)));
293 $this->tempStore->delete($this->currentUser->id());
294 $form_state->setRedirectUrl($this->getCancelUrl());
298 * Returns the message to show the user after an item was deleted.
301 * Count of deleted translations.
303 * @return \Drupal\Core\StringTranslation\TranslatableMarkup
304 * The item deleted message.
306 protected function getDeletedMessage($count) {
307 return $this->formatPlural($count, 'Deleted @count item.', 'Deleted @count items.');
311 * Returns the message to show the user when an item has not been deleted.
314 * Count of deleted translations.
316 * @return \Drupal\Core\StringTranslation\TranslatableMarkup
317 * The item inaccessible message.
319 protected function getInaccessibleMessage($count) {
320 return $this->formatPlural($count, "@count item has not been deleted because you do not have the necessary permissions.", "@count items have not been deleted because you do not have the necessary permissions.");