From e89e3f75cbea9297424ed39ee65a84425ccf937c Mon Sep 17 00:00:00 2001
From: shriram1056 <saravananshriram80@gmail.com>
Date: Fri, 2 Dec 2022 16:36:04 -0400
Subject: [PATCH] minor changes and post_update for delete_media_and_files

---
 css/{islandora.theme.css => islandora.css} |  0
 islandora.libraries.yml                    |  4 +--
 islandora.module                           | 32 +++++++++++++---------
 islandora.post_update.php                  | 16 +++++++++++
 src/Form/IslandoraSettingsForm.php         |  2 +-
 5 files changed, 38 insertions(+), 16 deletions(-)
 rename css/{islandora.theme.css => islandora.css} (100%)
 create mode 100644 islandora.post_update.php

diff --git a/css/islandora.theme.css b/css/islandora.css
similarity index 100%
rename from css/islandora.theme.css
rename to css/islandora.css
diff --git a/islandora.libraries.yml b/islandora.libraries.yml
index 047006bf..840dc294 100644
--- a/islandora.libraries.yml
+++ b/islandora.libraries.yml
@@ -1,5 +1,5 @@
-drupal.islandora.theme_css:
+islandora:
   version: VERSION
   css:
     theme:
-      css/islandora.theme.css: {}
+      css/islandora.css: {}
diff --git a/islandora.module b/islandora.module
index 3e52e96a..6f7a15d2 100644
--- a/islandora.module
+++ b/islandora.module
@@ -355,7 +355,16 @@ function islandora_form_alter(&$form, FormStateInterface $form_state, $form_id)
           $media_list[] = $media->getName();
         }
 
-        $form['media_items'] = [
+        $form['container'] = [
+          '#type' => 'container',
+          '#states' => [
+            'visible' => [
+              ':input[name="delete_associated_content"]' => ['checked' => TRUE],
+            ],
+          ],
+        ];
+
+        $form['container']['media_items'] = [
           '#theme' => 'item_list',
           '#type' => 'ul',
           '#items' => $media_list,
@@ -363,7 +372,7 @@ function islandora_form_alter(&$form, FormStateInterface $form_state, $form_id)
           '#wrapper_attributes' => ['class' => ['container']],
           '#attached' => [
             'library' => [
-              'islandora/drupal.islandora.theme_css',
+              'islandora/islandora',
             ],
           ],
         ];
@@ -380,7 +389,7 @@ function islandora_form_alter(&$form, FormStateInterface $form_state, $form_id)
 /**
  * Implements a submit handler for the delete form.
  */
-function islandora_object_delete_form_submit($form, &$form_state) {
+function islandora_object_delete_form_submit($form, FormStateInterface $form_state) {
 
   $result = $form_state->getValues('delete_associated_content');
   $utils = \Drupal::service('islandora.utils');
@@ -389,14 +398,13 @@ function islandora_object_delete_form_submit($form, &$form_state) {
 
     $node = $form_state->getFormObject()->getEntity();
     $medias = $utils->getMedia($node);
-    $media_list = "";
+    $media_list = [];
 
     $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 = [];
     $media_translations = [];
     $media_files = [];
@@ -424,7 +432,6 @@ function islandora_object_delete_form_submit($form, &$form_state) {
               continue;
             }
             $media_files[$id][$file->id()] = $file;
-            $total_count++;
           }
         }
       }
@@ -435,7 +442,6 @@ function islandora_object_delete_form_submit($form, &$form_state) {
         if ($entity->isDefaultTranslation()) {
           $delete_media[$id] = $entity;
           unset($media_translations[$id]);
-          $total_count += count($entity->getTranslationLanguages());
         }
         elseif (!isset($delete_media[$id])) {
           $media_translations[$id][] = $entity;
@@ -447,7 +453,7 @@ function islandora_object_delete_form_submit($form, &$form_state) {
       foreach ($delete_media as $id => $media) {
         try {
           $media->delete();
-          $media_list .= $id . ", ";
+          $media_list[] = $id;
           $logger->notice('The media %label has been deleted.', [
             '%label' => $media->label(),
           ]);
@@ -490,12 +496,11 @@ function islandora_object_delete_form_submit($form, &$form_state) {
             '@language' => $translation->language()->getName(),
           ]);
         }
-        $total_count += count($translations);
       }
     }
 
     if ($inaccessible_entities) {
-      $messenger->addWarning("@count items has not been deleted because you do not have the necessary permissions.", [
+      $messenger->addWarning("@count items have not been deleted because you do not have the necessary permissions.", [
         '@count' => count($inaccessible_entities),
       ]);
     }
@@ -507,9 +512,10 @@ function islandora_object_delete_form_submit($form, &$form_state) {
         '#value' => t("The repository item @node and @media", [
           '@node' => $node->getTitle(),
           '@media' => \Drupal::translation()->formatPlural(
-            substr_count($media_list, ",") + 1, 'the media with the id @media has been deleted.',
-            'the medias with the id @media have been deleted.',
-            ['@media' => mb_substr($media_list, 0, strlen($media_list) - 2)]),
+            count($media_list), 'the media with the id @media has been deleted.',
+            'the medias with the ids @media have been deleted.',
+            ['@media' => implode(", ", $media_list)],
+          ),
         ]),
       ],
     ];
diff --git a/islandora.post_update.php b/islandora.post_update.php
new file mode 100644
index 00000000..0a29e56e
--- /dev/null
+++ b/islandora.post_update.php
@@ -0,0 +1,16 @@
+<?php
+
+/**
+ * @file
+ * Post updates.
+ */
+
+/**
+ * Set default value for delete_media_and_files field in settings.
+ */
+function islandora_post_update_delete_media_and_files() {
+  $config_factory = \Drupal::configFactory();
+  $config = $config_factory->getEditable('islandora.settings');
+  $config->set('delete_media_and_files', TRUE);
+  $config->save(TRUE);
+}
diff --git a/src/Form/IslandoraSettingsForm.php b/src/Form/IslandoraSettingsForm.php
index a65ab82f..90e0b420 100644
--- a/src/Form/IslandoraSettingsForm.php
+++ b/src/Form/IslandoraSettingsForm.php
@@ -205,7 +205,7 @@ class IslandoraSettingsForm extends ConfigFormBase {
     $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.'
+      '#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),
     ];