From 79a07a4e31a1588f792d1ba93b4d4ad2a87e2a3b Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Sun, 10 Dec 2023 23:38:20 +0000 Subject: [PATCH] Issue #2901412 by claudiu.cristea, pfrenssen, grathbone, ravi.shankar, saidatom, Lendude, quietone: Add current route parameters to the confirmation form route --- .../views/field/CommentBulkFormTest.php | 5 +- .../Plugin/views/field/NodeBulkFormTest.php | 5 +- .../Plugin/views/field/UserBulkFormTest.php | 5 +- .../views/src/Plugin/views/field/BulkForm.php | 24 +++++- .../action_bulk_test.routing.yml | 10 +++ .../install/system.action.test_action.yml | 10 +++ .../install/views.view.test_bulk_form.yml | 86 +++++++++++++++++++ .../config/schema/action_bulk_test.schema.yml | 2 + .../src/Form/TestActionConfirmForm.php | 42 +++++++++ .../src/Plugin/Action/TestAction.php | 36 ++++++++ .../tests/src/Functional/BulkFormTest.php | 26 ++++++ 11 files changed, 245 insertions(+), 6 deletions(-) create mode 100644 core/modules/views/tests/modules/action_bulk_test/action_bulk_test.routing.yml create mode 100644 core/modules/views/tests/modules/action_bulk_test/config/install/system.action.test_action.yml create mode 100644 core/modules/views/tests/modules/action_bulk_test/config/schema/action_bulk_test.schema.yml create mode 100644 core/modules/views/tests/modules/action_bulk_test/src/Form/TestActionConfirmForm.php create mode 100644 core/modules/views/tests/modules/action_bulk_test/src/Plugin/Action/TestAction.php diff --git a/core/modules/comment/tests/src/Unit/Plugin/views/field/CommentBulkFormTest.php b/core/modules/comment/tests/src/Unit/Plugin/views/field/CommentBulkFormTest.php index e4aa6dae605..4f769b93820 100644 --- a/core/modules/comment/tests/src/Unit/Plugin/views/field/CommentBulkFormTest.php +++ b/core/modules/comment/tests/src/Unit/Plugin/views/field/CommentBulkFormTest.php @@ -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'); diff --git a/core/modules/node/tests/src/Unit/Plugin/views/field/NodeBulkFormTest.php b/core/modules/node/tests/src/Unit/Plugin/views/field/NodeBulkFormTest.php index 044e4340b41..67e1ad4e912 100644 --- a/core/modules/node/tests/src/Unit/Plugin/views/field/NodeBulkFormTest.php +++ b/core/modules/node/tests/src/Unit/Plugin/views/field/NodeBulkFormTest.php @@ -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'); diff --git a/core/modules/user/tests/src/Unit/Plugin/views/field/UserBulkFormTest.php b/core/modules/user/tests/src/Unit/Plugin/views/field/UserBulkFormTest.php index 97c9bfc4c5b..9b007ff11f3 100644 --- a/core/modules/user/tests/src/Unit/Plugin/views/field/UserBulkFormTest.php +++ b/core/modules/user/tests/src/Unit/Plugin/views/field/UserBulkFormTest.php @@ -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'); diff --git a/core/modules/views/src/Plugin/views/field/BulkForm.php b/core/modules/views/src/Plugin/views/field/BulkForm.php index a2552bdca7d..54517f8bc36 100644 --- a/core/modules/views/src/Plugin/views/field/BulkForm.php +++ b/core/modules/views/src/Plugin/views/field/BulkForm.php @@ -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 diff --git a/core/modules/views/tests/modules/action_bulk_test/action_bulk_test.routing.yml b/core/modules/views/tests/modules/action_bulk_test/action_bulk_test.routing.yml new file mode 100644 index 00000000000..43cab18ec54 --- /dev/null +++ b/core/modules/views/tests/modules/action_bulk_test/action_bulk_test.routing.yml @@ -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 diff --git a/core/modules/views/tests/modules/action_bulk_test/config/install/system.action.test_action.yml b/core/modules/views/tests/modules/action_bulk_test/config/install/system.action.test_action.yml new file mode 100644 index 00000000000..555531cc8e2 --- /dev/null +++ b/core/modules/views/tests/modules/action_bulk_test/config/install/system.action.test_action.yml @@ -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: { } diff --git a/core/modules/views/tests/modules/action_bulk_test/config/install/views.view.test_bulk_form.yml b/core/modules/views/tests/modules/action_bulk_test/config/install/views.view.test_bulk_form.yml index 29286312842..6446dc86c8c 100644 --- a/core/modules/views/tests/modules/action_bulk_test/config/install/views.view.test_bulk_form.yml +++ b/core/modules/views/tests/modules/action_bulk_test/config/install/views.view.test_bulk_form.yml @@ -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: { } diff --git a/core/modules/views/tests/modules/action_bulk_test/config/schema/action_bulk_test.schema.yml b/core/modules/views/tests/modules/action_bulk_test/config/schema/action_bulk_test.schema.yml new file mode 100644 index 00000000000..8627f6abef0 --- /dev/null +++ b/core/modules/views/tests/modules/action_bulk_test/config/schema/action_bulk_test.schema.yml @@ -0,0 +1,2 @@ +action.configuration.test_action: + type: mapping diff --git a/core/modules/views/tests/modules/action_bulk_test/src/Form/TestActionConfirmForm.php b/core/modules/views/tests/modules/action_bulk_test/src/Form/TestActionConfirmForm.php new file mode 100644 index 00000000000..f49aa712bbf --- /dev/null +++ b/core/modules/views/tests/modules/action_bulk_test/src/Form/TestActionConfirmForm.php @@ -0,0 +1,42 @@ +'); + } + + /** + * {@inheritdoc} + */ + public function getQuestion(): MarkupInterface { + return $this->t('Do you agree?'); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state): void { + } + +} diff --git a/core/modules/views/tests/modules/action_bulk_test/src/Plugin/Action/TestAction.php b/core/modules/views/tests/modules/action_bulk_test/src/Plugin/Action/TestAction.php new file mode 100644 index 00000000000..9e60d2895b9 --- /dev/null +++ b/core/modules/views/tests/modules/action_bulk_test/src/Plugin/Action/TestAction.php @@ -0,0 +1,36 @@ +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'); + } + }