|
|
|
@ -27,6 +27,8 @@ use Drupal\file\FileInterface;
|
|
|
|
|
use Drupal\taxonomy\TermInterface; |
|
|
|
|
use Drupal\Core\Routing\RouteMatchInterface; |
|
|
|
|
use Drupal\serialization\Normalizer\CacheableNormalizerInterface; |
|
|
|
|
use Drupal\Core\Entity\EntityForm; |
|
|
|
|
use Drupal\file\Entity\File; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Implements hook_help(). |
|
|
|
@ -331,6 +333,197 @@ function islandora_form_alter(&$form, FormStateInterface $form_state, $form_id)
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$form_object = $form_state->getFormObject(); |
|
|
|
|
$utils = \Drupal::service('islandora.utils'); |
|
|
|
|
$config = \Drupal::config('islandora.settings')->get('delete_media_and_files'); |
|
|
|
|
|
|
|
|
|
if ($config == 1 && $form_object instanceof EntityForm) { |
|
|
|
|
$entity = $form_object->getEntity(); |
|
|
|
|
|
|
|
|
|
if ($entity->getEntityTypeId() == "node" && $utils->isIslandoraType($entity->getEntityTypeId(), $entity->bundle()) && strpos($form['#form_id'], 'delete_form') !== FALSE) { |
|
|
|
|
$medias = $utils->getMedia($form_state->getFormObject()->getEntity()); |
|
|
|
|
if (count($medias) != 0) { |
|
|
|
|
$form['delete_associated_content'] = [ |
|
|
|
|
'#type' => 'checkbox', |
|
|
|
|
'#title' => t('Delete all associated medias and nodes'), |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
$media_list = []; |
|
|
|
|
|
|
|
|
|
foreach ($medias as $media) { |
|
|
|
|
$media_list[] = $media->getName(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$form['container'] = [ |
|
|
|
|
'#type' => 'container', |
|
|
|
|
'#states' => [ |
|
|
|
|
'visible' => [ |
|
|
|
|
':input[name="delete_associated_content"]' => ['checked' => TRUE], |
|
|
|
|
], |
|
|
|
|
], |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
$form['container']['media_items'] = [ |
|
|
|
|
'#theme' => 'item_list', |
|
|
|
|
'#type' => 'ul', |
|
|
|
|
'#items' => $media_list, |
|
|
|
|
'#attributes' => ['class' => ['islandora-media-items']], |
|
|
|
|
'#wrapper_attributes' => ['class' => ['container']], |
|
|
|
|
'#attached' => [ |
|
|
|
|
'library' => [ |
|
|
|
|
'islandora/islandora', |
|
|
|
|
], |
|
|
|
|
], |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
$form['actions']['submit']['#submit'][] = 'islandora_object_delete_form_submit'; |
|
|
|
|
return $form; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return $form; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Implements a submit handler for the delete form. |
|
|
|
|
*/ |
|
|
|
|
function islandora_object_delete_form_submit($form, FormStateInterface $form_state) { |
|
|
|
|
|
|
|
|
|
$result = $form_state->getValues('delete_associated_content'); |
|
|
|
|
$utils = \Drupal::service('islandora.utils'); |
|
|
|
|
|
|
|
|
|
if ($result['delete_associated_content'] == 1) { |
|
|
|
|
|
|
|
|
|
$node = $form_state->getFormObject()->getEntity(); |
|
|
|
|
$medias = $utils->getMedia($node); |
|
|
|
|
$media_list = []; |
|
|
|
|
|
|
|
|
|
$entity_field_manager = \Drupal::service('entity_field.manager'); |
|
|
|
|
$current_user = \Drupal::currentUser(); |
|
|
|
|
$logger = \Drupal::logger('logger.channel.islandora'); |
|
|
|
|
$messenger = \Drupal::messenger(); |
|
|
|
|
|
|
|
|
|
$delete_media = []; |
|
|
|
|
$media_translations = []; |
|
|
|
|
$media_files = []; |
|
|
|
|
$entity_protected_medias = []; |
|
|
|
|
$inaccessible_entities = []; |
|
|
|
|
|
|
|
|
|
foreach ($medias as $id => $media) { |
|
|
|
|
$lang = $media->language()->getId(); |
|
|
|
|
$selected_langcodes[$lang] = $lang; |
|
|
|
|
|
|
|
|
|
if (!$media->access('delete', $current_user)) { |
|
|
|
|
$inaccessible_entities[] = $media; |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
// Check for files. |
|
|
|
|
$fields = $entity_field_manager->getFieldDefinitions('media', $media->bundle()); |
|
|
|
|
foreach ($fields as $field) { |
|
|
|
|
$type = $field->getType(); |
|
|
|
|
if ($type == 'file' || $type == 'image') { |
|
|
|
|
$target_id = $media->get($field->getName())->target_id; |
|
|
|
|
$file = File::load($target_id); |
|
|
|
|
if ($file) { |
|
|
|
|
if (!$file->access('delete', $current_user)) { |
|
|
|
|
$inaccessible_entities[] = $file; |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
$media_files[$id][$file->id()] = $file; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
foreach ($selected_langcodes as $langcode) { |
|
|
|
|
// We're only working with media, which are translatable. |
|
|
|
|
$entity = $media->getTranslation($langcode); |
|
|
|
|
if ($entity->isDefaultTranslation()) { |
|
|
|
|
$delete_media[$id] = $entity; |
|
|
|
|
unset($media_translations[$id]); |
|
|
|
|
} |
|
|
|
|
elseif (!isset($delete_media[$id])) { |
|
|
|
|
$media_translations[$id][] = $entity; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ($delete_media) { |
|
|
|
|
foreach ($delete_media as $id => $media) { |
|
|
|
|
try { |
|
|
|
|
$media->delete(); |
|
|
|
|
$media_list[] = $id; |
|
|
|
|
$logger->notice('The media %label has been deleted.', [ |
|
|
|
|
'%label' => $media->label(), |
|
|
|
|
]); |
|
|
|
|
} |
|
|
|
|
catch (Exception $e) { |
|
|
|
|
$entity_protected_medias[] = $id; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$delete_files = array_filter($media_files, function ($media) use ($entity_protected_medias) { |
|
|
|
|
return !in_array($media, $entity_protected_medias); |
|
|
|
|
}, ARRAY_FILTER_USE_KEY); |
|
|
|
|
|
|
|
|
|
if ($delete_files) { |
|
|
|
|
foreach ($delete_files as $files_array) { |
|
|
|
|
foreach ($files_array as $file) { |
|
|
|
|
$file->delete(); |
|
|
|
|
$logger->notice('The file %label has been deleted.', [ |
|
|
|
|
'%label' => $file->label(), |
|
|
|
|
]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$delete_media_translations = array_filter($media_translations, function ($media) use ($entity_protected_medias) { |
|
|
|
|
return !in_array($media, $entity_protected_medias); |
|
|
|
|
}, ARRAY_FILTER_USE_KEY); |
|
|
|
|
|
|
|
|
|
if ($delete_media_translations) { |
|
|
|
|
foreach ($delete_media_translations as $id => $translations) { |
|
|
|
|
$media = $medias[$id]; |
|
|
|
|
foreach ($translations as $translation) { |
|
|
|
|
$media->removeTranslation($translation->language()->getId()); |
|
|
|
|
} |
|
|
|
|
$media->save(); |
|
|
|
|
foreach ($translations as $translation) { |
|
|
|
|
$logger->notice('The media %label @language translation has been deleted', [ |
|
|
|
|
'%label' => $media->label(), |
|
|
|
|
'@language' => $translation->language()->getName(), |
|
|
|
|
]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ($inaccessible_entities) { |
|
|
|
|
$messenger->addWarning("@count items have not been deleted because you do not have the necessary permissions.", [ |
|
|
|
|
'@count' => count($inaccessible_entities), |
|
|
|
|
]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$build = [ |
|
|
|
|
'heading' => [ |
|
|
|
|
'#type' => 'html_tag', |
|
|
|
|
'#tag' => 'div', |
|
|
|
|
'#value' => t("The repository item @node and @media", [ |
|
|
|
|
'@node' => $node->getTitle(), |
|
|
|
|
'@media' => \Drupal::translation()->formatPlural( |
|
|
|
|
count($media_list), 'the media with the id @media has been deleted.', |
|
|
|
|
'the medias with the ids @media have been deleted.', |
|
|
|
|
['@media' => implode(", ", $media_list)], |
|
|
|
|
), |
|
|
|
|
]), |
|
|
|
|
], |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
$message = \Drupal::service('renderer')->renderPlain($build); |
|
|
|
|
$messenger->deleteByType('status'); |
|
|
|
|
$messenger->addStatus($message); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|