a12s-1.0.0-beta7/modules/theme_builder/a12s_theme_builder.module
modules/theme_builder/a12s_theme_builder.module
<?php
/**
* @file
* Primary module hooks for A12s Theme Builder module.
*/
use Drupal\a12s_theme_builder\Block\LanguageSwitcher;
use Drupal\a12s_theme_builder\Block\SettingsAlterBase;
use Drupal\block\BlockInterface;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_theme().
*/
function a12s_theme_builder_theme($existing, $type, $theme, $path): array {
return [
'user_login_form' => [
'render element' => 'form',
],
];
}
/**
* Add extra variables to "menu" derived themes, to provide some suggestions.
*
* @see hook_theme_registry_alter()
*/
function a12s_theme_builder_theme_registry_alter(array &$themeRegistry): void {
$path = \Drupal::service('extension.list.module')->getPath('a12s_theme_builder');
if ($element = &$themeRegistry['menu']) {
$element['theme path'] = $path;
$element['path'] = $path . '/templates';
}
// $menuThemes = preg_grep('/^menu(?:__.*)?$/', array_keys($themeRegistry));
//
// foreach ($menuThemes as $key) {
// if (isset($themeRegistry[$key]['variables'])) {
// $themeRegistry[$key]['variables'] += [
// 'block_id' => NULL,
// 'block_region' => NULL,
// ];
// }
// }
}
/**
* Preprocess all templates.
*
* @see hook_preprocess()
*/
function a12s_theme_builder_preprocess(array &$variables): void {
$variables['base_path'] = base_path();
/** @var \Drupal\Core\Theme\ActiveTheme $currentTheme */
$currentTheme = \Drupal::service('theme.manager')->getActiveTheme();
$variables['theme_path'] = base_path() . $currentTheme->getPath();
}
/**
* Add extra options to the block configuration form.
*
* @see hook_form_FORM_ID_alter()
*/
function a12s_theme_builder_form_block_form_alter(array &$form, FormStateInterface $formState): void {
$formObject = $formState->getFormObject();
if ($formObject instanceof EntityForm) {
$entity = $formObject->getEntity();
if ($entity instanceof BlockInterface) {
foreach (SettingsAlterBase::BLOCK_SETTINGS_ALTER_CLASSES as $class) {
call_user_func_array([$class, 'alterForm'], [$entity, &$form, $formState]);
}
}
}
}
/**
* Add menu attributes to the block content.
*
* @see \hook_preprocess_HOOK()
* @see hook_block_build_BASE_BLOCK_ID_alter()
*/
function a12s_theme_builder_preprocess_block(array &$variables): void {
// Preprocess "language switcher" blocks.
if (!empty($variables['elements']['#a12s_theme_builder']['language_switcher'])) {
\Drupal::classResolver(LanguageSwitcher::class)->preprocessBlock($variables, $variables['elements']['#a12s_theme_builder']['language_switcher']);
}
// Special case with block wrapper: we need to force the ID if defined through
// the standard #attributes array, as the Block module does not take care of
// this and override defined ID.
// @see template_preprocess_block()
if (!empty($variables['elements']['#attributes']['id'])) {
$variables['attributes']['id'] = $variables['elements']['#attributes']['id'];
}
}
/**
* @see hook_block_view_alter()
*/
function a12s_theme_builder_block_view_alter(array &$build, BlockPluginInterface $block): void {
if ($build['#block'] instanceof BlockInterface) {
foreach (SettingsAlterBase::BLOCK_SETTINGS_ALTER_CLASSES as $class) {
call_user_func_array([$class, 'alterView'], [$build['#block'], &$build, $block]);
}
}
}
