public static function WebformFormHelper::buildTabs in Webform 8.5
Same name and namespace in other branches
- 6.x src/Utility/WebformFormHelper.php \Drupal\webform\Utility\WebformFormHelper::buildTabs()
Build form jQuery UI tabs.
Parameters
array $form: A form.
array $tabs: An associative array contain tabs.
string $active_tab: The active tab name.
Return value
array The form with tabs.
See also
\Drupal\webform\Form\WebformHandlerFormBase::buildForm
\Drupal\webform\Plugin\WebformElementBase::buildConfigurationFormTabs
3 calls to WebformFormHelper::buildTabs()
- WebformElementBase::buildConfigurationFormTabs in src/
Plugin/ WebformElementBase.php - Build configuration form tabs.
- WebformHandlerFormBase::buildForm in src/
Form/ WebformHandlerFormBase.php - Form constructor.
- WebformVariantFormBase::buildForm in src/
Form/ WebformVariantFormBase.php - Form constructor.
File
- src/
Utility/ WebformFormHelper.php, line 53
Class
- WebformFormHelper
- Helper class webform based methods.
Namespace
Drupal\webform\UtilityCode
public static function buildTabs(array $form, array $tabs, $active_tab = '') {
// Allow tabs to be disabled via $form['#tab'] = FALSE.
if (isset($form['#tabs']) && $form['#tabs'] === FALSE) {
return $form;
}
// Determine if the form has nested (configuration) settings.
// Used by WebformHandlers.
$has_settings = isset($form['settings']) && !empty($form['settings']['#tree']);
// Always include general tab.
$tabs = [
'general' => [
'title' => t('General'),
'elements' => [],
'weight' => 0,
],
] + $tabs;
// Sort tabs by weight.
uasort($tabs, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
// Assign tabs to elements.
foreach ($tabs as $tab_name => $tab) {
foreach ($tab['elements'] as $element_key) {
if ($has_settings && isset($form['settings'][$element_key])) {
$form['settings'][$element_key]['#group'] = 'tab_' . $tab_name;
$tabs[$tab_name]['has_tabs'] = TRUE;
}
elseif (isset($form[$element_key])) {
$form[$element_key]['#group'] = 'tab_' . $tab_name;
$tabs[$tab_name]['has_tabs'] = TRUE;
}
}
}
// Set default general tab for settings.
if ($has_settings) {
foreach (Element::children($form['settings']) as $element_key) {
if (!isset($form['settings'][$element_key]['#group'])) {
$form['settings'][$element_key]['#group'] = 'tab_general';
$tabs['general']['has_tabs'] = TRUE;
}
}
$form['settings']['#group'] = FALSE;
}
// Set default general tab for all other elements.
foreach (Element::children($form) as $element_key) {
if (!isset($form[$element_key]['#group'])) {
$form[$element_key]['#group'] = 'tab_general';
$tabs['general']['has_tabs'] = TRUE;
}
}
// Build tabs.
$tab_items = [];
$index = 0;
foreach ($tabs as $tab_name => $tab) {
// Skip empty tab.
if (empty($tab['has_tabs'])) {
continue;
}
$tab_items[] = [
'#type' => 'link',
'#url' => Url::fromRoute('<none>', [], [
'fragment' => 'webform-tab--' . $tab_name,
]),
'#title' => $tab['title'],
'#attributes' => [
'class' => [
'webform-tab',
],
'data-tab-index' => $index++,
],
];
$form['tab_' . $tab_name] = [
'#type' => 'container',
'#group' => 'tabs',
'#attributes' => [
'id' => 'webform-tab--' . $tab_name,
'class' => [
'webform-tab',
],
],
];
}
// Add tabs.
$form['tabs'] = [
'#weight' => -1000,
'#type' => 'container',
'#attributes' => [
'class' => [
'webform-tabs',
],
],
'#attached' => [
'library' => [
'webform/webform.form.tabs',
],
],
];
if ($active_tab) {
$form['tabs']['#attributes']['data-tab-active'] = 'webform-tab--' . $active_tab;
}
$form['tabs']['items'] = [
'#theme' => 'item_list',
'#items' => $tab_items,
];
return $form;
}