From 0d9405a1b937ba776d0c02745a0d453fcc25ef51 Mon Sep 17 00:00:00 2001
From: shriram <saravananshriram80@gmail.com>
Date: Thu, 27 Oct 2022 14:31:18 -0300
Subject: [PATCH] list media associated with a Islandora object

---
 islandora.module | 69 +++++++++++++++++++++++++++++++++---------------
 1 file changed, 47 insertions(+), 22 deletions(-)

diff --git a/islandora.module b/islandora.module
index 700c4deb..b6cf61ba 100644
--- a/islandora.module
+++ b/islandora.module
@@ -27,6 +27,7 @@ use Drupal\file\FileInterface;
 use Drupal\taxonomy\TermInterface;
 use Drupal\Core\Routing\RouteMatchInterface;
 use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
+use Drupal\Core\Entity\EntityForm;
 
 /**
  * Implements hook_help().
@@ -320,7 +321,7 @@ 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();
 
@@ -332,6 +333,51 @@ function islandora_form_alter(&$form, FormStateInterface $form_state, $form_id)
       }
     }
   }
+
+  $form_object = $form_state->getFormObject();
+
+  $utils = \Drupal::service('islandora.utils');
+
+//  kint($form,$form_state);
+
+  if($form_object instanceof EntityForm) {
+    $entity = $form_object->getEntity();
+    if ($utils->isIslandoraType($entity->getEntityTypeId(), $entity->bundle()) && strpos($form['#form_id'], 'delete_form') !== FALSE) {
+      $form['delete_associated_content'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Delete all associated medias and nodes'),
+      );
+
+      $medias = $utils->getMedia($form_state->getFormObject()->getEntity());
+      $media_list = "";
+
+      foreach($medias as $media){
+        $media_list.= "<li>{$media->getName()}</li>";
+      }
+
+      $form['media_items'] = array(
+        '#suffix' => "<ul>{$media_list}</ul>", // Add markup after form item
+      );
+
+      $form['actions']['submit']['#submit'][] = 'islandora_object_delete_form_submit';
+      return $form;
+    }
+  }
+  return $form;
+}
+
+function islandora_object_delete_form_submit($form, &$form_state){
+
+  $result = $form_state->getValues('delete_associated_content');
+  $utils = \Drupal::service('islandora.utils');
+
+  if($result['delete_associated_content'] == 1) {
+
+    $medias = $utils->getMedia($form_state->getFormObject()->getEntity());
+    foreach ($medias as $media) {
+      $media->delete();
+    }
+  }
 }
 
 /**
@@ -551,26 +597,5 @@ 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();
-    }
-  }
-}