From 5a71339fc3bbbfbbe680488feff0b14e28561f5e Mon Sep 17 00:00:00 2001
From: shriram <saravananshriram80@gmail.com>
Date: Tue, 18 Oct 2022 16:10:19 -0300
Subject: [PATCH] delete media associated with an islandora object

---
 islandora.module | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/islandora.module b/islandora.module
index 9814820e..700c4deb 100644
--- a/islandora.module
+++ b/islandora.module
@@ -320,9 +320,10 @@ function islandora_form_alter(&$form, FormStateInterface $form_state, $form_id)
     'media_extracted_text_add_form', 'media_file_add_form', 'media_image_add_form',
     'media_fits_technical_metadata_add_form', 'media_video_add_form',
   ];
-
+  //kint($form);
   if (in_array($form['#form_id'], $media_add_forms)) {
     $params = \Drupal::request()->query->all();
+
     if (isset($params['edit'])) {
       $media_of_nid = $params['edit']['field_media_of']['widget'][0]['target_id'];
       $node = \Drupal::entityTypeManager()->getStorage('node')->load($media_of_nid);
@@ -346,6 +347,7 @@ function islandora_field_widget_image_image_form_alter(&$element, $form_state, $
 function islandora_add_default_image_alt_text($element, $form_state, $form) {
   if ($element['alt']['#access']) {
     $params = \Drupal::request()->query->all();
+
     if (isset($params['edit'])) {
       $media_of_nid = $params['edit']['field_media_of']['widget'][0]['target_id'];
       $node = \Drupal::entityTypeManager()->getStorage('node')->load($media_of_nid);
@@ -423,7 +425,7 @@ function islandora_entity_extra_field_info() {
   if (!empty($pseudo_bundles)) {
     foreach ($pseudo_bundles as $key) {
       [$bundle, $content_entity] = explode(":", $key);
-      $extra_field[$content_entity][$bundle]['display'][IslandoraSettingsForm::GEMINI_PSEUDO_FIELD] = [
+      $extra_field[$content_entity][$bundle]['display']['field_gemini_uri'] = [
         'label' => t('Fedora URI'),
         'description' => t('The URI to the persistent'),
         'weight' => 100,
@@ -548,3 +550,27 @@ function islandora_preprocess_views_view_table(&$variables) {
     }
   }
 }
+
+function islandora_form_node_islandora_object_delete_form_alter(&$form, &$form_state){
+  $form['delete_associated_content'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Delete all associated medias and nodes'),
+  );
+
+  $form['actions']['submit']['#submit'][] = 'islandora_form_node_islandora_object_delete_form_submit';
+  return $form;
+}
+
+function islandora_form_node_islandora_object_delete_form_submit($form, &$form_state){
+
+  $result = $form_state->getValues('delete_associated_content');
+
+  if($result['delete_associated_content'] == 1) {
+    $utils = \Drupal::service('islandora.utils');
+    $medias = $utils->getMedia($form_state->getFormObject()->getEntity());
+    foreach ($medias as $media) {
+      $media->delete();
+    }
+  }
+}
+