v1/web/themes/contrib/gin/includes/page.theme

159 lines
5.9 KiB
PHP

<?php
/**
* @file
* page.theme
*/
use Drupal\gin\GinContentFormHelper;
use Drupal\Core\Entity\EntityInterface;
use Drupal\gin\GinSettings;
use Drupal\node\Entity\Node;
/**
* Implements hook_preprocess_HOOK() for page.
*/
function gin_preprocess_page(&$variables) {
// Required for allowing subtheming Gin.
$activeThemeName = \Drupal::theme()->getActiveTheme()->getName();
$variables['active_admin_theme'] = $activeThemeName;
/** @var \Drupal\gin\GinSettings $settings */
$settings = \Drupal::classResolver(GinSettings::class);
// Expose Toolbar variant.
$variables['toolbar_variant'] = $settings->get('classic_toolbar');
// Expose Route name.
$variables['route_name'] = \Drupal::routeMatch()->getRouteName();
if (preg_match('#entity\.(?<entity_type_id>.+)\.canonical#', $variables['route_name'], $matches)) {
$entity = \Drupal::request()->attributes->get($matches['entity_type_id']);
if ($entity instanceof EntityInterface && $entity->hasLinkTemplate('edit-form')) {
$variables['entity_title'] = $entity->label();
$variables['entity_edit_url'] = $entity->toUrl('edit-form');
}
}
}
/**
* Implements hook_preprocess_HOOK() for page_alter.
*/
function gin_theme_suggestions_page_alter(&$suggestions, $variables) {
$path = \Drupal::requestStack()->getCurrentRequest()->getPathInfo();
if ($path != '/') {
$path = trim($path, '/');
$arg = str_replace(["/", '-'], ['_', '_'], $path);
$suggestions[] = 'page__' . $arg;
}
// The node page template is required to use the node content form.
if (\Drupal::classResolver(GinContentFormHelper::class)->isContentForm()
&& !in_array('page__node', $suggestions)) {
$suggestions[] = 'page__node';
}
}
/**
* Implements hook_preprocess_HOOK() for page_attachments.
*/
function gin_page_attachments_alter(&$page) {
// Are we relevant?
$gin_activated = _gin_is_active();
if ($gin_activated) {
$drupal_version = (float) Drupal::VERSION;
// Get theme settings.
$settings = \Drupal::classResolver(GinSettings::class);
$toolbar = $settings->get('classic_toolbar');
// Attach the init script.
$page['#attached']['library'][] = 'gin/gin_init';
if ($toolbar === 'classic') {
// Attach the classic toolbar styles.
$page['#attached']['library'][] = 'gin/gin_classic_toolbar';
}
elseif ($toolbar === 'horizontal') {
// Attach the horizontal toolbar styles.
$page['#attached']['library'][] = 'gin/gin_horizontal_toolbar';
}
else {
// Attach toolbar styles.
$page['#attached']['library'][] = 'gin/gin_toolbar';
}
// Attach accent library.
$page['#attached']['library'][] = 'gin/gin_accent';
// Attach sticky library.
$page['#attached']['library'][] = 'gin/sticky';
// Attach Drupal.once for older Drupal versions.
if ($drupal_version < 9.3) {
$page['#attached']['library'][] = 'gin/once';
}
// Custom CSS file.
if (file_exists('public://gin-custom.css')) {
$page['#attached']['library'][] = 'gin/gin_custom_css';
}
// Expose settings to JS.
$page['#attached']['drupalSettings']['gin']['darkmode'] = $settings->get('enable_darkmode');
$page['#attached']['drupalSettings']['gin']['darkmode_class'] = 'gin--dark-mode';
$page['#attached']['drupalSettings']['gin']['preset_accent_color'] = $settings->get('preset_accent_color');
$page['#attached']['drupalSettings']['gin']['accent_color'] = $settings->get('accent_color');
$page['#attached']['drupalSettings']['gin']['preset_focus_color'] = $settings->get('preset_focus_color');
$page['#attached']['drupalSettings']['gin']['focus_color'] = $settings->get('focus_color');
$page['#attached']['drupalSettings']['gin']['highcontrastmode'] = $settings->get('high_contrast_mode');
$page['#attached']['drupalSettings']['gin']['highcontrastmode_class'] = 'gin--high-contrast-mode';
$page['#attached']['drupalSettings']['gin']['toolbar_variant'] = $settings->get('classic_toolbar');
// Expose stylesheets to JS.
$basethemeurl = '/' . \Drupal::service('extension.list.theme')->getPath('gin');
$page['#attached']['drupalSettings']['gin']['variables_css_path'] = $basethemeurl . '/dist/css/theme/variables.css';
$page['#attached']['drupalSettings']['gin']['accent_css_path'] = $basethemeurl . '/dist/css/theme/accent.css';
$page['#attached']['drupalSettings']['gin']['ckeditor_css_path'] = $basethemeurl . '/dist/css/theme/ckeditor.css';
// Expose default settings to JS.
$page['#attached']['drupalSettings']['gin']['default_darkmode'] = $settings->getDefault('enable_darkmode');
$page['#attached']['drupalSettings']['gin']['default_accent_color'] = $settings->getDefault('accent_color');
$page['#attached']['drupalSettings']['gin']['default_preset_accent_color'] = $settings->getDefault('preset_accent_color');
$page['#attached']['drupalSettings']['gin']['default_high_contrast_mode'] = $settings->getDefault('high_contrast_mode');
}
}
/**
* Page title.
*/
function gin_preprocess_page_title(&$variables) {
if (preg_match('/entity\.node\..*/', \Drupal::routeMatch()->getRouteName(), $matches)) {
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof Node) {
if ($node->isDefaultTranslation() && $matches[0] !== 'entity.node.content_translation_add') {
$variables['title'] = $node->getTitle();
}
elseif ($matches[0] === 'entity.node.edit_form') {
$variables['title_attributes']['class'][] = 'page-title--is-translation';
$args = [
'@title' => $node->getTitle(),
'@language' => $node->language()->getName(),
];
$variables['title'] = t('@title <span class="page-title__language">(@language translation)</span>', $args);
}
}
}
}
/**
* Node revisions.
*/
function gin_preprocess_page__node__revisions(&$page) {
// Attach the init script.
$page['#attached']['library'][] = 'gin/revisions';
}