From d41a21fd98c42a9504acdef29634c7e84d56b8b9 Mon Sep 17 00:00:00 2001
From: Igor Biki <ibiki@uwaterloo.ca>
Date: Fri, 14 Oct 2022 10:39:42 -0400
Subject: [PATCH] ISTWCMS-5544: Refactoring of code.

---
 .../UwDeleteFormEventSubscriber.php           | 86 +++----------------
 1 file changed, 12 insertions(+), 74 deletions(-)

diff --git a/src/EventSubscriber/UwDeleteFormEventSubscriber.php b/src/EventSubscriber/UwDeleteFormEventSubscriber.php
index 286c1198..4d5fa9b1 100644
--- a/src/EventSubscriber/UwDeleteFormEventSubscriber.php
+++ b/src/EventSubscriber/UwDeleteFormEventSubscriber.php
@@ -50,80 +50,18 @@ class UwDeleteFormEventSubscriber implements EventSubscriberInterface {
   public function alterForm(FormAlterEvent $event): void {
     $form = &$event->getForm();
 
-    // Define all names in an array.
-    $names = [
-      'ct_names' => [
-        'uw_ct_blog',
-        'uw_ct_contact',
-        'uw_ct_catalog_item',
-        'uw_ct_event',
-        'uw_ct_expand_collapse_group',
-        'uw_ct_news_item',
-        'uw_ct_opportunity',
-        'uw_ct_profile',
-        'uw_ct_project',
-        'uw_ct_service',
-        'uw_ct_sidebar',
-        'uw_ct_site_footer',
-        'uw_ct_web_page',
-      ],
-      'vocab_names' => [
-        'uw_vocab_blog_tags',
-        'uw_vocab_contact_group',
-        'uw_vocab_audience',
-        'uw_vocab_catalog_categories',
-        'uw_vocab_catalogs',
-        'uw_tax_event_tags',
-        'uw_tax_event_type',
-        'uw_vocab_news_tags',
-        'uw_vocab_profile_type',
-        'uw_vocab_project_roles',
-        'uw_vocab_project_topics',
-        'uw_vocab_service_categories',
-      ],
-      'media_names' => [
-        'uw_mt_file',
-        'uw_mt_icon',
-        'uw_mt_image',
-        'uw_mt_local_video',
-        'uw_mt_remote_video',
-        'uw_mt_vimeo_banner_video',
-      ],
-    ];
-
-    // Loop all names in array.
-    foreach ($names as $key => $type_names) {
-      foreach ($type_names as $name) {
-        $custom_waring_message = $this->t('CAUTION. This will permanently delete this piece of content; this action cannot be undone. If anything references this content, it may cause visual or structural issues on that page. Make sure you have removed or updated all references before deleting.');
-
-        // Use custom warning message for deleting nodes, terms and medias.
-        // The user has 'delete any' and 'delete own' permissions.
-        if ($key == 'ct_names') {
-          if (($form['#form_id'] == 'node_' . $name . '_delete_form') &&
-            ($this->currentUser->hasPermission('delete any ' . $name . ' content') ||
-              $this->currentUser->hasPermission('delete own ' . $name . ' content'))) {
-            $form['description']['#markup'] = $custom_waring_message;
-            break;
-          }
-        }
-        // The user has 'delete terms' permission.
-        if ($key == 'vocab_names') {
-          if (($form['#form_id'] == 'taxonomy_term_' . $name . '_delete_form') &&
-            $this->currentUser->hasPermission('delete terms in ' . $name)) {
-            $form['description']['#markup'] = $custom_waring_message;
-            break;
-          }
-        }
-        // The user has 'delete any' permission.
-        if ($key == 'media_names') {
-          if (($form['#form_id'] == 'media_' . $name . '_delete_form') &&
-            ($this->currentUser->hasPermission('delete any media') || $this->currentUser->hasPermission('delete any ' . $name . ' media') ||
-              $this->currentUser->hasPermission('delete own ' . $name . ' media'))) {
-            $form['description']['#markup'] = $custom_waring_message;
-            break;
-          }
-        }
-      }
+    // Change the deletion warning on any of the following forms.
+    // - any node, taxonomy term, or media deletion page.
+    // - the node delete multiple page.
+    // - the media delete multiple page.
+    if (
+      preg_match('/^(?:node|taxonomy_term|media)_.+_delete_form$/', $form['#form_id'])
+      ||
+      $form['#form_id'] == 'node_delete_multiple_confirm_form'
+      ||
+      $form['#form_id'] == 'media_delete_multiple_confirm_form'
+    ) {
+      $form['description']['#markup'] = $this->t('CAUTION. This will permanently delete this piece of content; this action cannot be undone. If anything references this content, it may cause visual or structural issues on that page. Make sure you have removed or updated all references before deleting.');
     }
   }
 
-- 
GitLab