From 7d58ddb49c815c4ba870ca337e3a6431b0fa5177 Mon Sep 17 00:00:00 2001
From: shriram <saravananshriram80@gmail.com>
Date: Tue, 15 Nov 2022 14:35:01 -0400
Subject: [PATCH] added feature toggle for the behavior

---
 islandora.module                   |  4 +++-
 src/Form/IslandoraSettingsForm.php | 10 ++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/islandora.module b/islandora.module
index d079aae6..dd1d66f8 100644
--- a/islandora.module
+++ b/islandora.module
@@ -333,10 +333,12 @@ 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 ($form_object instanceof EntityForm) {
+  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) {
diff --git a/src/Form/IslandoraSettingsForm.php b/src/Form/IslandoraSettingsForm.php
index 5049ef16..a65ab82f 100644
--- a/src/Form/IslandoraSettingsForm.php
+++ b/src/Form/IslandoraSettingsForm.php
@@ -42,6 +42,7 @@ class IslandoraSettingsForm extends ConfigFormBase {
     'year',
   ];
   const GEMINI_PSEUDO_FIELD = 'field_gemini_uri';
+  const NODE_DELETE_MEDIA_AND_FILES = 'delete_media_and_files';
 
   /**
    * To list the available bundle types.
@@ -201,6 +202,14 @@ class IslandoraSettingsForm extends ConfigFormBase {
       $fedora_url = NULL;
     }
 
+    $form[self::NODE_DELETE_MEDIA_AND_FILES] = [
+      '#type' => 'checkbox',
+      '#title' => $this->t('Node Delete with Media and Files'),
+      '#description' => $this->t('adds a checkbox in the "Delete" tab of islandora objects to delete media and files associated with the object.'
+      ),
+      '#default_value' => (bool) $config->get(self::NODE_DELETE_MEDIA_AND_FILES),
+    ];
+
     $form[self::FEDORA_URL] = [
       '#type' => 'textfield',
       '#title' => $this->t('Fedora URL'),
@@ -351,6 +360,7 @@ class IslandoraSettingsForm extends ConfigFormBase {
       ->set(self::UPLOAD_FORM_LOCATION, $form_state->getValue(self::UPLOAD_FORM_LOCATION))
       ->set(self::UPLOAD_FORM_ALLOWED_MIMETYPES, $form_state->getValue(self::UPLOAD_FORM_ALLOWED_MIMETYPES))
       ->set(self::GEMINI_PSEUDO, $new_pseudo_types)
+      ->set(self::NODE_DELETE_MEDIA_AND_FILES, $form_state->getValue(self::NODE_DELETE_MEDIA_AND_FILES))
       ->save();
 
     parent::submitForm($form, $form_state);