admin_local_tasks-1.0.0-rc1/admin_local_tasks.module
admin_local_tasks.module
<?php
/**
* @file
* This is the module to make fancier local tasks.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Html;
/**
* Implements hook_help().
*/
function admin_local_tasks_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.admin_local_tasks':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This module make Drupal (Admin) Local Task links fancier and more accessible for content editors on non-admin routes (front pages) - with minimalistic design and fixed position on left.') . '</p>';
$output .= '<h3>' . t('Supporting organizations:') . '</h3>';
$output .= '<p>' . t('<a href="https://www.drupal.org/improve-group">iMprove GROUP</a> - Funded Development') . '</p>';
$output .= '<p>' . t('<a href="https://www.drupal.org/previon-plus-ag">Previon Plus AG</a> - Funded Development') . '</p>';
return $output;
}
}
/**
* Implements hook_page_attachments_alter().
*/
function admin_local_tasks_page_attachments_alter(array &$attachments) {
/** @var \Drupal\Core\Routing\AdminContext $admin_context */
$admin_context = \Drupal::service('router.admin_context');
/** @var \Drupal\Core\Session\AccountInterface $current_user */
$current_user = \Drupal::currentUser();
if (!$admin_context->isAdminRoute() && !$current_user->isAnonymous()) {
$attachments['#attached']['library'][] = 'admin_local_tasks/base';
}
}
/**
* Implements hook_preprocess_preprocess_block().
*/
function admin_local_tasks_preprocess_block__local_tasks_block(&$variables) {
/** @var \Drupal\Core\Config\Config $config */
$config = \Drupal::config('admin_local_tasks.settings');
$variables['attributes']['class'][] = 'position-' . ($config->get('position') ?: 'left');
if ($config->get('tooltips')) {
$variables['attributes']['class'][] = 'tooltips';
}
$variables['#cache']['tags'][] = 'admin_local_tasks:settings';
}
/**
* Implements hook_preprocess_menu_local_tasks().
*/
function admin_local_tasks_preprocess_menu_local_tasks(&$variables) {
/** @var \Drupal\Core\Routing\AdminContext $admin_context */
$admin_context = \Drupal::service('router.admin_context');
/** @var \Drupal\Core\Config\Config $theme */
$theme = \Drupal::config('system.theme');
// Get active theme to switch template classes.
$variables['default_theme'] = $theme->get('default');
$variables['admin_theme'] = $theme->get('admin');
// Check if current route is admin.
$variables['is_admin_route'] = $admin_context->isAdminRoute();
}
/**
* Implements hook_preprocess_menu_local_task().
*
* Add a css class and icon to each local task tab.
*/
function admin_local_tasks_preprocess_menu_local_task(&$variables) {
/** @var \Drupal\Core\Extension\ModuleHandler $module_handler */
$module_handler = \Drupal::moduleHandler();
$link = $variables['element']['#link'];
$route_name = $link['url']->getRouteName();
$mappings = [
'canonical' => 'view',
'edit_form' => 'edit',
'delete_form' => 'delete',
];
// Allow modules to alter mappings.
$module_handler->alter('local_tasks_mapping', $mappings);
// Transform link title to class.
$class = Html::getClass($link['title']);
foreach ($mappings as $key => $value) {
if (preg_match('/\.' . $key . '$/', $route_name)) {
$class = $value;
}
}
$variables['link']['#options']['attributes']['class'][] = $class;
$variables['link']['#options']['attributes']['data-tippy-content'] = $link['title'];
$variables['link']['#title'] = t('@title @icon', [
'@title' => $link['title'],
'@icon' => new FormattableMarkup('<span class="icon"></span>', []),
]);
}
/**
* Implements hook_theme_registry_alter().
*
* Template overrides for local tasks.
*/
function admin_local_tasks_theme_registry_alter(&$theme_registry) {
/** @var \Drupal\Core\Extension\ModuleExtensionList $extension */
$extension = \Drupal::service('extension.list.module');
$path = $extension->getPath('admin_local_tasks');
if (isset($theme_registry['menu_local_tasks'])) {
$theme_registry['menu_local_tasks']['path'] = $path . '/templates/navigation';
}
}
/**
* Implements hook_local_tasks_mapping_alter().
*
* Extend routing mappings.
*/
function admin_local_tasks_local_tasks_mapping_alter(array &$mapping) {
$mapping['user.edit_form'] = 'user';
$mapping['version_history'] = 'revisions';
$mapping['devel_load'] = 'settings';
$mapping['content_translation_overview'] = 'translate';
$mapping['config_translation_overview'] = 'translate';
$mapping['scheduler_scheduled_content.user_page'] = 'scheduled';
$mapping['clone_form'] = 'clone';
$mapping['backlinks'] = 'linkplus';
$mapping['set_switch'] = 'shortcuts';
$mapping['webform_submission.user'] = 'submissions';
$mapping['webform.test_form'] = 'test';
$mapping['webform.results_submissions'] = 'list';
$mapping['webform.settings'] = 'settings';
$mapping['webform.export_form'] = 'export';
}
