Commit ce45b6e2 authored by alexpott's avatar alexpott

Issue #1611954 by tim.plunkett, sun, samuel.mortenson, Berdir, mgifford, chx:...

Issue #1611954 by tim.plunkett, sun, samuel.mortenson, Berdir, mgifford, chx: Setting #access = FALSE on a vertical tab does not prevent input processing of the contained form elements
parent 7365cd41
......@@ -71,6 +71,10 @@ public static function preRenderVerticalTabs($element) {
* The processed element.
*/
public static function processVerticalTabs(&$element, FormStateInterface $form_state, &$complete_form) {
if (isset($element['#access']) && !$element['#access']) {
return $element;
}
// Inject a new details as child, so that form_process_details() processes
// this details element like any other details.
$element['group'] = array(
......
<?php
/**
* @file
* Contains Drupal\system\Tests\Form\ElementsAccessTest.
*/
namespace Drupal\system\Tests\Form;
use Drupal\simpletest\WebTestBase;
/**
* Tests access control for form elements.
*
* @group Form
*/
class ElementsAccessTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('form_test');
/**
* Ensures that child values are still processed when #access = FALSE.
*/
public function testAccessFalse() {
$this->drupalPostForm('form_test/vertical-tabs-access', NULL, t('Submit'));
$this->assertNoText(t('This checkbox inside a vertical tab does not have its default value.'));
$this->assertNoText(t('This textfield inside a vertical tab does not have its default value.'));
$this->assertNoText(t('This checkbox inside a fieldset does not have its default value.'));
$this->assertNoText(t('This checkbox inside a container does not have its default value.'));
$this->assertNoText(t('This checkbox inside a nested container does not have its default value.'));
$this->assertNoText(t('This checkbox inside a vertical tab whose fieldset access is allowed does not have its default value.'));
$this->assertText(t('The form submitted correctly.'));
}
}
......@@ -93,3 +93,14 @@ function form_test_user_register_form_rebuild($form, FormStateInterface $form_st
drupal_set_message('Form rebuilt.');
$form_state->setRebuild();
}
/**
* Implements hook_form_FORM_ID_alter() for form_test_vertical_tabs_access_form().
*/
function form_test_form_form_test_vertical_tabs_access_form_alter(&$form, &$form_state, $form_id) {
$form['vertical_tabs1']['#access'] = FALSE;
$form['vertical_tabs2']['#access'] = FALSE;
$form['tabs3']['#access'] = TRUE;
$form['fieldset1']['#access'] = FALSE;
$form['container']['#access'] = FALSE;
}
......@@ -172,6 +172,14 @@ form_test.storage:
requirements:
_access: 'TRUE'
form_test.vertical_tabs_access:
path: '/form_test/vertical-tabs-access'
defaults:
_form: '\Drupal\form_test\Form\FormTestVerticalTabsAccessForm'
_title: 'Vertical tabs tests'
requirements:
_access: 'TRUE'
form_test.state_clean:
path: '/form_test/form-state-values-clean'
defaults:
......
<?php
/**
* @file
* Contains \Drupal\form_test\Form\FormTestVerticalTabsAccessForm.
*/
namespace Drupal\form_test\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class FormTestVerticalTabsAccessForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'form_test_vertical_tabs_access_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['vertical_tabs1'] = array(
'#type' => 'vertical_tabs',
);
$form['tab1'] = array(
'#type' => 'fieldset',
'#title' => t('Tab 1'),
'#collapsible' => TRUE,
'#group' => 'vertical_tabs1',
);
$form['tab1']['field1'] = array(
'#title' => t('Field 1'),
'#type' => 'checkbox',
'#default_value' => TRUE,
);
$form['tab2'] = array(
'#type' => 'fieldset',
'#title' => t('Tab 2'),
'#collapsible' => TRUE,
'#group' => 'vertical_tabs1',
);
$form['tab2']['field2'] = array(
'#title' => t('Field 2'),
'#type' => 'textfield',
'#default_value' => 'field2',
);
$form['fieldset1'] = array(
'#type' => 'fieldset',
'#title' => t('Fieldset'),
);
$form['fieldset1']['field3'] = array(
'#type' => 'checkbox',
'#title' => t('Field 3'),
'#default_value' => TRUE,
);
$form['container'] = array(
'#type' => 'container',
);
$form['container']['field4'] = array(
'#type' => 'checkbox',
'#title' => t('Field 4'),
'#default_value' => TRUE,
);
$form['container']['subcontainer'] = array(
'#type' => 'container',
);
$form['container']['subcontainer']['field5'] = array(
'#type' => 'checkbox',
'#title' => t('Field 5'),
'#default_value' => TRUE,
);
$form['vertical_tabs2'] = array(
'#type' => 'vertical_tabs',
);
$form['tab3'] = array(
'#type' => 'fieldset',
'#title' => t('Tab 3'),
'#collapsible' => TRUE,
'#group' => 'vertical_tabs2',
);
$form['tab3']['field6'] = array(
'#title' => t('Field 6'),
'#type' => 'checkbox',
'#default_value' => TRUE,
);
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
if (empty($values['field1'])) {
$form_state->setErrorByName('tab1][field1', t('This checkbox inside a vertical tab does not have its default value.'));
}
if ($values['field2'] != 'field2') {
$form_state->setErrorByName('tab2][field2', t('This textfield inside a vertical tab does not have its default value.'));
}
if (empty($values['field3'])) {
$form_state->setErrorByName('fieldset][field3', t('This checkbox inside a fieldset does not have its default value.'));
}
if (empty($values['field4'])) {
$form_state->setErrorByName('container][field4', t('This checkbox inside a container does not have its default value.'));
}
if (empty($values['field5'])) {
$form_state->setErrorByName('container][subcontainer][field5', t('This checkbox inside a nested container does not have its default value.'));
}
if (empty($values['field5'])) {
$form_state->setErrorByName('tab3][field6', t('This checkbox inside a vertical tab whose fieldset access is allowed does not have its default value.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
drupal_set_message(t('The form submitted correctly.'));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment