quadstat_misc-8.x-1.x-dev/quadstat_misc.module
quadstat_misc.module
<?php
use \Drupal\Core\Url;
use \Drupal\Core\Path\PathMatcher;
use \Drupal\Core\Entity;
function quadstat_misc_preprocess_html(&$variables) {
// Add node ID
if ($node = \Drupal::request()->attributes->get('node')) {
$variables['attributes']['class'][] = 'page-node-' . $node->id();
}
// Get route
$route_name = \Drupal::routeMatch()->getRouteName();
// Add edit class
if ($route_name == 'entity.node.edit_form') {
$variables['attributes']['class'][] = 'entity-edit';
}
// Add create class
elseif ($route_name == 'node.add') {
$variables['attributes']['class'][] = 'entity-create';
}
}
function quadstat_misc_menu_local_tasks_alter(&$data, $route_name) {
// Add a tab linking to the frontpage to all pages
$is_front = \Drupal::service('path.matcher')->isFrontPage();
// Make tab inactive on its page; only show to authenticated users
$current_path = \Drupal::service('path.current')->getPath();
$alias = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
$user = \Drupal::currentUser();
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
// You can get nid and anything else you need from the node object.
$nid = $node->id();
}
$data['tabs'][0]['custom.home'] = array(
'#theme' => 'menu_local_task',
'#link' => array(
'title' => t('Home'),
'url' => Url::fromUri('//www.quadstat.net'),
'localized_options' => array(
'attributes' => array(
'title' => t('Quadstat'),
'class' => ['quadstat-menu-icon', 'fa-home'],
),
),
),
'#weight' => -50,
'#active' => $current_path == '/' ? TRUE : FALSE,
);
$data['tabs'][0]['custom.dashboard'] = array(
'#theme' => 'menu_local_task',
'#link' => array(
'title' => t('Dash'),
'url' => Url::fromUserInput('/statistics'),
'localized_options' => array(
'attributes' => array(
'title' => t('Statistics Dashboard'),
'class' => ['quadstat-menu-icon', 'fa-dashboard'],
),
),
),
'#weight' => -50,
'#active' => $current_path == '/node/3365' ? TRUE : FALSE,
);
$data['tabs'][0]['custom.add_dataset'] = array(
'#theme' => 'menu_local_task',
'#link' => array(
'title' => t('Create Dataset'),
'url' => Url::fromUserInput('/node/add/dataset'),
'localized_options' => array(
'attributes' => array(
'title' => t('Import or create a new dataset on Quadstat.com.'),
'class' => ['quadstat-menu-icon', 'fa-navicon'],
),
),
),
'#weight' => -49,
'#active' => $current_path == '/node/add/dataset' ? TRUE : FALSE,
);
if ($user->id() == 0) {
$data['tabs'][0]['custom.login'] = array(
'#theme' => 'menu_local_task',
'#link' => array(
'title' => t('Login'),
'url' => Url::fromUserInput('/user/login'),
'localized_options' => array(
'attributes' => array(
'title' => t('Login to Quadstat'),
'class' => ['quadstat-menu-icon', 'fa-sign-in'],
),
),
),
'#weight' => 49,
'#active' => $current_path == '/user/login' ? TRUE : FALSE,
);
}
if($user->id()) {
$data['tabs'][0]['custom.logout'] = array(
'#theme' => 'menu_local_task',
'#link' => array(
'title' => t('Logout'),
'url' => Url::fromUserInput('/user/logout'),
'localized_options' => array(
'attributes' => array(
'title' => t('Logout of Quadstat.com.'),
'class' => ['quadstat-menu-icon', 'fa-user-circle-o'],
),
),
),
'#weight' => 50,
'#active' => FALSE,
);
$data['tabs'][0]['custom.profile'] = array(
'#theme' => 'menu_local_task',
'#link' => array(
'title' => t('Profile'),
'url' => Url::fromUserInput('/user/' . $user->id() . '/edit'),
'localized_options' => array(
'attributes' => array(
'title' => t('Edit your Quadstat.com Profile.'),
'class' => ['quadstat-menu-icon', 'fa-id-card-o'],
),
),
),
'#weight' => 48,
'#active' => FALSE,
);
} else if ($user->id() == 0 && strpos($current_path, '/user') === 0) {
// Menu for anonymous user
$data['tabs'][0]['custom.login'] = array(
'#theme' => 'menu_local_task',
'#link' => array(
'title' => t('Login'),
'url' => Url::fromUserInput('/user/login'),
'localized_options' => array(
'attributes' => array(
'title' => t('Login to Quadstat'),
'class' => ['quadstat-menu-icon', 'fa-sign-in'],
),
),
),
'#weight' => 49,
'#active' => $current_path == '/user/login' ? TRUE : FALSE,
);
$data['tabs'][0]['custom.register'] = array(
'#theme' => 'menu_local_task',
'#link' => array(
'title' => t('Register'),
'url' => Url::fromUserInput('/user/register'),
'localized_options' => array(
'attributes' => array(
'title' => t('Register with Quadstat'),
'class' => ['quadstat-menu-icon', 'fa-user-circle'],
),
),
),
'#weight' => 49,
'#active' => $current_path == '/user/register' ? TRUE : FALSE,
);
$data['tabs'][0]['custom.password'] = array(
'#theme' => 'menu_local_task',
'#link' => array(
'title' => t('Reset Password'),
'url' => Url::fromUserInput('/user/password'),
'localized_options' => array(
'attributes' => array(
'title' => t('Reset your password'),
'class' => ['quadstat-menu-icon', 'fa-life-buoy'],
),
),
),
'#weight' => 49,
'#active' => $current_path == '/user/password' ? TRUE : FALSE,
);
}
if ($user->id() > 0) {
$data['tabs'][0]['custom.logout'] = array(
'#theme' => 'menu_local_task',
'#link' => array(
'title' => t('Logout'),
'url' => Url::fromUserInput('/user/logout'),
'localized_options' => array(
'attributes' => array(
'title' => t('Logout Quadstat'),
'class' => ['quadstat-menu-icon', 'fa-sign-out'],
),
),
),
'#weight' => 49,
'#active' => $current_path == '/user/logout' ? TRUE : FALSE,
);
}
if (is_object($node) && $node->getType() == 'dataset' && $node->access('update', $user, FALSE)) {
$data['tabs'][0]['custom.dataset_edit'] = array(
'#theme' => 'menu_local_task',
'#link' => array(
'title' => t('Edit'),
'url' => Url::fromUserInput("/node/$nid/edit"),
'localized_options' => array(
'attributes' => array(
'title' => t('Edit this dataset'),
'class' => ['quadstat-menu-icon', 'fa-pencil-square-o'],
),
),
),
'#weight' => -50,
'#active' => strpos($alias, '/dataset/') !== 0,
);
}
if (is_object($node) && $node->getType() == 'dataset') {
$data['tabs'][0]['custom.dataview'] = array(
'#theme' => 'menu_local_task',
'#link' => array(
'title' => t('View'),
'url' => Url::fromUserInput(str_replace('/edit', '', $current_path)),
'localized_options' => array(
'attributes' => array(
'title' => t('View this dataset'),
'class' => ['quadstat-menu-icon', 'fa-eye'],
),
),
),
'#weight' => -50,
'#active' => strpos($alias, '/dataset/') === 0,
);
}
$data['tabs'][0]['custom.tour'] = array(
'#theme' => 'menu_local_task',
'#link' => array(
'title' => t('Tour Page'),
'url' => Url::fromUserInput('/Guided-Tour'),
'localized_options' => array(
'attributes' => array(
'title' => t('Tour Page'),
'class' => ['quadstat-menu-icon', 'fa-plane'],
),
),
),
'#weight' => -44,
'#active' => FALSE,
);
$data['tabs'][0]['custom.contact'] = array(
'#theme' => 'menu_local_task',
'#link' => array(
'title' => t('Contact'),
'url' => Url::fromUserInput('/contact'),
'localized_options' => array(
'attributes' => array(
'title' => t('Contact North Penn Networks'),
'class' => ['quadstat-menu-icon', 'fa-mail-reply'],
),
),
),
'#weight' => 47,
'#active' => $current_path == '/contact' ? TRUE : FALSE,
);
// Hide the 'View' tab on front page
if($is_front || $current_path == '/node/3365') {
unset($data['tabs'][0]['entity.node.canonical']);
}
if (strpos($alias, '/dataset/') === FALSE && strpos($alias, '/app/') !== FALSE && strpos($alias, '/dash') !== FALSE || (strpos($current_path, '/user') !== FALSE)) {
unset($data['tabs'][0]['custom.tour']);
}
if(strpos($current_path, '/user') === 0 || strpos($current_path, '/contact') === 0) {
unset($data['tabs'][0]['custom.add_dataset']);
unset($data['tabs'][0]['custom.tour']);
}
if(strpos($current_path, '/user') === 0) {
unset($data['tabs'][0]['custom.contact']);
}
if(strpos($current_path, '/user/reset') === 0) {
unset($data['tabs'][0]['node.front']);
unset($data['tabs'][0]['custom.logout']);
unset($data['tabs'][0]['custom.add_dataset']);
}
// Remove 'Results' Menu Link
unset($data['tabs'][0]['entity.node.webform.results']);
unset($data['tabs'][0]['user.logout']);
unset($data['tabs'][0]['user.pass']);
unset($data['tabs'][0]['user.login']);
unset($data['tabs'][0]['user.register']);
unset($data['tabs'][0]['entity.user.edit_form']);
unset($data['tabs'][0]['entity.user.canonical']);
if (is_object($node) && ($node->getType() == 'dataset' || $node->getType() == 'output')) {
unset($data['tabs'][0]['entity.node.canonical']);
unset($data['tabs'][0]['entity.node.delete_form']);
unset($data['tabs'][0]['entity.node.edit_form']);
}
}
/**
* Implements hook_page_attachments() to attach CSS and JS to specific pages
*/
function quadstat_misc_page_attachments(&$attachments) {
$attachments['#attached']['library'][] = 'quadstat_misc/quadstat_misc';
}
/**
* Implements hook_theme_registry_alter
*/
function quadstat_misc_theme_registry_alter(&$theme_registry) {
$path = drupal_get_path('module', 'quadstat_misc') . '/templates';
#$theme_registry['webform_submission_html']['path'] = $path;
#$theme_registry['webform_submission_html']['template'] = 'custom-submission-html';
#$theme_registry['webform_submission_information']['path'] = $path;
#$theme_registry['webform_submission_information']['template'] = 'custom-submission-information';
$theme_registry['node']['type'] = 'theme';
$theme_registry['node']['path'] = $path;
$theme_registry['node']['template'] = 'node';
}
/**
* Implements hook_field_widget_form_alter().
*/
function quadstat_misc_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
if (get_class($context['widget']) === 'Drupal\file\Plugin\Field\FieldWidget\FileWidget' && !empty($context['form']['#attributes'])) {
$element['#description'] = '<p>File must be less than <strong>15 MB</strong>.<br/>Allowed types: <code>txt csv tsv xls xlsx sav sas7bdat</code></p>';
}
}
