function vertical_tabs_config_form_node_form_alter in Vertical Tabs Config 8
Same name and namespace in other branches
- 7 vertical_tabs_config.module \vertical_tabs_config_form_node_form_alter()
Implements hook_form_BASE_FORM_ID_alter().
Hide vertical tabs according to configuration.
File
- ./
vertical_tabs_config.module, line 16 - Vertical tabs config main file.
Code
function vertical_tabs_config_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$user = \Drupal::currentUser();
$roles = $user
->getRoles();
// 1. Vertical tabs visibility.
$config = vertical_tabs_config_get_config();
$current_path = \Drupal::service('path.current')
->getPath();
$path = explode('/', $current_path);
if ($path[1] == 'node' && ($path[2] == 'add' || $path[3] == 'edit')) {
// Maybe in $form there is the content_type like in D7.
$content_type = str_replace('node_', '', $form_id);
$content_type = str_replace('_edit_form', '', $content_type);
$content_type = str_replace('_form', '', $content_type);
if (!empty($config[$content_type])) {
// If restricted roles array is empty restrictions apply to all roles.
if (empty($config[$content_type]['roles']) || vertical_tabs_config_user_is_restricted_by_all_his_roles($config[$content_type]['roles'])) {
foreach ($config[$content_type] as $vt_machine_name => $hidden) {
if ($hidden == "1") {
hide($form[$vt_machine_name]);
}
}
}
else {
// Vertical tabs restriction for specific roles.
foreach ($roles as $rid_user) {
foreach ($config[$content_type]['roles'] as $rid_restricted) {
if ($rid_user == $rid_restricted) {
foreach ($config[$content_type] as $vt_machine_name => $hidden) {
if ($hidden == "1") {
hide($form[$vt_machine_name]);
}
}
}
}
}
}
}
}
// 2. Vertical tabs order.
$order = 0;
$vertical_tabs = vertical_tabs_config_vertical_tab_list();
foreach ($vertical_tabs as $vt_machine_name => $vt_human_name) {
// Important note: metatag vertical tab cannot be moved, it always appears
// on top if its weight is changed.
// Other vertical tabs position can't also be moved.
$excluded = [
'metatag',
];
if (isset($form[$vt_machine_name]) && !in_array($vt_machine_name, $excluded)) {
$vt_weight = \Drupal::config('vertical_tabs_config.order')
->get('vertical_tabs_config_' . $vt_machine_name);
$form[$vt_machine_name]['#weight'] = $vt_weight;
$order++;
}
}
}