Issue #2901412 by claudiu.cristea, pfrenssen, grathbone, ravi.shankar, saidatom, Lendude, quietone: Add current route parameters to the confirmation form route

merge-requests/5760/head
Alex Pott 2023-12-10 23:38:20 +00:00
parent 64e5e002c2
commit 79a07a4e31
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
11 changed files with 245 additions and 6 deletions

View File

@ -6,6 +6,7 @@ use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\comment\Plugin\views\field\CommentBulkForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\ResettableStackedRouteMatchInterface;
use Drupal\Tests\UnitTestCase;
/**
@ -60,6 +61,8 @@ class CommentBulkFormTest extends UnitTestCase {
$messenger = $this->createMock('Drupal\Core\Messenger\MessengerInterface');
$route_match = $this->createMock(ResettableStackedRouteMatchInterface::class);
$views_data = $this->getMockBuilder('Drupal\views\ViewsData')
->disableOriginalConstructor()
->getMock();
@ -90,7 +93,7 @@ class CommentBulkFormTest extends UnitTestCase {
$definition['title'] = '';
$options = [];
$comment_bulk_form = new CommentBulkForm([], 'comment_bulk_form', $definition, $entity_type_manager, $language_manager, $messenger, $entity_repository);
$comment_bulk_form = new CommentBulkForm([], 'comment_bulk_form', $definition, $entity_type_manager, $language_manager, $messenger, $entity_repository, $route_match);
$comment_bulk_form->init($executable, $display, $options);
$reflected_actions = (new \ReflectionObject($comment_bulk_form))->getProperty('actions');

View File

@ -5,6 +5,7 @@ namespace Drupal\Tests\node\Unit\Plugin\views\field;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\ResettableStackedRouteMatchInterface;
use Drupal\node\Plugin\views\field\NodeBulkForm;
use Drupal\Tests\UnitTestCase;
@ -60,6 +61,8 @@ class NodeBulkFormTest extends UnitTestCase {
$messenger = $this->createMock('Drupal\Core\Messenger\MessengerInterface');
$route_match = $this->createMock(ResettableStackedRouteMatchInterface::class);
$views_data = $this->getMockBuilder('Drupal\views\ViewsData')
->disableOriginalConstructor()
->getMock();
@ -90,7 +93,7 @@ class NodeBulkFormTest extends UnitTestCase {
$definition['title'] = '';
$options = [];
$node_bulk_form = new NodeBulkForm([], 'node_bulk_form', $definition, $entity_type_manager, $language_manager, $messenger, $entity_repository);
$node_bulk_form = new NodeBulkForm([], 'node_bulk_form', $definition, $entity_type_manager, $language_manager, $messenger, $entity_repository, $route_match);
$node_bulk_form->init($executable, $display, $options);
$reflected_actions = (new \ReflectionObject($node_bulk_form))->getProperty('actions');

View File

@ -5,6 +5,7 @@ namespace Drupal\Tests\user\Unit\Plugin\views\field;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\ResettableStackedRouteMatchInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Plugin\views\field\UserBulkForm;
@ -60,6 +61,8 @@ class UserBulkFormTest extends UnitTestCase {
$messenger = $this->createMock('Drupal\Core\Messenger\MessengerInterface');
$route_match = $this->createMock(ResettableStackedRouteMatchInterface::class);
$views_data = $this->getMockBuilder('Drupal\views\ViewsData')
->disableOriginalConstructor()
->getMock();
@ -90,7 +93,7 @@ class UserBulkFormTest extends UnitTestCase {
$definition['title'] = '';
$options = [];
$user_bulk_form = new UserBulkForm([], 'user_bulk_form', $definition, $entity_type_manager, $language_manager, $messenger, $entity_repository);
$user_bulk_form = new UserBulkForm([], 'user_bulk_form', $definition, $entity_type_manager, $language_manager, $messenger, $entity_repository, $route_match);
$user_bulk_form->init($executable, $display, $options);
$reflected_actions = (new \ReflectionObject($user_bulk_form))->getProperty('actions');

View File

@ -12,6 +12,7 @@ use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Routing\ResettableStackedRouteMatchInterface;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\views\Entity\Render\EntityTranslationRenderTrait;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
@ -73,6 +74,13 @@ class BulkForm extends FieldPluginBase implements CacheableDependencyInterface {
*/
protected $messenger;
/**
* The current route match service.
*
* @var \Drupal\Core\Routing\ResettableStackedRouteMatchInterface
*/
protected ResettableStackedRouteMatchInterface $routeMatch;
/**
* Constructs a new BulkForm object.
*
@ -90,10 +98,13 @@ class BulkForm extends FieldPluginBase implements CacheableDependencyInterface {
* The messenger.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
* @param \Drupal\Core\Routing\ResettableStackedRouteMatchInterface $route_match
* The current route match service.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, MessengerInterface $messenger, EntityRepositoryInterface $entity_repository) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, MessengerInterface $messenger, EntityRepositoryInterface $entity_repository, ResettableStackedRouteMatchInterface $route_match = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
@ -101,6 +112,11 @@ class BulkForm extends FieldPluginBase implements CacheableDependencyInterface {
$this->languageManager = $language_manager;
$this->messenger = $messenger;
$this->entityRepository = $entity_repository;
if (!$route_match) {
@trigger_error('Calling BulkForm::__construct() without the $route_match argument is deprecated in drupal:10.3.0 and the $route_match argument will be required in drupal:11.0.0. See https://www.drupal.org/node/3115868', E_USER_DEPRECATED);
$route_match = \Drupal::routeMatch();
}
$this->routeMatch = $route_match;
}
/**
@ -114,7 +130,8 @@ class BulkForm extends FieldPluginBase implements CacheableDependencyInterface {
$container->get('entity_type.manager'),
$container->get('language_manager'),
$container->get('messenger'),
$container->get('entity.repository')
$container->get('entity.repository'),
$container->get('current_route_match')
);
}
@ -428,7 +445,8 @@ class BulkForm extends FieldPluginBase implements CacheableDependencyInterface {
$options = [
'query' => $this->getDestinationArray(),
];
$form_state->setRedirect($operation_definition['confirm_form_route_name'], [], $options);
$route_parameters = $this->routeMatch->getRawParameters()->all();
$form_state->setRedirect($operation_definition['confirm_form_route_name'], $route_parameters, $options);
}
else {
// Don't display the message unless there are some elements affected and

View File

@ -0,0 +1,10 @@
action_bulk_test.action.confirm:
path: '/node/{node}/confirm'
defaults:
_form: Drupal\action_bulk_test\Form\TestActionConfirmForm
requirements:
_access: 'TRUE'
options:
parameters:
node:
type: entity:node

View File

@ -0,0 +1,10 @@
langcode: en
status: true
dependencies:
module:
- action_bulk_test
id: test_action
label: 'Test action'
type: node
plugin: test_action
configuration: { }

View File

@ -120,6 +120,8 @@ display:
group: 1
expose:
operator: ''
operator_limit_selection: false
operator_list: { }
style:
type: table
options:
@ -151,6 +153,17 @@ display:
type: fields
query:
type: views_query
display_extenders: { }
arguments: { }
cache_metadata:
max-age: 0
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
- 'user.node_grants:view'
- user.permissions
tags: { }
page_1:
id: page_1
display_title: Page
@ -158,6 +171,16 @@ display:
position: null
display_options:
path: test_bulk_form
display_extenders: { }
cache_metadata:
max-age: 0
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
- 'user.node_grants:view'
- user.permissions
tags: { }
page_2:
id: page_2
display_title: Page
@ -205,3 +228,66 @@ display:
row: false
display_extenders: { }
path: display-without-fields
page_4:
id: page_4
display_title: 'Page 4'
display_plugin: page
position: 4
display_options:
arguments:
nid:
id: nid
table: node_field_data
field: nid
relationship: none
group_type: group
admin_label: ''
default_action: ignore
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: fixed
default_argument_options:
argument: ''
default_argument_skip_url: false
summary_options:
base_path: ''
count: true
items_per_page: 25
override: false
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: true
validate:
type: 'entity:node'
fail: 'not found'
validate_options:
operation: view
multiple: 0
bundles: { }
access: false
break_phrase: false
not: false
entity_type: node
entity_field: nid
plugin_id: node_nid
defaults:
arguments: false
display_description: ''
display_extenders: { }
path: node/%node/test_bulk_form
cache_metadata:
max-age: 0
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- url.query_args
- 'user.node_grants:view'
- user.permissions
tags: { }

View File

@ -0,0 +1,2 @@
action.configuration.test_action:
type: mapping

View File

@ -0,0 +1,42 @@
<?php
namespace Drupal\action_bulk_test\Form;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Confirmation form for 'test_action' action.
*/
class TestActionConfirmForm extends ConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'test_action_confirm_form';
}
/**
* {@inheritdoc}
*/
public function getCancelUrl(): Url {
return Url::fromRoute('<front>');
}
/**
* {@inheritdoc}
*/
public function getQuestion(): MarkupInterface {
return $this->t('Do you agree?');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace Drupal\action_bulk_test\Plugin\Action;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Action\Attribute\Action;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Test action.
*/
#[Action(
id: 'test_action',
label: new TranslatableMarkup('Test action'),
type: 'node',
confirm_form_route_name: 'action_bulk_test.action.confirm'
)]
class TestAction extends ActionBase {
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE): bool|AccessResultInterface {
return $return_as_object ? AccessResult::allowed() : TRUE;
}
/**
* {@inheritdoc}
*/
public function execute(): void {
}
}

View File

@ -4,6 +4,7 @@ namespace Drupal\Tests\views\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\views\Views;
/**
@ -14,6 +15,8 @@ use Drupal\views\Views;
*/
class BulkFormTest extends BrowserTestBase {
use NodeCreationTrait;
/**
* Modules to install.
*
@ -219,4 +222,27 @@ class BulkFormTest extends BrowserTestBase {
$this->assertSession()->pageTextContains('No content selected.');
}
/**
* Tests that route parameters are passed to the confirmation form route.
*/
public function testConfirmRouteWithParameters(): void {
$session = $this->getSession();
$page = $session->getPage();
$assert = $this->assertSession();
$node = $this->createNode();
// Access the view page.
$this->drupalGet('/node/' . $node->id() . '/test_bulk_form');
// Select a node and perform the 'Test action'.
$page->checkField('node_bulk_form[0]');
$page->selectFieldOption('Action', 'Test action');
$page->pressButton('Apply to selected items');
// Check that we've been landed on the confirmation form.
$assert->pageTextContains('Do you agree?');
// Check that route parameters were passed to the confirmation from route.
$assert->addressEquals('/node/' . $node->id() . '/confirm');
}
}