'Contextual Tabs',
'description' => 'Configuration for Contextual Tabs',
'weight' => 0,
'page callback' => 'drupal_get_form',
'page arguments' => array('contextual_tabs_config_form'),
'access arguments' => array('administer site configuration'),
);
return $items;
}
/**
* Implements hook_ctools_plugin_directory().
*/
function contextual_tabs_ctools_plugin_directory($module, $plugin) {
return 'plugins/' . $plugin;
}
/**
* Configuration Form for Open Atrium
*/
function contextual_tabs_config_form($form, &$form_state) {
$form = array();
$form['contextual_tabs']['contextual_tabs_pages'] = array(
'#title' => t('URLs to ignore'),
'#type' => 'textarea',
'#default_value' => variable_get('contextual_tabs_pages', ''),
'#description' => t('List of URL patterns to be ignored. One per line.'),
);
$form['contextual_tabs']['contextual_tabs_extract'] = array(
'#title' => t('Tabs to extract'),
'#type' => 'textarea',
'#default_value' => variable_get('contextual_tabs_extract', ''),
'#description' => t('List of tab captions to extract as buttons, one per line. Case in-sensitive.
Can begin with an optional URL path filter followed by a colon. Example: "user:Shortcuts" would only extract the "Shortcuts" tab from the /user page.
To rename a tab, place the new caption after the normal caption separated by | character. For example: View|Cancel would change the View tab to a Cancel button.
To add an icon, separate the icon name from the caption with a comma. For example: Edit,icon_pencil'),
);
return system_settings_form($form);
}
/**
* Implements hook_theme()
*/
function contextual_tabs_theme() {
return array(
'contextual_tabs' => array(
'template' => 'contextual-tabs',
'arguments' => array(
'public' => NULL,
'links' => array(),
),
'path' => drupal_get_path('module', 'contextual_tabs'),
),
);
}
/**
* Helper function to create links for the normal tabs
*/
function _contextual_tabs_links($new_tabs_links = array()) {
static $tabs_links = NULL;
if (!isset($tabs_links)) {
$local_tasks = menu_local_tasks();
$tabs_links = array();
foreach($local_tasks['tabs']['output'] as $link) {
$tabs_links[] = $link['#link'];
}
}
if (count($new_tabs_links)) {
$tabs_links = $new_tabs_links;
}
drupal_alter('contextual_tabs_tasks', $tabs_links);
return $tabs_links;
}
/**
* Render the contextual tab buttons
*/
function contextual_tabs_render($conf = array()) {
$pages = variable_get('contextual_tabs_pages', '');
$current = current_path();
$output = '';
if (!drupal_match_path($current, $pages)) {
$tabs_links = _contextual_tabs_links();
$current_link = NULL;
$links = array();
$link_by_name = array();
$extract = explode("\n", variable_get('contextual_tabs_extract', ''));
foreach ($tabs_links as $id => $link) {
if ($link['href'] != $current) {
$key = 'link-' . $id;
$links[$key] = l($link['title'], $link['href']);
$link_by_name[strtolower($link['title'])] = $id;
}
}
$buttons = array();
$processed = array();
foreach ($extract as $item) {
$caption = $item;
$icon = '';
$url = '';
$new_name = '';
if (strpos($caption, ':')) {
list($url, $caption) = explode(':', $caption);
}
if (strpos($caption, ',')) {
list($caption, $icon) = explode(',', $caption);
}
if (strpos($caption, '|')) {
list($caption, $new_name) = explode('|', check_plain($caption));
}
if (empty($new_name)) {
$new_name = trim($caption);
}
$caption = strtolower(trim($caption));
$url = trim($url);
if (!in_array($caption, $processed) && isset($link_by_name[$caption]) &&
(!empty($icon) || empty($conf['hide_captions'])) &&
(empty($url) || drupal_match_path($current, $url))) {
$id = $link_by_name[$caption];
$key = 'link-' . $id;
$buttons[] = array(
'title' => $new_name,
'link' => url($tabs_links[$id]['href']),
'icon' => isset($icon) ? $icon : '',
);
unset($links[$key]);
$processed[] = $caption; // first match wins
}
}
// allow other modules to modify the buttons and/or links
$data = array(
'buttons' => $buttons,
'links' => $links,
);
drupal_alter('contextual_tabs', $data);
if (!empty($conf['prevent_alter'])) {
$data['buttons'] = $buttons;
}
$vars = array(
'title' => t('Configure'),
'buttons' => $data['buttons'],
'alignment' => !empty($conf['alignment']) ? 'align-right' : '',
'show_captions' => !empty($conf['hide_captions']) ? FALSE : TRUE,
'direction' => !empty($conf['direction']) ? 'dropup' : '',
'menu_caption' => !empty($conf['menu_caption']) ? check_plain($conf['menu_caption']) : t('Configure'),
'icon_class' => !empty($conf['icon_class']) ? check_plain($conf['icon_class']) : 'icon-cog',
'btn_class' => !empty($conf['btn_class']) ? check_plain($conf['btn_class']) : 'btn-small btn-sm btn-default',
);
// remove links with empty data
foreach ($data['links'] as $key => $value) {
if (is_array($value) && array_key_exists('data', $value) && empty($value['data'])) {
unset($data['links'][$key]);
}
}
if (!empty($data['links'])) {
$vars['links'] = theme('item_list', array(
'items' => $data['links'],
'type' => 'ul',
));
}
$output = theme('contextual_tabs', $vars);
}
return $output;
}
/**
* Implements hook_preprocess_page().
*/
function contextual_tabs_preprocess_page(&$variables) {
$tabs = contextual_tabs_render();
if (!empty($tabs)) {
unset($variables['tabs']['#primary']);
}
$variables['primarytabs'] = $tabs;
}