clean install

This commit is contained in:
matze committed 2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 files changed
+3555714

No files matched your search

+59
View File
@@ -0,0 +1,59 @@
<?php
namespace Drupal\gin;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Url;
/**
* After build callbacks for the gin theme.
*/
class GinAfterBuild implements TrustedCallbackInterface {
/**
* After build callback to modify the description if a setting is overwritten.
*
* @param array $element
* A renderable array.
*
* @return array
* The updated renderable array containing the new description.
*/
public static function overriddenSettingByUser(array $element) {
/** @var \Drupal\gin\GinSettings $settings */
$settings = \Drupal::classResolver(GinSettings::class);
// Check if this is overridden by the logged in user.
if ($element && isset($element['#name']) && $settings->overridden($element['#name'])) {
$userEditUrl = Url::fromRoute('entity.user.edit_form', ['user' => \Drupal::currentUser()->id()])->toString();
$value = $settings->get($element['#name']);
if ($element['#type'] === 'radios' || $element['#type'] === 'select') {
$value = $element['#options'][$value];
}
if ($element['#type'] === 'checkbox') {
$value = $value ? t('Enabled') : t('Disabled');
}
$element += ['#description' => ''];
$element['#description'] .= '<span class="form-item__warning">' .
t('This setting is overridden by the <a href=":editUrl">current user</a>. @title: %value',
[
'@title' => $element['#title'],
'%value' => $value,
':editUrl' => $userEditUrl,
]) . '</span>';
}
return $element;
}
/**
* {@inheritdoc}
*/
public static function trustedCallbacks() {
return ['overriddenSettingByUser'];
}
}
+288
View File
@@ -0,0 +1,288 @@
<?php
namespace Drupal\gin;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Theme\ThemeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Service to handle content form overrides.
*/
class GinContentFormHelper implements ContainerInjectionInterface {
use StringTranslationTrait;
/**
* The current user object.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The theme manager.
*
* @var \Drupal\Core\Theme\ThemeManagerInterface
*/
protected $themeManager;
/**
* GinContentFormHelper constructor.
*
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
* @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
* The theme manager.
*/
public function __construct(AccountInterface $current_user, ModuleHandlerInterface $module_handler, RouteMatchInterface $route_match, ThemeManagerInterface $theme_manager) {
$this->currentUser = $current_user;
$this->moduleHandler = $module_handler;
$this->routeMatch = $route_match;
$this->themeManager = $theme_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_user'),
$container->get('module_handler'),
$container->get('current_route_match'),
$container->get('theme.manager'),
);
}
/**
* Add some major form overrides.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param string $form_id
* The form id.
*
* @see hook_form_alter()
*/
public function formAlter(array &$form, FormStateInterface $form_state, $form_id) {
// Are we on an edit form?
if (!$this->isContentForm($form, $form_state, $form_id)) {
return;
}
// Provide a default meta form element if not already provided.
// @see NodeForm::form()
$form['advanced']['#attributes']['class'][] = 'entity-meta';
if (!isset($form['meta'])) {
$form['meta'] = [
'#type' => 'container',
'#group' => 'advanced',
'#weight' => -10,
'#title' => $this->t('Status'),
'#attributes' => ['class' => ['entity-meta__header']],
'#tree' => TRUE,
'#access' => TRUE,
];
}
// Ensure correct settings for advanced, meta and revision form elements.
$form['advanced']['#type'] = 'container';
$form['advanced']['#accordion'] = TRUE;
$form['meta']['#type'] = 'container';
$form['meta']['#access'] = TRUE;
$form['revision_information']['#type'] = 'container';
$form['revision_information']['#group'] = 'meta';
$form['revision_information']['#attributes']['class'][] = 'entity-meta__revision';
// Action buttons.
if (isset($form['actions'])) {
if (isset($form['actions']['preview'])) {
// Put Save after Preview.
$save_weight = $form['actions']['preview']['#weight'] ? $form['actions']['preview']['#weight'] + 1 : 11;
$form['actions']['submit']['#weight'] = $save_weight;
}
// Move entity_save_and_addanother_node after preview.
if (isset($form['actions']['entity_save_and_addanother_node'])) {
// Put Save after Preview.
$save_weight = $form['actions']['entity_save_and_addanother_node']['#weight'];
$form['actions']['preview']['#weight'] = $save_weight - 1;
}
// Create gin_actions group.
$form['gin_actions'] = [
'#type' => 'container',
'#weight' => -1,
'#multilingual' => TRUE,
'#attributes' => [
'class' => [
'gin-sticky',
],
],
];
// Assign status to gin_actions.
$form['status']['#group'] = 'gin_actions';
// Move all actions over.
$form['gin_actions']['actions'] = ($form['actions']) ?? [];
$form['gin_actions']['actions']['#weight'] = 130;
// Now let's just remove delete, as we'll move that over to gin_sidebar.
unset($form['gin_actions']['actions']['delete']);
unset($form['gin_actions']['actions']['delete_translation']);
// Add sidebar toggle.
$hide_panel = t('Hide sidebar panel');
$form['gin_actions']['gin_sidebar_toggle'] = [
'#markup' => '<a href="#toggle-sidebar" class="meta-sidebar__trigger trigger" role="button" title="' . $hide_panel . '" aria-controls="gin_sidebar"><span class="visually-hidden">' . $hide_panel . '</span></a>',
'#weight' => '999',
];
$form['#attached']['library'][] = 'gin/sidebar';
// Create gin_sidebar group.
$form['gin_sidebar'] = [
'#group' => 'meta',
'#type' => 'container',
'#weight' => 99,
'#multilingual' => TRUE,
'#attributes' => [
'class' => [
'gin-sidebar',
],
],
];
// Copy footer over.
$form['gin_sidebar']['footer'] = ($form['footer']) ?? [];
// Copy actions.
$form['gin_sidebar']['actions'] = [];
$form['gin_sidebar']['actions']['#type'] = ($form['actions']['#type']) ?? [];
// Copy delete action.
$form['gin_sidebar']['actions']['delete'] = ($form['actions']['delete']) ?? [];
// Copy delete_translation action.
if (isset($form['actions']['delete_translation'])) {
$form['gin_sidebar']['actions']['delete_translation'] = ($form['actions']['delete_translation']) ?? [];
$form['gin_sidebar']['actions']['delete_translation']['#attributes']['class'][] = 'button--danger';
$form['gin_sidebar']['actions']['delete_translation']['#attributes']['class'][] = 'action-link';
}
// Sidebar close button.
$close_sidebar_translation = t('Close sidebar panel');
$form['gin_sidebar']['gin_sidebar_close'] = [
'#markup' => '<a href="#close-sidebar" class="meta-sidebar__close trigger" role="button" title="' . $close_sidebar_translation . '"><span class="visually-hidden">' . $close_sidebar_translation . '</span></a>',
];
$form['gin_sidebar_overlay'] = [
'#markup' => '<div class="meta-sidebar__overlay trigger"></div>',
];
}
// Specify necessary node form theme and library.
// @see claro_form_node_form_alter
$form['#theme'] = ['node_edit_form'];
// Attach libraries.
$form['#attached']['library'][] = 'gin/edit_form';
// Add a class that allows the logic in edit_form.js to identify the form.
$form['#attributes']['class'][] = 'gin-node-edit-form';
// If not logged in hide changed and author node info on add forms.
$not_logged_in = $this->currentUser->isAnonymous();
$route = $this->routeMatch->getRouteName();
if ($not_logged_in && $route == 'node.add') {
unset($form['meta']['changed']);
unset($form['meta']['author']);
}
}
/**
* Check if we´re on a content edit form.
*
* _gin_is_content_form() is replaced by
* \Drupal::classResolver(GinContentFormHelper::class)->isContentForm().
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param string $form_id
* The form id.
*/
public function isContentForm(array $form = NULL, FormStateInterface $form_state = NULL, $form_id = '') {
$is_content_form = FALSE;
// Get route name.
$route_name = $this->routeMatch->getRouteName();
// Routes to include.
$route_names = [
'node.add',
'entity.node.content_translation_add',
'entity.node.content_translation_edit',
'quick_node_clone.node.quick_clone',
'entity.node.edit_form',
];
$additional_routes = $this->moduleHandler->invokeAll('gin_content_form_routes');
$route_names = array_merge($additional_routes, $route_names);
$this->moduleHandler->alter('gin_content_form_routes', $route_names);
$this->themeManager->alter('gin_content_form_routes', $route_names);
if (
in_array($route_name, $route_names, TRUE) ||
($form_state && ($form_state->getBuildInfo()['base_form_id'] ?? NULL) === 'node_form') ||
($route_name === 'entity.group_content.create_form' && substr($this->routeMatch->getParameter('plugin_id'), 0, 11) === "group_node:") ||
($route_name === 'entity.group_relationship.create_form' && substr($this->routeMatch->getParameter('plugin_id'), 0, 11) === "group_node:")
) {
$is_content_form = TRUE;
}
// Forms to exclude.
// If media library widget, don't use new content edit form.
// gin_preprocess_html is not triggered here, so checking
// the form id is enough.
$form_ids_to_ignore = [
'media_library_add_form_',
'views_form_media_library_widget_',
'views_exposed_form',
'date_recur_modular_sierra_occurrences_modal',
'date_recur_modular_sierra_modal',
];
foreach ($form_ids_to_ignore as $form_id_to_ignore) {
if ($form_id && strpos($form_id, $form_id_to_ignore) !== FALSE) {
$is_content_form = FALSE;
}
}
return $is_content_form;
}
}
@@ -0,0 +1,82 @@
<?php
namespace Drupal\gin;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Service to handle toggling form descriptions.
*/
class GinDescriptionToggle implements ContainerInjectionInterface {
/**
* The content form helper class.
*
* @var \Drupal\gin\GinContentFormHelper
*/
protected $contentFormHelper;
/**
* The gin theme settings class.
*
* @var \Drupal\gin\GinSettings
*/
protected $ginSettings;
/**
* GinDescriptionToggle constructor.
*
* @param \Drupal\gin\GinSettings $ginSettings
* The gin theme settings class.
* @param \Drupal\gin\GinContentFormHelper $contentFormHelper
* The content form helper class.
*/
public function __construct(GinSettings $ginSettings, GinContentFormHelper $contentFormHelper) {
$this->ginSettings = $ginSettings;
$this->contentFormHelper = $contentFormHelper;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$classResolver = $container->get('class_resolver');
return new static(
$classResolver->getInstanceFromDefinition(GinSettings::class),
$classResolver->getInstanceFromDefinition(GinContentFormHelper::class),
);
}
/**
* Generic preprocess enabling toggle.
*
* @param array $variables
* The variables array (modify in place).
*/
public function preprocess(array &$variables) {
if ($this->isEnabled()) {
if (!empty($variables['description'])) {
$variables['description_display'] = 'invisible';
$variables['description_toggle'] = TRUE;
}
// Add toggle for text_format, description is in wrapper.
elseif (!empty($variables['element']['#description_toggle'])) {
$variables['description_toggle'] = TRUE;
}
}
}
/**
* Functionality is enabled via setting on content forms.
*
* @return bool
* Wether feature is enabled or not.
*/
public function isEnabled() {
return $this->ginSettings->get('show_description_toggle') && $this->contentFormHelper->isContentForm();
}
}
+326
View File
@@ -0,0 +1,326 @@
<?php
namespace Drupal\gin;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Url;
use Drupal\taxonomy\Entity\Vocabulary;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Service to handle overridden user settings.
*/
class GinNavigation implements ContainerInjectionInterface {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static();
}
/**
* Get Navigation Admin Menu Items.
*/
public function getNavigationAdminMenuItems(): array {
$parameters = new MenuTreeParameters();
$parameters->setMinDepth(2)->setMaxDepth(4)->onlyEnabledLinks();
/** @var Drupal\Core\Menu\MenuLinkTree $menu_tree */
$menu_tree = \Drupal::service('menu.link_tree');
$tree = $menu_tree->load('admin', $parameters);
$manipulators = [
['callable' => 'menu.default_tree_manipulators:checkAccess'],
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
['callable' => 'toolbar_menu_navigation_links'],
];
$tree = $menu_tree->transform($tree, $manipulators);
$build = $menu_tree->build($tree);
/** @var \Drupal\Core\Menu\MenuLinkInterface $link */
$first_link = reset($tree)->link;
// Get the menu name of the first link.
$menu_name = $first_link->getMenuName();
$build['#menu_name'] = $menu_name;
$build['#theme'] = 'menu_region__middle';
// Loop through menu items and add the plugin id as a class.
foreach ($tree as $item) {
$plugin_id = $item->link->getPluginId();
$plugin_class = str_replace('.', '_', $plugin_id);
$build['#items'][$plugin_id]['class'] = $plugin_class;
}
// Remove content and help from admin menu.
unset($build['#items']['system.admin_content']);
unset($build['#items']['help.main']);
$build['#title'] = t('Administration');
return $build;
}
/**
* Get Navigation Bookmarks.
*/
public function getNavigationBookmarksMenuItems(): array {
// Check if the shortcut module is installed.
if (\Drupal::hasService('shortcut.lazy_builders') === TRUE) {
$shortcuts = \Drupal::service('shortcut.lazy_builders')->lazyLinks()['shortcuts'];
$shortcuts['#theme'] = 'menu_region__top';
$shortcuts['#menu_name'] = 'bookmarks';
$shortcuts['#title'] = t('Bookmarks');
return $shortcuts;
}
else {
return [];
}
}
/**
* Get Navigation Create menu.
*/
public function getNavigationCreateMenuItems(): array {
// Get the Entity Type Manager service.
$entity_type_manager = \Drupal::entityTypeManager();
// Needs to be this syntax to
// support older PHP versions
// for Druapl 9.0+.
$create_type_items = [];
$create_item_url = '';
// Get node types.
if ($entity_type_manager->hasDefinition('node')) {
$create_item_url = Url::fromRoute('node.add_page')->toString();
$content_types = $entity_type_manager->getStorage('node_type')->loadMultiple();
$content_type_items = [];
foreach ($content_types as $item) {
$content_type_items[] = [
'title' => $item->label(),
'class' => $item->id(),
'url' => $create_item_url,
];
}
$create_type_items = array_merge($content_type_items);
}
// Get block types.
if ($entity_type_manager->hasDefinition('block_content')) {
$block_content_types = BlockContentType::loadMultiple();
$block_type_items = [];
foreach ($block_content_types as $item) {
$block_type_items[] = [
'title' => $item->label(),
'class' => $item->id(),
'url' => Url::fromRoute('block_content.add_form', ['block_content_type' => $item->id()]),
];
}
$create_type_items = array_merge(
$create_type_items,
[
[
'title' => t('Blocks'),
'class' => 'blocks',
'url' => '',
'below' => $block_type_items,
],
]
);
}
// Get media types.
if ($entity_type_manager->hasDefinition('media')) {
$media_types = $entity_type_manager->getStorage('media_type')->loadMultiple();
$media_type_items = [];
foreach ($media_types as $item) {
$media_type_items[] = [
'title' => $item->label(),
'class' => $item->label(),
'url' => Url::fromRoute('entity.media.add_form', ['media_type' => $item->id()]),
];
}
$create_type_items = array_merge(
$create_type_items,
[
[
'title' => t('Media'),
'class' => 'media',
'url' => '',
'below' => $media_type_items,
],
]
);
}
// Get taxomony types.
if ($entity_type_manager->hasDefinition('taxonomy_term')) {
$taxonomy_types = Vocabulary::loadMultiple();
$taxonomy_type_items = [];
foreach ($taxonomy_types as $item) {
$taxonomy_type_items[] = [
'title' => $item->label(),
'class' => $item->id(),
'url' => Url::fromRoute('entity.taxonomy_term.add_form', ['taxonomy_vocabulary' => $item->id()]),
];
}
$create_type_items = array_merge(
$create_type_items,
[
[
'title' => t('Taxonomy'),
'class' => 'taxonomy',
'url' => '',
'below' => $taxonomy_type_items,
],
]
);
}
if (!$create_type_items && !$create_item_url) {
return [];
}
// Generate menu items.
$create_items['create'] = [
'title' => t('Create'),
'class' => 'create',
'url' => $create_item_url,
'below' => $create_type_items,
];
return [
'#theme' => 'menu_region__middle',
'#items' => $create_items,
'#menu_name' => 'create',
'#title' => t('Create Navigation'),
];
}
/**
* Get Navigation Content menu.
*/
public function getNavigationContentMenuItems(): array {
$entity_type_manager = \Drupal::entityTypeManager();
$create_content_items = [];
// Get Content menu item.
if ($entity_type_manager->hasDefinition('node')) {
$create_content_items['content'] = [
'title' => t('Content'),
'class' => 'content',
'url' => Url::fromRoute('system.admin_content')->toString(),
];
}
// Get Blocks menu item.
if ($entity_type_manager->hasDefinition('block_content')) {
$create_content_items['blocks'] = [
'title' => t('Blocks'),
'class' => 'blocks',
'url' => Url::fromRoute('entity.block_content.collection')->toString(),
];
}
// Get File menu item.
if ($entity_type_manager->hasDefinition('file')) {
$create_content_items['files'] = [
'title' => t('Files'),
'class' => 'files',
'url' => '/admin/content/files',
];
}
// Get Media menu item.
if ($entity_type_manager->hasDefinition('media')) {
$create_content_items['media'] = [
'title' => t('Media'),
'class' => 'media',
'url' => '/admin/content/media',
];
}
return [
'#theme' => 'menu_region__middle',
'#items' => $create_content_items,
'#menu_name' => 'content',
'#title' => t('Content Navigation'),
];
}
/**
* Get Navigation User menu.
*/
public function getMenuNavigationUserItems(): array {
$user_items = [
[
'title' => t('Profile'),
'class' => 'profile',
'url' => Url::fromRoute('user.page')->toString(),
],
[
'title' => t('Settings'),
'class' => 'settings',
'url' => Url::fromRoute('entity.user.admin_form')->toString(),
],
[
'title' => t('Log out'),
'class' => 'logout',
'url' => Url::fromRoute('user.logout')->toString(),
],
];
return [
'#theme' => 'menu_region__bottom',
'#items' => $user_items,
'#menu_name' => 'user',
'#title' => t('User'),
];
}
/**
* Get Navigation.
*/
public function getNavigationStructure() {
// Get navigation items.
$menu['top']['create'] = $this->getNavigationCreateMenuItems();
$menu['middle']['content'] = $this->getNavigationContentMenuItems();
$menu['middle']['admin'] = $this->getNavigationAdminMenuItems();
$menu['bottom']['user'] = $this->getMenuNavigationUserItems();
return [
'#theme' => 'navigation',
'#menu_top' => $menu['top'],
'#menu_middle' => $menu['middle'],
'#menu_bottom' => $menu['bottom'],
'#attached' => [
'library' => [
'gin/navigation',
],
],
'#access' => \Drupal::currentUser()->hasPermission('access toolbar'),
];
}
/**
* Get Active trail.
*/
public function getNavigationActiveTrail() {
// Get the breadcrumb paths to maintain active trail in the toolbar.
$links = \Drupal::service('breadcrumb')->build(\Drupal::routeMatch())->getLinks();
$paths = [];
foreach ($links as $link) {
$paths[] = $link->getUrl()->getInternalPath();
}
return $paths;
}
}
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace Drupal\gin;
use Drupal\Core\Security\TrustedCallbackInterface;
/**
* Implements trusted prerender callbacks for the Gin theme.
*
* @internal
*/
class GinPreRender implements TrustedCallbackInterface {
/**
* Prepare description toggle for output in template.
*/
public static function textFormat($element) {
if (\Drupal::classResolver(GinDescriptionToggle::class)->isEnabled() && !empty($element['#description'])) {
if ($element['#type'] === 'text_format') {
$element['value']['#description_toggle'] = TRUE;
}
else {
$element['#description_toggle'] = TRUE;
$element['#description_display'] = 'invisible';
}
}
return $element;
}
/**
* {@inheritdoc}
*/
public static function trustedCallbacks() {
return [
'textFormat',
];
}
}
+493
View File
@@ -0,0 +1,493 @@
<?php
namespace Drupal\gin;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Service to handle overridden user settings.
*/
class GinSettings implements ContainerInjectionInterface {
use StringTranslationTrait;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The user data service.
*
* @var \Drupal\user\UserDataInterface|null
*/
protected $userData;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* Settings constructor.
*
* @param \Drupal\Core\Session\AccountInterface $currentUser
* The current user.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory.
*/
public function __construct(AccountInterface $currentUser, ConfigFactoryInterface $configFactory) {
if (\Drupal::hasService('user.data')) {
$this->userData = \Drupal::service('user.data');
}
$this->currentUser = $currentUser;
$this->configFactory = $configFactory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_user'),
$container->get('config.factory')
);
}
/**
* Get the setting for the current user.
*
* @param string $name
* The name of the setting.
* @param \Drupal\Core\Session\AccountInterface|null $account
* The account object. Current user if NULL.
*
* @return array|bool|mixed|null
* The current value.
*/
public function get($name, AccountInterface $account = NULL) {
$value = NULL;
if (!$account) {
$account = $this->currentUser;
}
if ($this->userOverrideEnabled($account)) {
$settings = $this->userData->get('gin', $account->id(), 'settings');
if (isset($settings[$name])) {
$value = $settings[$name];
}
else {
// Try loading legacy settings from user data.
$value = $this->userData->get('gin', $account->id(), $name);
}
}
if (is_null($value)) {
$admin_theme = $this->getAdminTheme();
$value = theme_get_setting($name, $admin_theme);
}
return $this->handleLegacySettings($name, $value);
}
/**
* Get the default setting from theme.
*
* @param string $name
* The name of the setting.
*
* @return array|bool|mixed|null
* The current value.
*/
public function getDefault($name) {
$admin_theme = $this->getAdminTheme();
return $this->handleLegacySettings($name, theme_get_setting($name, $admin_theme));
}
/**
* Set user overrides.
*
* @param array $settings
* The user specific theme settings.
* @param \Drupal\Core\Session\AccountInterface|null $account
* The account object. Current user if NULL.
*/
public function setAll(array $settings, AccountInterface $account = NULL) {
if (!$account || !$this->userData) {
$account = $this->currentUser;
}
// All settings are deleted to remove legacy settings.
$this->userData->delete('gin', $account->id());
$this->userData->set('gin', $account->id(), 'enable_user_settings', TRUE);
$this->userData->set('gin', $account->id(), 'settings', $settings);
}
/**
* Clears all gin settings for the current user.
*
* @param \Drupal\Core\Session\AccountInterface|null $account
* The account object. Current user if NULL.
*/
public function clear(AccountInterface $account = NULL) {
if (!$account || !$this->userData) {
$account = $this->currentUser;
}
$this->userData->delete('gin', $account->id());
}
/**
* Determine if user overrides are allowed.
*
* @return bool
* TRUE or FALSE.
*/
public function allowUserOverrides() {
$admin_theme = $this->getAdminTheme();
return theme_get_setting('show_user_theme_settings', $admin_theme);
}
/**
* Determine if the user enabled overrides.
*
* @param \Drupal\Core\Session\AccountInterface|null $account
* The account object. Current user if NULL.
*
* @return bool
* TRUE or FALSE.
*/
public function userOverrideEnabled(AccountInterface $account = NULL) {
if (!$account || !$this->userData) {
$account = $this->currentUser;
}
return $this->allowUserOverrides() && (bool) $this->userData->get('gin', $account->id(), 'enable_user_settings');
}
/**
* Check if the user setting overrides the global setting.
*
* @param string $name
* Name of the setting to check.
* @param \Drupal\Core\Session\AccountInterface|null $account
* The account object. Current user if NULL.
*
* @return bool
* TRUE or FALSE.
*/
public function overridden($name, AccountInterface $account = NULL) {
if (!$account) {
$account = $this->currentUser;
}
$admin_theme = $this->getAdminTheme();
return $this->handleLegacySettings($name, theme_get_setting($name, $admin_theme)) !== $this->get($name, $account);
}
/**
* Return a massaged value from deprecated theme settings.
*
* @param string $name
* Name of the setting to check.
* @param array|bool|mixed|null $value
* The value of the currently used setting.
*
* @return array|bool|mixed|null
* The value determined by a legacy setting.
*/
private function handleLegacySettings($name, $value) {
$admin_theme = $this->getAdminTheme();
// Darkmode legacy setting.
if ($name === 'enable_darkmode') {
$value = (string) $value;
}
// High contrast mode legacy setting.
if ($name === 'high_contrast_mode') {
$value = (bool) $value;
}
// Accent color legacy setting check.
if ($name === 'preset_accent_color') {
$value = $value === 'claro_blue' ? 'blue' : $value;
}
// Toolbar legacy setting check.
if ($name === 'classic_toolbar') {
$value = $value === TRUE || $value === 'true' || $value === '1' || $value === 1 ? 'classic' : $value;
}
// Layout density check.
if ($name === 'layout_density') {
$value = $value === '0' ? 'default' : $value;
}
// Logo legacy settings check.
if ($name === 'icon_default' && is_null($value)) {
$value = $this->get('logo.use_default');
}
if ($name === 'icon_path' && is_null($value)) {
$value = $this->get('logo.path');
}
// Handles switching new version code with old config present.
if ($name === 'logo.use_default') {
if (theme_get_setting('icon_default', $admin_theme) === FALSE) {
return FALSE;
}
}
if ($name === 'logo.path') {
if (theme_get_setting('icon_default', $admin_theme) === FALSE && !is_null($this->get('icon_path'))) {
return $this->get('icon_path');
}
}
return $value;
}
/**
* Return the active admin theme.
*
* @return string
* The active admin theme name.
*/
private function getAdminTheme() {
$admin_theme = $this->configFactory->get('system.theme')->get('admin');
if (empty($admin_theme)) {
$admin_theme = $this->configFactory->get('system.theme')->get('default');
}
return $admin_theme;
}
/**
* Build the settings form for the theme.
*
* @param \Drupal\Core\Session\AccountInterface|null $account
* The account object.
*
* @return array
* The theme setting form elements.
*/
public function getSettingsForm(AccountInterface $account = NULL): array {
$experimental_label = ' <span class="gin-experimental-flag">Experimental</span>';
$beta_label = ' <span class="gin-beta-flag">Beta</span>';
$new_label = ' <span class="gin-new-flag">New</span>';
$form['enable_darkmode'] = [
'#type' => 'radios',
'#title' => $this->t('Appearance'),
'#description' => $this->t('Enables Darkmode for the admin interface.'),
'#default_value' => (string) ($account ? $this->get('enable_darkmode', $account) : $this->getDefault('enable_darkmode')),
'#options' => [
0 => $this->t('Light'),
1 => $this->t('Dark'),
'auto' => $this->t('Auto'),
],
];
// Accent color setting.
$form['preset_accent_color'] = [
'#type' => 'radios',
'#title' => $this->t('Accent color'),
'#default_value' => $account ? $this->get('preset_accent_color', $account) : $this->getDefault('preset_accent_color'),
'#options' => [
'blue' => $this->t('Gin Blue (Default)'),
'light_blue' => $this->t('Light Blue'),
'dark_purple' => $this->t('Dark Purple'),
'purple' => $this->t('Purple'),
'teal' => $this->t('Teal'),
'green' => $this->t('Green'),
'pink' => $this->t('Pink'),
'red' => $this->t('Red'),
'orange' => $this->t('Orange'),
'yellow' => $this->t('Yellow'),
'neutral' => $this->t('Neutral'),
'custom' => $this->t('Custom'),
],
'#after_build' => [
'_gin_accent_radios',
],
];
// Accent color group.
$form['accent_group'] = [
'#type' => 'fieldset',
'#title' => $this->t('Custom Accent color'),
'#description' => $this->t('Use with caution, values should meet a11y criteria.'),
'#states' => [
// Show if met.
'visible' => [
':input[name="preset_accent_color"]' => ['value' => 'custom'],
],
],
];
// Main Accent color setting.
$form['accent_color'] = [
'#type' => 'textfield',
'#placeholder' => '#777777',
'#maxlength' => 7,
'#size' => 7,
'#title' => $this->t('Custom Accent color'),
'#title_display' => 'invisible',
'#default_value' => $account ? $this->get('accent_color', $account) : $this->getDefault('accent_color'),
'#group' => 'accent_group',
'#attributes' => [
'pattern' => '^#[a-fA-F0-9]{6}',
],
];
// Accent color picker (helper field).
$form['accent_group']['accent_picker'] = [
'#type' => 'color',
'#placeholder' => '#777777',
'#default_value' => $account ? $this->get('accent_color', $account) : $this->getDefault('accent_color'),
'#process' => [
[static::class, 'processColorPicker'],
],
];
// Focus color setting.
$form['preset_focus_color'] = [
'#type' => 'select',
'#title' => $this->t('Focus color'),
'#default_value' => $account ? $this->get('preset_focus_color', $account) : $this->getDefault('preset_focus_color'),
'#options' => [
'gin' => $this->t('Gin Focus color (Default)'),
'green' => $this->t('Green'),
'claro' => $this->t('Claro Green'),
'orange' => $this->t('Orange'),
'dark' => $this->t('Neutral'),
'accent' => $this->t('Same as Accent color'),
'custom' => $this->t('Custom'),
],
];
// Focus color group.
$form['focus_group'] = [
'#type' => 'fieldset',
'#title' => $this->t('Custom Focus color') . $beta_label,
'#description' => $this->t('Use with caution, values should meet a11y criteria.'),
'#states' => [
// Show if met.
'visible' => [
':input[name="preset_focus_color"]' => ['value' => 'custom'],
],
],
];
// Focus color picker (helper).
$form['focus_group']['focus_picker'] = [
'#type' => 'color',
'#placeholder' => '#777777',
'#default_value' => $account ? $this->get('focus_color', $account) : $this->getDefault('focus_color'),
'#process' => [
[static::class, 'processColorPicker'],
],
];
// Custom Focus color setting.
$form['focus_color'] = [
'#type' => 'textfield',
'#title' => $this->t('Custom Focus color') . $beta_label,
'#title_display' => 'invisible',
'#placeholder' => '#777777',
'#maxlength' => 7,
'#size' => 7,
'#default_value' => $account ? $this->get('focus_color', $account) : $this->getDefault('focus_color'),
'#group' => 'focus_group',
'#attributes' => [
'pattern' => '^#[a-fA-F0-9]{6}',
],
];
// High contrast mode.
$form['high_contrast_mode'] = [
'#type' => 'checkbox',
'#title' => $this->t('Increase contrast') . $experimental_label,
'#description' => $this->t('Enables high contrast mode.'),
'#default_value' => $account ? $this->get('high_contrast_mode', $account) : $this->getDefault('high_contrast_mode'),
];
// Toolbar setting.
$form['classic_toolbar'] = [
'#type' => 'radios',
'#title' => $this->t('Navigation (Drupal Toolbar)'),
'#default_value' => $account ? $this->get('classic_toolbar', $account) : $this->getDefault('classic_toolbar'),
'#options' => [
'vertical' => $this->t('Sidebar, Vertical Toolbar (Default)'),
'horizontal' => $this->t('Horizontal, Modern Toolbar'),
'classic' => $this->t('Legacy, Classic Drupal Toolbar'),
'new' => $this->t('New Drupal Navigation, Test integration') . $new_label . $experimental_label,
],
'#after_build' => [
'_gin_toolbar_radios',
],
];
// Show secondary toolbar in Frontend.
if (!$account) {
$form['secondary_toolbar_frontend'] = [
'#type' => 'checkbox',
'#title' => $this->t('Show Secondary Toolbar in Frontend'),
'#description' => $this->t('Show the secondary toolbar in the Frontend (when logged in to Drupal).'),
'#default_value' => $this->getDefault('secondary_toolbar_frontend'),
];
}
// Layout density setting.
$form['layout_density'] = [
'#type' => 'radios',
'#title' => $this->t('Layout density') . $beta_label,
'#description' => $this->t('Changes the layout density for tables in the admin interface.'),
'#default_value' => (string) ($account ? $this->get('layout_density', $account) : $this->getDefault('layout_density')),
'#options' => [
'default' => $this->t('Default'),
'medium' => $this->t('Compact'),
'small' => $this->t('Narrow'),
],
];
// Description toggle.
$form['show_description_toggle'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable form description toggle'),
'#description' => $this->t('Show a help icon to show/hide form descriptions on content forms.'),
'#default_value' => $account ? $this->get('show_description_toggle', $account) : $this->getDefault('show_description_toggle'),
];
if (!$account) {
foreach ($form as $key => $element) {
$form[$key]['#after_build'][] = [
GinAfterBuild::class, 'overriddenSettingByUser',
];
}
}
return $form;
}
/**
* Unset color picker fields.
*
* @param array $element
* An associative array containing the properties and children of the
* element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* The form element.
*/
public static function processColorPicker(array $element, FormStateInterface $form_state) {
$keys = $form_state->getCleanValueKeys();
$form_state->setCleanValueKeys(array_merge((array) $keys, $element['#parents']));
return $element;
}
}
+123
View File
@@ -0,0 +1,123 @@
<?php
namespace Drupal\gin;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* User picture build callback for the gin theme.
*/
class GinUserPicture implements ContainerInjectionInterface, TrustedCallbackInterface {
/**
* The currently authenticated user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* GinUserPicture constructor.
*
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager) {
$this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_user'),
$container->get('entity_type.manager')
);
}
/**
* Lazy builder callback for the user picture.
*/
public function build(): array {
/** @var \Drupal\user\UserInterface $user */
$user = $this->entityTypeManager->getStorage('user')->load($this->currentUser->id());
$build = [
'#type' => 'link',
'#url' => $user->toUrl(),
'#title' => [
'#markup' => $user->getDisplayName(),
],
'#attributes' => [
'id' => 'toolbar-item-user-secondary',
'class' => [
'toolbar-icon',
'toolbar-icon-user',
'trigger',
'toolbar-item',
],
'role' => 'button',
],
];
/** @var \Drupal\image\ImageStyleInterface $style */
$style = NULL;
try {
$style = $this->entityTypeManager->getStorage('image_style')->load('thumbnail');
}
catch (PluginNotFoundException $e) {
// The image style plugin does not exists. $style stays NULL and no user
// picture will be added.
}
if ($style === NULL) {
return ['link' => $build];
}
$file = $user->user_picture ? $user->user_picture->entity : NULL;
if ($file === NULL) {
return ['link' => $build];
}
$image_url = $style->buildUrl($file->getFileUri());
$build['#attributes']['class'] = ['toolbar-item icon-user'];
$build['#title'] = [
'#type' => 'html_tag',
'#tag' => 'img',
'#attributes' => [
'src' => $image_url,
'alt' => $user->getAccountName(),
'class' => [
'icon-user__image',
],
],
];
return ['link' => $build];
}
/**
* {@inheritdoc}
*/
public static function trustedCallbacks() {
return ['build'];
}
}