public function MediaDeleteMultipleConfirmForm::submitForm in Drupal 8
@todo Change to trait or base class when #2843395 is done.
Overrides FormInterface::submitForm
See also
https://www.drupal.org/node/2843395
File
- core/
modules/ media/ src/ Form/ MediaDeleteMultipleConfirmForm.php, line 160
Class
- MediaDeleteMultipleConfirmForm
- Provides a confirmation form to delete multiple media items at once.
Namespace
Drupal\media\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('confirm') && !empty($this->mediaItems)) {
$total_count = 0;
$delete_entities = [];
/** @var \Drupal\Core\Entity\ContentEntityInterface[][] $delete_translations */
$delete_translations = [];
/** @var \Drupal\media\MediaInterface[] $entities */
$entities = $this->storage
->loadMultiple(array_keys($this->mediaItems));
foreach ($this->mediaItems as $id => $langcodes) {
foreach ($langcodes as $langcode) {
$entity = $entities[$id]
->getTranslation($langcode);
if ($entity
->isDefaultTranslation()) {
$delete_entities[$id] = $entity;
unset($delete_translations[$id]);
$total_count += count($entity
->getTranslationLanguages());
}
elseif (!isset($delete_entities[$id])) {
$delete_translations[$id][] = $entity;
}
}
}
if ($delete_entities) {
$this->storage
->delete($delete_entities);
$this
->logger('media')
->notice('Deleted @count media items.', [
'@count' => count($delete_entities),
]);
}
if ($delete_translations) {
$count = 0;
foreach ($delete_translations as $id => $translations) {
$entity = $entities[$id]
->getUntranslated();
foreach ($translations as $translation) {
$entity
->removeTranslation($translation
->language()
->getId());
}
$entity
->save();
$count += count($translations);
}
if ($count) {
$total_count += $count;
$this
->logger('media')
->notice('Deleted @count media translations.', [
'@count' => $count,
]);
}
}
if ($total_count) {
$this
->messenger()
->addStatus($this
->formatPlural($total_count, 'Deleted 1 media item.', 'Deleted @count media items.'));
}
$this->tempStoreFactory
->get('media_multiple_delete_confirm')
->delete(\Drupal::currentUser()
->id());
}
// @todo Change to media library when #2834729 is done.
// https://www.drupal.org/node/2834729.
$form_state
->setRedirect('system.admin_content');
}