You are here

better_local_tasks.module in Better Local Tasks 8

This is the module to make fancier local tasks.

File

better_local_tasks.module
View source
<?php

/**
 * @file
 * This is the module to make fancier local tasks.
 */

/**
 * Implements hook_page_attachments_alter().
 */
function better_local_tasks_page_attachments_alter(array &$attachments) {
  if (!\Drupal::currentUser()
    ->hasPermission('access contextual links')) {
    return;
  }
  $admin_context = \Drupal::service('router.admin_context');
  if (!$admin_context
    ->isAdminRoute()) {
    $attachments['#attached']['library'][] = 'better_local_tasks/local-tasks';
  }
}

/**
 * Implements hook_preprocess_menu_local_task().
 *
 * Add a css class to each local task tab.
 */
function better_local_tasks_preprocess_menu_local_task(&$variables) {
  $link_name = $variables['element']['#link']['title'];

  /* @var \Drupal\Core\Url $url */
  $url = $variables['element']['#link']['url'];
  $route_name = $url
    ->getRouteName();
  if (preg_match('/\\.canonical$/', $route_name)) {
    $link_name = 'view';
  }
  elseif (preg_match('/\\.edit_form$/', $route_name)) {
    $link_name = 'edit';
  }
  elseif (preg_match('/\\.delete_form$/', $route_name)) {
    $link_name = 'delete';
  }
  elseif (preg_match('/\\.version_history$/', $route_name)) {
    $link_name = 'revisions';
  }
  elseif (preg_match('/\\.devel_load$/', $route_name)) {
    $link_name = 'devel';
  }
  elseif (preg_match('/\\.content_translation_overview$/', $route_name)) {
    $link_name = 'translate';
  }
  elseif (preg_match('/\\.clone_form$/', $route_name)) {
    $link_name = 'clone';
  }
  elseif ($route_name = 'shortcut.set_switch') {
    $link_name = 'shortcuts';
  }
  $variables['link']['#attributes']['class'][] = $link_name;
}

/**
 * Implements hook_theme_registry_alter().
 *
 * Template overrides for local tasks.
 */
function better_local_tasks_theme_registry_alter(&$theme_registry) {
  $admin_context = \Drupal::service('router.admin_context');
  if (!$admin_context
    ->isAdminRoute()) {
    $theme_registry['block__local_tasks_block']['path'] = drupal_get_path('module', 'better_local_tasks') . '/templates/block';
    $theme_registry['menu_local_tasks']['path'] = drupal_get_path('module', 'better_local_tasks') . '/templates/navigation';
  }
}