Skip to content

Commit

Permalink
skip entity types protected by entity integrity reference and updated…
Browse files Browse the repository at this point in the history
… test cases for toggle feature
  • Loading branch information
shriram1056 committed Nov 25, 2022
1 parent 5c24c19 commit 48b5333
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
1 change: 1 addition & 0 deletions config/install/islandora.settings.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
broker_url: 'tcp://localhost:61613'
jwt_expiry: '+2 hour'
gemini_url: ''
delete_media_and_files: TRUE
gemini_pseudo_bundles: []
5 changes: 4 additions & 1 deletion config/schema/islandora.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ islandora.settings:
jwt_expiry:
type: string
label: 'How long JWTs should last before expiring.'
delete_media_and_files:
type: boolean
label: 'Node Delete with Media and Files'
upload_form_location:
type: string
label: 'Upload Form Location'
Expand Down Expand Up @@ -166,7 +169,7 @@ condition.plugin.node_had_namespace:
pid_field:
type: ignore
label: 'PID field'

field.formatter.settings.islandora_image:
type: field.formatter.settings.image
label: 'Islandora image field display format settings'
Expand Down
50 changes: 32 additions & 18 deletions islandora.module
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ function islandora_form_alter(&$form, FormStateInterface $form_state, $form_id)
}
}
}

return $form;
}

Expand All @@ -390,22 +391,19 @@ function islandora_object_delete_form_submit($form, &$form_state) {
$medias = $utils->getMedia($node);
$media_list = "";

$entity_type_manager = \Drupal::entityTypeManager();
$entity_field_manager = \Drupal::service('entity_field.manager');
$current_user = \Drupal::currentUser();
$logger = \Drupal::logger('logger.channel.islandora');
$messenger = \Drupal::messenger();

$total_count = 0;
$delete_media = [];
$delete_media_translations = [];
$delete_files = [];
$media_translations = [];
$media_files = [];
$entity_protected_medias = [];
$inaccessible_entities = [];
$media_storage = $entity_type_manager->getStorage('media');
$file_storage = $entity_type_manager->getStorage('file');

foreach ($medias as $id => $media) {
$media_list .= $id . ", ";
$lang = $media->language()->getId();
$selected_langcodes[$lang] = $lang;

Expand All @@ -425,7 +423,7 @@ function islandora_object_delete_form_submit($form, &$form_state) {
$inaccessible_entities[] = $file;
continue;
}
$delete_files[$file->id()] = $file;
$media_files[$id][$file->id()] = $file;
$total_count++;
}
}
Expand All @@ -436,33 +434,49 @@ function islandora_object_delete_form_submit($form, &$form_state) {
$entity = $media->getTranslation($langcode);
if ($entity->isDefaultTranslation()) {
$delete_media[$id] = $entity;
unset($delete_media_translations[$id]);
unset($media_translations[$id]);
$total_count += count($entity->getTranslationLanguages());
}
elseif (!isset($delete_media[$id])) {
$delete_media_translations[$id][] = $entity;
$media_translations[$id][] = $entity;
}
}
}

if ($delete_media) {
$media_storage->delete($delete_media);
foreach ($delete_media as $media) {
$logger->notice('The media %label has been deleted.', [
'%label' => $media->label(),
]);
try {
$media->delete();
$logger->notice('The media %label has been deleted.', [
'%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) {
$file_storage->delete($delete_files);
foreach ($delete_files as $file) {
$logger->notice('The file %label has been deleted.', [
'%label' => $file->label(),
]);
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];
Expand Down
5 changes: 5 additions & 0 deletions tests/src/Functional/DeleteNodeWithMediaAndFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function testDeleteNodeWithMediaAndFile() {
'view media',
'bypass node access',
'access files overview',
'administer site configuration',
]);
$this->drupalLogin($account);

Expand Down Expand Up @@ -75,6 +76,10 @@ public function testDeleteNodeWithMediaAndFile() {
]);
$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];

$this->drupalGet("node/1/delete");
Expand Down

0 comments on commit 48b5333

Please sign in to comment.