adminimal_theme.theme 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * @file
  4. * Functions to support theming in the Adminimal theme.
  5. */
  6. use Drupal\Core\Form\FormStateInterface;
  7. /**
  8. * Implements hook_preprocess_HOOK() for HTML document templates.
  9. */
  10. function adminimal_theme_preprocess_html(&$variables) {
  11. // Add adminimal class to the body.
  12. $variables['attributes']['class'][] = 'adminimal';
  13. // Add library with custom CSS.
  14. if (theme_get_setting('custom_css')) {
  15. $variables['#attached']['library'][] = 'adminimal_theme/custom-styling';
  16. }
  17. }
  18. /**
  19. * Implements hook_form_system_theme_settings_alter().
  20. */
  21. function adminimal_theme_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id = NULL) {
  22. // Work-around for a core bug affecting admin themes. See issue #943212.
  23. if (isset($form_id)) {
  24. return;
  25. }
  26. // Get adminimal theme path.
  27. global $base_url;
  28. $adminimal_path = drupal_get_path('theme', 'adminimal_theme');
  29. $old_css_path = $adminimal_path . '/css/custom.css';
  30. $custom_css_path = 'public://adminimal-custom.css';
  31. $custom_css_dir = str_replace($base_url . '/', "", file_create_url($custom_css_path));
  32. $custom_css_url = file_create_url($custom_css_path);
  33. // Try to create the adminimal-custom.css file automatically.
  34. if (!file_exists($custom_css_path)) {
  35. // Try to migrate from the old css.
  36. if (file_exists($old_css_path)) {
  37. file_unmanaged_copy($old_css_path, $custom_css_path, FILE_EXISTS_ERROR);
  38. }
  39. // Else create a new blank css file.
  40. else {
  41. file_unmanaged_save_data("", $custom_css_path, FILE_EXISTS_ERROR);
  42. }
  43. }
  44. // Notify user to remove his old css file.
  45. if (file_exists($old_css_path)) {
  46. drupal_set_message(t('Please delete the old @css_location file, as its no longer used.', ['@css_location file' => $old_css_path]), 'warning');
  47. }
  48. $form['adminimal_custom'] = [
  49. '#type' => 'fieldset',
  50. '#title' => t('Adminimal Customization'),
  51. '#weight' => -10,
  52. ];
  53. $form['adminimal_custom']['custom_css'] = [
  54. '#type' => 'checkbox',
  55. '#title' => t('Use "adminimal-custom.css"'),
  56. '#description' => t('Include adminimal-custom.css file to override or add custom css code without subthememing/hacking Adminimal Theme.'),
  57. '#default_value' => theme_get_setting('custom_css'),
  58. ];
  59. $form['adminimal_custom']['adminimal_custom_check'] = [
  60. '#type' => 'fieldset',
  61. '#title' => t('Custom CSS file check'),
  62. '#weight' => 50,
  63. '#states' => [
  64. // Hide the settings when the cancel notify checkbox is disabled.
  65. 'visible' => [
  66. ':input[name="custom_css"]' => ['checked' => TRUE],
  67. ],
  68. ],
  69. ];
  70. if (file_exists($custom_css_path)) {
  71. $form['adminimal_custom']['adminimal_custom_check']['custom_css_description'] = [
  72. '#type' => 'container',
  73. '#attributes' => [
  74. 'class' => ['messages', 'messages--status'],
  75. ],
  76. 'message' => [
  77. '#markup' => t('Custom CSS file Found in: @css', ['@css' => $custom_css_dir]),
  78. ],
  79. ];
  80. }
  81. else {
  82. $form['adminimal_custom']['adminimal_custom_check']['custom_css_not_found'] = [
  83. '#type' => 'container',
  84. '#attributes' => [
  85. 'class' => ['messages', 'messages--error'],
  86. ],
  87. 'message' => [
  88. '#markup' => t('Custom CSS file not found. You must create the @css file manually.', ['@css' => $custom_css_dir]),
  89. ],
  90. ];
  91. }
  92. }