content_dashboard-1.0.3/content_dashboard.module
content_dashboard.module
<?php
/**
* @file
* Editor dashboard module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeTypeInterface;
use Drupal\Core\Url;
use Drupal\node\Entity\NodeType;
use Drupal\media\Entity\MediaType;
/**
* Implements hook_theme().
*/
function content_dashboard_theme() {
return [
'content_dashboard' => [
'variables' => [
'items_content' => NULL,
'items_medias' => NULL,
'items_others' => NULL,
'roles_defined' => NULL,
],
'template' => 'dashboard',
'render element' => 'children',
],
];
}
/**
* Implements hook_toolbar().
*/
function content_dashboard_toolbar() {
$current_user = \Drupal::currentUser();
// Create the dashboard toolbar render array.
$items = [];
if ($current_user->hasPermission('access content dashboard')) {
$items['dashboard'] = [
'#type' => 'toolbar_item',
'tab' => [
'#type' => 'link',
'#title' => t('My Dashboard'),
'#options' => [
'set_active_class' => TRUE,
],
'#url' => Url::fromRoute('content_dashboard.dashboard'),
'#attributes' => [
'title' => t('Dashboard'),
'class' => ['toolbar-custom-dashboard', 'toolbar-item'],
],
],
'#weight' => -99,
'#attached' => [
'library' => [
'content_dashboard/dashboard',
],
],
];
}
return $items;
}
/**
* Implements hook_preprocess_menu_local_action().
*
* Override link and title of the "add content" button.
*/
function content_dashboard_preprocess_menu_local_action(&$variables) {
// Checking the current view route.
$route_match = \Drupal::routeMatch()->getRouteName();
if ($route_match !== 'system.admin_content' && $route_match !== 'entity.media.collection') {
return;
}
$current_user = \Drupal::currentUser();
if (!$current_user->hasPermission('access editor dashboard')) {
return;
}
$link = $variables['element']['#link'];
$link['localized_options']['attributes']['class'][] = 'button button--action button--primary';
$link['localized_options']['set_active_class'] = TRUE;
if ($route_match === 'system.admin_content') {
$node_type = \Drupal::request()->query->get('type');
if (!$node_type) {
return;
}
$nodeType = NodeType::load($node_type);
if ($nodeType instanceof NodeTypeInterface) {
$node_type_label = $nodeType->label();
}
else {
$node_type_label = t('content');
}
$url = Url::fromRoute('node.add', ['node_type' => $node_type]);
$variables['link'] = [
'#type' => 'link',
'#title' => t('Add') . ' ' . $node_type_label,
'#options' => $link['localized_options'],
'#url' => $url,
];
}
elseif ($route_match === 'entity.media.collection') {
$media_type = \Drupal::request()->query->get('type');
if (!$media_type) {
return;
}
$mediaType = MediaType::load($media_type);
if ($mediaType) {
$media_type_label = $mediaType->label();
}
else {
$media_type_label = t('media');
}
$url = $url = Url::fromUri('internal:/media/add/' . $media_type);
$variables['link'] = [
'#type' => 'link',
'#title' => t('Add') . ' ' . $media_type_label,
'#options' => $link['localized_options'],
'#url' => $url,
];
}
// Disable caching for this link.
$variables['link']['#cache']['max-age'] = 0;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function content_dashboard_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Add a custom submission handler.
$form['#submit'][] = 'content_dashboard_user_login_redirect';
}
/**
* Custom submit handler to redirect specific roles.
*/
function content_dashboard_user_login_redirect($form, FormStateInterface $form_state) {
// Retrieve the currently logging in user.
$account = \Drupal::currentUser();
// Check if the user has the permission to access admin dashboard.
if ($account->hasPermission('access admin dashboard')) {
// Set the redirection to the custom dashboard.
$form_state->setRedirect('content_dashboard.dashboard');
}
}
