Browse Source

skip entity types protected by entity integrity reference and updated test cases for toggle feature

pull/905/head
shriram1056 2 years ago
parent
commit
48b5333b2d
  1. 1
      config/install/islandora.settings.yml
  2. 3
      config/schema/islandora.schema.yml
  3. 38
      islandora.module
  4. 5
      tests/src/Functional/DeleteNodeWithMediaAndFile.php

1
config/install/islandora.settings.yml

@ -1,4 +1,5 @@
broker_url: 'tcp://localhost:61613' broker_url: 'tcp://localhost:61613'
jwt_expiry: '+2 hour' jwt_expiry: '+2 hour'
gemini_url: '' gemini_url: ''
delete_media_and_files: TRUE
gemini_pseudo_bundles: [] gemini_pseudo_bundles: []

3
config/schema/islandora.schema.yml

@ -14,6 +14,9 @@ islandora.settings:
jwt_expiry: jwt_expiry:
type: string type: string
label: 'How long JWTs should last before expiring.' label: 'How long JWTs should last before expiring.'
delete_media_and_files:
type: boolean
label: 'Node Delete with Media and Files'
upload_form_location: upload_form_location:
type: string type: string
label: 'Upload Form Location' label: 'Upload Form Location'

38
islandora.module

@ -373,6 +373,7 @@ function islandora_form_alter(&$form, FormStateInterface $form_state, $form_id)
} }
} }
} }
return $form; return $form;
} }
@ -390,7 +391,6 @@ function islandora_object_delete_form_submit($form, &$form_state) {
$medias = $utils->getMedia($node); $medias = $utils->getMedia($node);
$media_list = ""; $media_list = "";
$entity_type_manager = \Drupal::entityTypeManager();
$entity_field_manager = \Drupal::service('entity_field.manager'); $entity_field_manager = \Drupal::service('entity_field.manager');
$current_user = \Drupal::currentUser(); $current_user = \Drupal::currentUser();
$logger = \Drupal::logger('logger.channel.islandora'); $logger = \Drupal::logger('logger.channel.islandora');
@ -398,14 +398,12 @@ function islandora_object_delete_form_submit($form, &$form_state) {
$total_count = 0; $total_count = 0;
$delete_media = []; $delete_media = [];
$delete_media_translations = []; $media_translations = [];
$delete_files = []; $media_files = [];
$entity_protected_medias = [];
$inaccessible_entities = []; $inaccessible_entities = [];
$media_storage = $entity_type_manager->getStorage('media');
$file_storage = $entity_type_manager->getStorage('file');
foreach ($medias as $id => $media) { foreach ($medias as $id => $media) {
$media_list .= $id . ", ";
$lang = $media->language()->getId(); $lang = $media->language()->getId();
$selected_langcodes[$lang] = $lang; $selected_langcodes[$lang] = $lang;
@ -425,7 +423,7 @@ function islandora_object_delete_form_submit($form, &$form_state) {
$inaccessible_entities[] = $file; $inaccessible_entities[] = $file;
continue; continue;
} }
$delete_files[$file->id()] = $file; $media_files[$id][$file->id()] = $file;
$total_count++; $total_count++;
} }
} }
@ -436,32 +434,48 @@ function islandora_object_delete_form_submit($form, &$form_state) {
$entity = $media->getTranslation($langcode); $entity = $media->getTranslation($langcode);
if ($entity->isDefaultTranslation()) { if ($entity->isDefaultTranslation()) {
$delete_media[$id] = $entity; $delete_media[$id] = $entity;
unset($delete_media_translations[$id]); unset($media_translations[$id]);
$total_count += count($entity->getTranslationLanguages()); $total_count += count($entity->getTranslationLanguages());
} }
elseif (!isset($delete_media[$id])) { elseif (!isset($delete_media[$id])) {
$delete_media_translations[$id][] = $entity; $media_translations[$id][] = $entity;
} }
} }
} }
if ($delete_media) { if ($delete_media) {
$media_storage->delete($delete_media);
foreach ($delete_media as $media) { foreach ($delete_media as $media) {
try {
$media->delete();
$logger->notice('The media %label has been deleted.', [ $logger->notice('The media %label has been deleted.', [
'%label' => $media->label(), '%label' => $media->label(),
]); ]);
$media_list .= $id . ", ";
}
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) { if ($delete_files) {
$file_storage->delete($delete_files); foreach ($delete_files as $files_array) {
foreach ($delete_files as $file) { foreach ($files_array as $file) {
$file->delete();
$logger->notice('The file %label has been deleted.', [ $logger->notice('The file %label has been deleted.', [
'%label' => $file->label(), '%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) { if ($delete_media_translations) {
foreach ($delete_media_translations as $id => $translations) { foreach ($delete_media_translations as $id => $translations) {

5
tests/src/Functional/DeleteNodeWithMediaAndFile.php

@ -19,6 +19,7 @@ class DeleteNodeWithMediaAndFile extends IslandoraFunctionalTestBase {
'view media', 'view media',
'bypass node access', 'bypass node access',
'access files overview', 'access files overview',
'administer site configuration',
]); ]);
$this->drupalLogin($account); $this->drupalLogin($account);
@ -75,6 +76,10 @@ class DeleteNodeWithMediaAndFile extends IslandoraFunctionalTestBase {
]); ]);
$media2->save(); $media2->save();
$this->drupalGet("admin/config/islandora/core");
$assert_session->pageTextContains('Node Delete with Media and Files');
\Drupal::configFactory()->getEditable('islandora.settings')->set('delete_media_and_files', TRUE)->save();
$delete = ['delete_associated_content' => TRUE]; $delete = ['delete_associated_content' => TRUE];
$this->drupalGet("node/1/delete"); $this->drupalGet("node/1/delete");

Loading…
Cancel
Save