tag_block_site_branding_switcher-8.x-1.1/tag_block_site_branding_switcher.module
tag_block_site_branding_switcher.module
<?php
/**
* @file
* Module tag_block_site_branding_switcher.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function tag_block_site_branding_switcher_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.tag_block_site_branding_switcher':
$text = file_get_contents(dirname(__FILE__) . '/README.md');
if (!\Drupal::moduleHandler()->moduleExists('markdown')) {
return '<pre>' . $text . '</pre>';
}
else {
// Use the Markdown filter to render the README.
$filter_manager = \Drupal::service('plugin.manager.filter');
$settings = \Drupal::configFactory()->get('markdown.settings')->getRawData();
$config = ['settings' => $settings];
$filter = $filter_manager->createInstance('markdown', $config);
return $filter->process($text, 'en');
}
}
return NULL;
}
/**
* Implements hook_preprocess_HOOK().
*/
function tag_block_site_branding_switcher_preprocess_block(&$variables) {
if ($variables['plugin_id'] == 'system_branding_block') {
$is_front_page = \Drupal::service('path.matcher')->isFrontPage();
// Add variable and cache context.
$variables['is_front_page'] = $is_front_page;
$variables['#cache']['contexts'][] = 'url.path.is_front';
}
}
/**
* Implements hook_theme_registry_alter().
*/
function tag_block_site_branding_switcher_theme_registry_alter(&$theme_registry) {
$active_theme = \Drupal::service('theme.manager')->getActiveTheme();
// Check if file not exist in active theme.
if (!file_exists($active_theme->getPath() . '/templates/block--system-branding-block.html.twig')) {
// Check if theme has base theme to select the right overrided template.
// Base theme stable is default core template.
$base_theme = 'stable';
if (!empty($active_theme->getExtension()->base_theme)) {
$active_base_theme = $active_theme->getExtension()->base_theme;
$themes = \Drupal::service('theme_handler')->listInfo();
// Get base theme until core theme.
$base_theme = tag_block_site_branding_switcher_get_core_theme($active_base_theme, $themes, $theme_registry);
}
// Set the branding block template.
if (in_array($base_theme, ['bartik', 'classy', 'stable'])) {
$module_path = drupal_get_path('module', 'tag_block_site_branding_switcher');
$theme_registry['block__system_branding_block']['path'] = implode(
'/',
[$module_path, 'templates', $base_theme]
);
}
}
}
/**
* Get base theme until core theme with block__system_branding_block template.
*/
function tag_block_site_branding_switcher_get_core_theme($base_theme, $themes, $theme_registry) {
// Check theme use block--system-branding-block template.
// If not use base theme.
$templates = drupal_find_theme_templates($theme_registry, '.html.twig', drupal_get_path('theme', $base_theme));
if (
!empty($themes[$base_theme]->base_theme) &&
((!empty($themes[$base_theme]->origin) && $themes[$base_theme]->origin !== 'core') || empty($templates['block__system_branding_block']['path']))
) {
return tag_block_site_branding_switcher_get_core_theme($themes[$base_theme]->base_theme, $themes, $theme_registry);
}
else {
return $base_theme;
}
}
