vertical_tabs_config.module in Vertical Tabs Config 7
Same filename and directory in other branches
Vertical tabs config main file.
File
vertical_tabs_config.moduleView source
<?php
/**
* @file
* Vertical tabs config main file.
*/
/**
* Implements hook_menu().
*/
function vertical_tabs_config_menu() {
$items = array();
$items['admin/config/user-interface/vertical_tabs_config'] = array(
'title' => 'Vertical Tabs Config',
'description' => 'Change vertical tabs configuration',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'vertical_tabs_config_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'vertical_tabs_config.admin.inc',
);
$items['admin/config/user-interface/vertical_tabs_config/visibility'] = array(
'title' => 'Vertical tabs visibility',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
);
$items['admin/config/user-interface/vertical_tabs_config/order'] = array(
'title' => 'Vertical tabs order',
'type' => MENU_LOCAL_TASK,
'description' => 'Change vertical tabs order',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'vertical_tabs_config_admin_settings_order',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'vertical_tabs_config.admin.inc',
'weight' => 1,
);
return $items;
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*
* Hide vertical tabs according to configuration.
*/
function vertical_tabs_config_form_node_form_alter(&$form, &$form_state, $form_id) {
global $user;
// 1. Vertical tabs visibility.
$config = vertical_tabs_config_get_config();
if (preg_match('/_node_form$/', $form_id) == 1) {
$content_type = $form['type']['#value'];
if (isset($config[$content_type]) && is_array($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") {
if ($vt_machine_name == 'options') {
// To avoid problem with node publishing, see #2852158.
$form['options']['#attributes']['style'][] = 'display:none;';
}
else {
hide($form[$vt_machine_name]);
}
}
}
}
else {
// Vertical tabs restriction for specific roles.
foreach ($user->roles as $rid_user => $value) {
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") {
if ($vt_machine_name == 'options') {
// To avoid problem with node publishing, see #2852158.
$form['options']['#attributes']['style'][] = 'display:none;';
}
else {
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) {
// Importante 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 = array(
'metatag',
);
if (isset($form[$vt_machine_name]) && !in_array($vt_machine_name, $excluded)) {
$form[$vt_machine_name]['#weight'] = variable_get('vertical_tabs_config_' . $vt_machine_name);
$order++;
}
}
}
/**
* Checks if all user is restricted by configuration given all of his roles.
*
* @param array $restricted_roles
* List of all restricted roles.
*
* @return bool result
* Indicates if user is restricted given all his roles.
*/
function vertical_tabs_config_user_is_restricted_by_all_his_roles(array $restricted_roles) {
global $user;
$restricted_counter = 0;
foreach ($user->roles as $rid_user => $value) {
foreach ($restricted_roles as $rid_restricted) {
if ($rid_user == $rid_restricted) {
$restricted_counter++;
}
}
}
if ($restricted_counter == count($user->roles)) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Returns a list of all vertical tabs.
*
* @param bool $ordered_by_weight Return a list ordered by weight?
*
* @return array List of vertical tabs
*/
function vertical_tabs_config_vertical_tab_list($ordered_by_weight = FALSE) {
$vertical_tabs = array(
'options' => t('Publishing options'),
'menu' => t('Menu settings'),
'revision_information' => t('Revision information'),
'path' => t('Url path settings'),
'comment_settings' => t('Comment settings'),
'author' => t('Author information'),
'xmlsitemap' => t('XML sitemap'),
'metatags' => t('Meta tags'),
'custom_breadcrumbs' => t('Custom Breadcrumbs'),
'redirect' => t('URL redirects'),
'panelizer' => t('Panelizer') . '/' . t('Customize display'),
'book' => t('Book outline'),
);
if ($ordered_by_weight) {
$order = 0;
$unordered = array();
foreach ($vertical_tabs as $vt_machine_name => $vt_human_name) {
$weight = variable_get('vertical_tabs_config_' . $vt_machine_name, $order);
$unordered[$weight] = array(
'key' => $vt_machine_name,
'value' => $vt_human_name,
);
$order++;
}
ksort($unordered);
$vertical_tabs = array();
foreach ($unordered as $vt_assoc) {
$vertical_tabs[$vt_assoc['key']] = $vt_assoc['value'];
}
}
return $vertical_tabs;
}
/**
* Returns module visibility configuration or -1.
*/
function vertical_tabs_config_get_config() {
try {
$query = db_select('vertical_tabs_config', 'n');
$query
->fields('n', array(
'id',
'vertical_tab',
'content_type',
'roles',
'hidden',
));
$result = $query
->execute()
->fetchAll();
} catch (Exception $e) {
watchdog('vertical_tabs_config', $e
->getMessage());
return "-1";
}
if (isset($result) && is_array($result)) {
$conf_hash = array();
foreach ($result as $obj) {
$conf_hash[$obj->content_type][$obj->vertical_tab] = $obj->hidden;
if (!isset($conf_hash[$obj->content_type]['roles'])) {
$conf_hash[$obj->content_type]['roles'] = json_decode($obj->roles);
}
}
return $conf_hash;
}
else {
return "-1";
}
}
/**
* Save all visibility configuration.
*
* @param array $config
* The array ready to save to database.
*/
function vertical_tabs_config_save_config(array $config) {
$query = db_insert('vertical_tabs_config')
->fields(array(
'vertical_tab',
'content_type',
'roles',
'hidden',
));
foreach ($config as $record) {
$query
->values($record);
}
try {
db_delete('vertical_tabs_config')
->execute();
$query
->execute();
} catch (Exception $e) {
watchdog('vertical_tabs_config', $e
->getMessage());
}
}
/**
* Implements hook_theme().
*/
function vertical_tabs_config_theme($existing, $type, $theme, $path) {
$themes = array(
'table_drag_components' => array(
'render element' => 'element',
),
);
return $themes;
}
/**
* Rendering of draggable table.
*/
function theme_table_drag_components($vars) {
$element = $vars['element'];
drupal_add_tabledrag('vt_order_table', 'order', 'sibling', 'item-row-weight');
$header = array(
'label' => t('Vertical tab'),
'weight' => t('Weight'),
);
$rows = array();
foreach (element_children($element) as $key) {
$row = array();
$row['data'] = array();
foreach ($header as $fieldname => $title) {
$row['data'][] = drupal_render($element[$key][$fieldname]);
$row['class'] = array(
'draggable',
);
}
$rows[] = $row;
}
return theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array(
'id' => 'vt_order_table',
),
));
}
Functions
Name | Description |
---|---|
theme_table_drag_components | Rendering of draggable table. |
vertical_tabs_config_form_node_form_alter | Implements hook_form_BASE_FORM_ID_alter(). |
vertical_tabs_config_get_config | Returns module visibility configuration or -1. |
vertical_tabs_config_menu | Implements hook_menu(). |
vertical_tabs_config_save_config | Save all visibility configuration. |
vertical_tabs_config_theme | Implements hook_theme(). |
vertical_tabs_config_user_is_restricted_by_all_his_roles | Checks if all user is restricted by configuration given all of his roles. |
vertical_tabs_config_vertical_tab_list | Returns a list of all vertical tabs. |