diff --git a/core/includes/form.inc b/core/includes/form.inc
index 28a8847c9c2cbc6298eafd1476ced0bbceed9f4b..39203d2bce5511b44791ce8052bc9cce8945d17e 100644
--- a/core/includes/form.inc
+++ b/core/includes/form.inc
@@ -3815,11 +3815,25 @@ function form_process_vertical_tabs($element, &$form_state) {
*/
function theme_vertical_tabs($variables) {
$element = $variables['element'];
- // Add required JavaScript and Stylesheet.
- drupal_add_library('system', 'drupal.vertical-tabs');
+ // Even if there are no tabs the element will still have a child element for
+ // the active tab. We need to iterate over the tabs to ascertain if any
+ // are visible before showing the wrapper and h2.
+ $visible_tab = FALSE;
+ $output = '';
+ foreach (element_children($element['group']) as $tab_index) {
+ if (!isset($element['group'][$tab_index]['#access']) ||
+ !empty($element['group'][$tab_index]['#access'])) {
+ $visible_tab = TRUE;
+ break;
+ }
+ }
+ if ($visible_tab) {
+ // Add required JavaScript and Stylesheet.
+ drupal_add_library('system', 'drupal.vertical-tabs');
- $output = '
' . t('Vertical Tabs') . '
';
- $output .= '' . $element['#children'] . '
';
+ $output = '' . t('Vertical Tabs') . '
';
+ $output .= '' . $element['#children'] . '
';
+ }
return $output;
}
diff --git a/core/modules/system/lib/Drupal/system/Tests/Form/ElementsVerticalTabsTest.php b/core/modules/system/lib/Drupal/system/Tests/Form/ElementsVerticalTabsTest.php
index 9c16ec32a768c66ac13da9ef819c0ea90707a350..dd960228fc50c2de71ca4ef2ea41a8458787f37b 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Form/ElementsVerticalTabsTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Form/ElementsVerticalTabsTest.php
@@ -29,6 +29,14 @@ public static function getInfo() {
);
}
+ function setUp() {
+ parent::setUp();
+
+ $this->admin_user = $this->drupalCreateUser(array('access vertical_tab_test tabs'));
+ $this->web_user = $this->drupalCreateUser();
+ $this->drupalLogin($this->admin_user);
+ }
+
/**
* Ensures that vertical-tabs.js is included before collapse.js.
*
@@ -40,4 +48,20 @@ function testJavaScriptOrdering() {
$position2 = strpos($this->content, 'core/misc/collapse.js');
$this->assertTrue($position1 !== FALSE && $position2 !== FALSE && $position1 < $position2, t('vertical-tabs.js is included before collapse.js'));
}
+
+ /**
+ * Ensures that vertical tab markup is not shown if user has no tab access.
+ */
+ function testWrapperNotShownWhenEmpty() {
+ // Test admin user can see vertical tabs and wrapper.
+ $this->drupalGet('form_test/vertical-tabs');
+ $wrapper = $this->xpath("//div[@class='vertical-tabs-panes']");
+ $this->assertTrue(isset($wrapper[0]), 'Vertical tab panes found.');
+
+ // Test wrapper markup not present for non-privileged web user.
+ $this->drupalLogin($this->web_user);
+ $this->drupalGet('form_test/vertical-tabs');
+ $wrapper = $this->xpath("//div[@class='vertical-tabs-panes']");
+ $this->assertFalse(isset($wrapper[0]), 'Vertical tab wrappers are not displayed to unprivileged users.');
+ }
}
diff --git a/core/modules/system/tests/modules/form_test/form_test.module b/core/modules/system/tests/modules/form_test/form_test.module
index 3c322a6ce045c379aa0b74644bda35628ad18842..3cdb74692b431ed145be37a6ba24a437cf79c710 100644
--- a/core/modules/system/tests/modules/form_test/form_test.module
+++ b/core/modules/system/tests/modules/form_test/form_test.module
@@ -309,6 +309,19 @@ function form_test_menu() {
return $items;
}
+
+/**
+ * Implements hook_permission().
+ */
+function form_test_permission() {
+ $perms = array(
+ 'access vertical_tab_test tabs' => array(
+ 'title' => t('Access vertical_tab_test tabs'),
+ ),
+ );
+ return $perms;
+}
+
/**
* Form submit handler to return form values as JSON.
*/
@@ -798,6 +811,7 @@ function _form_test_vertical_tabs_form($form, &$form_state) {
'#title' => t('Tab 1'),
'#collapsible' => TRUE,
'#group' => 'vertical_tabs',
+ '#access' => user_access('access vertical_tab_test tabs')
);
$form['tab1']['field1'] = array(
'#title' => t('Field 1'),
@@ -808,6 +822,7 @@ function _form_test_vertical_tabs_form($form, &$form_state) {
'#title' => t('Tab 2'),
'#collapsible' => TRUE,
'#group' => 'vertical_tabs',
+ '#access' => user_access('access vertical_tab_test tabs')
);
$form['tab2']['field2'] = array(
'#title' => t('Field 2'),