diff --git a/config/install/system.action.message_delete_action.yml b/config/install/system.action.message_delete_action.yml index 24be25576009043b967b809ec5d16f817467e564..02428173e486d9138d07becd8b8578ab34a16443 100644 --- a/config/install/system.action.message_delete_action.yml +++ b/config/install/system.action.message_delete_action.yml @@ -3,7 +3,7 @@ label: 'Delete selected messages' status: true langcode: en type: message -plugin: message_delete_action +plugin: entity:delete_action:message dependencies: module: - message diff --git a/config/schema/message.schema.yml b/config/schema/message.schema.yml index 2d2807b33a0062001b92efca38bf3796490c0388..abf57e3e7895bbc681a88e4a8f402958c47f5a8a 100644 --- a/config/schema/message.schema.yml +++ b/config/schema/message.schema.yml @@ -75,10 +75,6 @@ message.template.*: weight: type: integer -action.configuration.message_delete_action: - type: action_configuration_default - label: 'Delete message configuration' - message.message: type: mapping label: 'Message settings' diff --git a/message.install b/message.install index 2657ae7bb2e20997680e769e55469efd60fa247f..2201576e2b60f4bdd6df0efeb184a43c51958f01 100644 --- a/message.install +++ b/message.install @@ -27,3 +27,20 @@ function message_update_8100() { } } } + +/** + * Update message_delete_action plugin. + */ +function message_update_8102() { + $ids = \Drupal::entityQuery('action') + ->accessCheck(FALSE) + ->condition('plugin', 'message_delete_action') + ->execute(); + + foreach ($ids as $id) { + \Drupal::configFactory() + ->getEditable('system.action.' . $id) + ->set('plugin', 'entity:delete_action:message') + ->save(); + } +} diff --git a/message.routing.yml b/message.routing.yml index 6a24d4e59067177879d0b8f8c3d7431fd4a8cbfe..d43e051d21911f1d5530304436aee2d8e8f342ae 100644 --- a/message.routing.yml +++ b/message.routing.yml @@ -55,10 +55,3 @@ message.settings: _form: 'Drupal\message\Form\MessageSettingsForm' requirements: _permission: 'administer message templates' - -message.multiple_delete_confirm: - path: '/admin/content/message/delete' - defaults: - _form: '\Drupal\message\Form\DeleteMultiple' - requirements: - _permission: 'administer message templates' diff --git a/src/Entity/Message.php b/src/Entity/Message.php index fac03c6bcb05fbc060fc4a4c3d85a906a2b3bb1a..9e0e71ff44c299ae9cb19a19f60e5520a1e4f6f0 100644 --- a/src/Entity/Message.php +++ b/src/Entity/Message.php @@ -49,9 +49,17 @@ use Drupal\user\UserInterface; * "views_data" = "Drupal\message\MessageViewsData", * "form" = { * "default" = "Drupal\Core\Entity\ContentEntityForm", + * "delete-multiple-confirm" = "Drupal\Core\Entity\Form\DeleteMultipleForm", + * }, + * "route_provider" = { + * "html" = "\Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider", * }, * }, - * field_ui_base_route = "entity.message_template.edit_form" + * field_ui_base_route = "entity.message_template.edit_form", + * admin_permission = "administer messages", + * links = { + * "delete-multiple-form" = "/admin/content/message/delete", + * } * ) */ class Message extends ContentEntityBase implements MessageInterface { @@ -414,4 +422,15 @@ class Message extends ContentEntityBase implements MessageInterface { return [\Drupal::currentUser()->id()]; } + /** + * {@inheritdoc} + */ + public function label() { + $params = [ + '@id' => $this->id(), + '@template' => $this->getTemplate()->label(), + ]; + return t('Message ID @id (template: @template)', $params); + } + } diff --git a/src/Form/DeleteMultiple.php b/src/Form/DeleteMultiple.php deleted file mode 100644 index a9e1bd580d759cb32c21fb2d61a2185964b00027..0000000000000000000000000000000000000000 --- a/src/Form/DeleteMultiple.php +++ /dev/null @@ -1,129 +0,0 @@ -<?php - -namespace Drupal\message\Form; - -use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Form\ConfirmFormBase; -use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Url; -use Drupal\message\Entity\Message; -use Drupal\Core\TempStore\PrivateTempStoreFactory; -use Symfony\Component\HttpFoundation\RedirectResponse; -use Symfony\Component\DependencyInjection\ContainerInterface; - -/** - * Provides a message deletion confirmation form. - */ -class DeleteMultiple extends ConfirmFormBase { - /** - * The array of messages to delete. - * - * @var array - */ - protected $messages = []; - - /** - * The tempstore factory. - * - * @var \Drupal\Core\TempStore\PrivateTempStoreFactory - */ - protected $tempStoreFactory; - - /** - * Constructs a DeleteMultiple form object. - * - * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory - * The tempstore factory. - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager - * The entity type manager. - */ - public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $entity_type_manager) { - $this->tempStoreFactory = $temp_store_factory; - $this->storage = $entity_type_manager->getStorage('message'); - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('tempstore.private'), - $container->get('entity_type.manager') - ); - } - - /** - * {@inheritdoc} - */ - public function getFormId() { - return 'message_multiple_delete_confirm'; - } - - /** - * {@inheritdoc} - */ - public function getQuestion() { - return \Drupal::translation()->formatPlural(count($this->messages), 'Are you sure you want to delete this item?', 'Are you sure you want to delete these items?'); - } - - /** - * {@inheritdoc} - */ - public function getCancelRoute() { - } - - /** - * {@inheritdoc} - */ - public function getConfirmText() { - return t('Delete'); - } - - /** - * {@inheritdoc} - */ - public function buildForm(array $form, FormStateInterface $form_state) { - $this->messages = $this->tempStoreFactory->get('message_multiple_delete_confirm')->get(\Drupal::currentUser()->id()); - if (empty($this->messages)) { - return new RedirectResponse($this->getCancelUrl()->setAbsolute()->toString()); - } - - $form['messages'] = [ - '#theme' => 'item_list', - '#items' => array_map(function (Message $message) { - $params = [ - '@id' => $message->id(), - '@template' => $message->getTemplate()->label(), - ]; - return t('Delete message ID @id for template @template', $params); - }, $this->messages), - ]; - $form = parent::buildForm($form, $form_state); - - $form['actions']['cancel']['#href'] = $this->getCancelRoute(); - $form['actions']['submit']['#submit'] = ['::submitForm']; - return $form; - } - - /** - * {@inheritdoc} - */ - public function submitForm(array &$form, FormStateInterface $form_state) { - if ($form_state->getValue('confirm') && !empty($this->messages)) { - $this->storage->delete($this->messages); - $this->tempStoreFactory->get('message_multiple_delete_confirm')->delete(\Drupal::currentUser()->id()); - $count = count($this->messages); - $this->logger('message')->notice('Deleted @count messages.', ['@count' => $count]); - $this->messenger()->addMessage(\Drupal::translation()->formatPlural($count, 'Deleted 1 message.', 'Deleted @count messages.')); - } - $form_state->setRedirect('message.messages'); - } - - /** - * {@inheritdoc} - */ - public function getCancelUrl() { - return new Url('message.messages'); - } - -} diff --git a/src/Plugin/Action/DeleteMessage.php b/src/Plugin/Action/DeleteMessage.php deleted file mode 100644 index accf644f678fac771639709deea86633b41ea1e7..0000000000000000000000000000000000000000 --- a/src/Plugin/Action/DeleteMessage.php +++ /dev/null @@ -1,92 +0,0 @@ -<?php - -namespace Drupal\message\Plugin\Action; - -use Drupal\Core\Action\ActionBase; -use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Drupal\Core\Session\AccountInterface; -use Drupal\Core\TempStore\PrivateTempStoreFactory; -use Symfony\Component\DependencyInjection\ContainerInterface; - -/** - * Redirects to a message deletion form. - * - * @Action( - * id = "message_delete_action", - * label = @Translation("Delete selected content"), - * type = "message", - * confirm_form_route_name = "message.multiple_delete_confirm" - * ) - */ -class DeleteMessage extends ActionBase implements ContainerFactoryPluginInterface { - - /** - * The tempstore object. - * - * @var \Drupal\Core\TempStore\PrivateTempStore - */ - protected $tempStore; - - /** - * The current user. - * - * @var \Drupal\Core\Session\AccountInterface - */ - protected $currentUser; - - /** - * Constructs a new DeleteMessage object. - * - * @param array $configuration - * A configuration array containing information about the plugin instance. - * @param string $plugin_id - * The plugin ID for the plugin instance. - * @param mixed $plugin_definition - * The plugin implementation definition. - * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory - * The tempstore factory. - * @param \Drupal\Core\Session\AccountInterface $current_user - * Current user. - */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, PrivateTempStoreFactory $temp_store_factory, AccountInterface $current_user) { - $this->currentUser = $current_user; - $this->tempStore = $temp_store_factory->get('message_multiple_delete_confirm'); - - parent::__construct($configuration, $plugin_id, $plugin_definition); - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('tempstore.private'), - $container->get('current_user') - ); - } - - /** - * {@inheritdoc} - */ - public function executeMultiple(array $entities) { - $this->tempStore->set($this->currentUser->id(), $entities); - } - - /** - * {@inheritdoc} - */ - public function execute($object = NULL) { - $this->executeMultiple([$object]); - } - - /** - * {@inheritdoc} - */ - public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { - return $account->hasPermission('administer messages'); - } - -}