Skip to content
Snippets Groups Projects

ISTWCMS-5544 Refactor add a warning message when deleting content

2 files
+ 37
10
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -2,9 +2,11 @@
@@ -2,9 +2,11 @@
namespace Drupal\uw_cfg_common\EventSubscriber;
namespace Drupal\uw_cfg_common\EventSubscriber;
 
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\core_event_dispatcher\Event\Form\FormAlterEvent;
use Drupal\core_event_dispatcher\Event\Form\FormAlterEvent;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\core_event_dispatcher\FormHookEvents;
 
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
/**
@@ -14,12 +16,39 @@ class UwDeleteFormEventSubscriber implements EventSubscriberInterface {
@@ -14,12 +16,39 @@ class UwDeleteFormEventSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
use StringTranslationTrait;
 
/**
 
* Current user.
 
*
 
* @var \Drupal\Core\Session\AccountProxyInterface
 
*/
 
protected $currentUser;
 
 
/**
 
* Default constructor.
 
*
 
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
 
* Current user.
 
*/
 
public function __construct(AccountProxyInterface $currentUser) {
 
$this->currentUser = $currentUser;
 
}
 
 
/**
 
* {@inheritdoc}
 
*/
 
public static function create(ContainerInterface $container) {
 
// Instantiates this form class.
 
return new static(
 
$container->get('current_user'),
 
);
 
}
 
/**
/**
* {@inheritdoc}
* {@inheritdoc}
*/
*/
public static function getSubscribedEvents() {
public static function getSubscribedEvents() {
return [
return [
HookEventDispatcherInterface::FORM_ALTER => 'alterForm',
FormHookEvents::FORM_ALTER => 'alterForm',
];
];
}
}
@@ -73,9 +102,6 @@ class UwDeleteFormEventSubscriber implements EventSubscriberInterface {
@@ -73,9 +102,6 @@ class UwDeleteFormEventSubscriber implements EventSubscriberInterface {
],
],
];
];
// Get current user.
$user = \Drupal::currentUser();
// Loop all names in array.
// Loop all names in array.
foreach ($names as $key => $type_names) {
foreach ($names as $key => $type_names) {
foreach ($type_names as $name) {
foreach ($type_names as $name) {
@@ -85,8 +111,8 @@ class UwDeleteFormEventSubscriber implements EventSubscriberInterface {
@@ -85,8 +111,8 @@ class UwDeleteFormEventSubscriber implements EventSubscriberInterface {
// The user has 'delete any' and 'delete own' permissions.
// The user has 'delete any' and 'delete own' permissions.
if ($key == 'ct_names') {
if ($key == 'ct_names') {
if (($form['#form_id'] == 'node_' . $name . '_delete_form') &&
if (($form['#form_id'] == 'node_' . $name . '_delete_form') &&
($user->hasPermission('delete any ' . $name . ' content') ||
($this->currentUser->hasPermission('delete any ' . $name . ' content') ||
$user->hasPermission('delete own ' . $name . ' content'))) {
$this->currentUser->hasPermission('delete own ' . $name . ' content'))) {
$form['description']['#markup'] = $custom_waring_message;
$form['description']['#markup'] = $custom_waring_message;
break;
break;
}
}
@@ -94,7 +120,7 @@ class UwDeleteFormEventSubscriber implements EventSubscriberInterface {
@@ -94,7 +120,7 @@ class UwDeleteFormEventSubscriber implements EventSubscriberInterface {
// The user has 'delete terms' permission.
// The user has 'delete terms' permission.
if ($key == 'vocab_names') {
if ($key == 'vocab_names') {
if (($form['#form_id'] == 'taxonomy_term_' . $name . '_delete_form') &&
if (($form['#form_id'] == 'taxonomy_term_' . $name . '_delete_form') &&
$user->hasPermission('delete terms in ' . $name)) {
$this->currentUser->hasPermission('delete terms in ' . $name)) {
$form['description']['#markup'] = $custom_waring_message;
$form['description']['#markup'] = $custom_waring_message;
break;
break;
}
}
@@ -102,8 +128,8 @@ class UwDeleteFormEventSubscriber implements EventSubscriberInterface {
@@ -102,8 +128,8 @@ class UwDeleteFormEventSubscriber implements EventSubscriberInterface {
// The user has 'delete any' permission.
// The user has 'delete any' permission.
if ($key == 'media_names') {
if ($key == 'media_names') {
if (($form['#form_id'] == 'media_' . $name . '_delete_form') &&
if (($form['#form_id'] == 'media_' . $name . '_delete_form') &&
($user->hasPermission('delete any ' . $name . ' media') ||
($this->currentUser->hasPermission('delete any ' . $name . ' media') ||
$user->hasPermission('delete own ' . $name . ' media'))) {
$this->currentUser->hasPermission('delete own ' . $name . ' media'))) {
$form['description']['#markup'] = $custom_waring_message;
$form['description']['#markup'] = $custom_waring_message;
break;
break;
}
}
Loading