Issue #2875987 by raman.b, smustgrave, ericgsmith, anushrikumari, Lendude: Form alter hook called twice for views forms without view arguments

merge-requests/2817/merge
Alex Pott 2022-10-03 11:24:38 +01:00
parent 5b84c09246
commit c723f4b78b
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
4 changed files with 62 additions and 2 deletions

View File

@ -148,8 +148,11 @@ class ViewsForm implements FormInterface, ContainerInjectionInterface {
}
$form_state->set(['step_controller', 'views_form_views_form'], 'Drupal\views\Form\ViewsFormMainForm');
// Add the base form ID.
$form_state->addBuildInfo('base_form_id', $this->getBaseFormId());
// Views forms without view arguments return the same Base Form ID and
// Form ID. Base form ID should only be added when different.
if ($this->getBaseFormId() !== $this->getFormId()) {
$form_state->addBuildInfo('base_form_id', $this->getBaseFormId());
}
$form = [];

View File

@ -0,0 +1,8 @@
name: 'Views Form Test'
type: module
description: 'Provides hook to alter views form for testing purposes.'
package: Testing
version: VERSION
dependencies:
- drupal:views
- drupal:media

View File

@ -0,0 +1,17 @@
<?php
/**
* @file
* Hook implementations for this module.
*/
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function views_form_test_form_views_form_media_media_page_list_alter(&$form, FormStateInterface $form_state, $form_id) {
$state = \Drupal::state();
$count = $state->get('hook_form_BASE_FORM_ID_alter_count', 0);
$state->set('hook_form_BASE_FORM_ID_alter_count', $count + 1);
}

View File

@ -0,0 +1,32 @@
<?php
namespace Drupal\Tests\views\Functional;
/**
* Tests hook_form_BASE_FORM_ID_alter for a ViewsForm.
*
* @group views
*/
class ViewsFormAlterTest extends ViewTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['views_form_test'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests hook_form_BASE_FORM_ID_alter for a ViewsForm.
*/
public function testViewsFormAlter() {
$this->drupalLogin($this->createUser(['access media overview']));
$this->drupalGet('admin/content/media');
$count = $this->container->get('state')->get('hook_form_BASE_FORM_ID_alter_count');
$this->assertEquals(1, $count, 'hook_form_BASE_FORM_ID_alter was invoked only once');
}
}