5 * Functions to support theming in the Bartik theme.
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Template\Attribute;
12 * Implements hook_preprocess_HOOK() for HTML document templates.
14 * Adds body classes if certain regions have content.
16 function bartik_preprocess_html(&$variables) {
17 // Add information about the number of sidebars.
18 if (!empty($variables['page']['sidebar_first']) && !empty($variables['page']['sidebar_second'])) {
19 $variables['attributes']['class'][] = 'layout-two-sidebars';
21 elseif (!empty($variables['page']['sidebar_first'])) {
22 $variables['attributes']['class'][] = 'layout-one-sidebar';
23 $variables['attributes']['class'][] = 'layout-sidebar-first';
25 elseif (!empty($variables['page']['sidebar_second'])) {
26 $variables['attributes']['class'][] = 'layout-one-sidebar';
27 $variables['attributes']['class'][] = 'layout-sidebar-second';
30 $variables['attributes']['class'][] = 'layout-no-sidebars';
33 if (!empty($variables['page']['featured_top'])) {
34 $variables['attributes']['class'][] = 'has-featured-top';
40 * Implements hook_preprocess_HOOK() for page title templates.
42 function bartik_preprocess_page_title(&$variables) {
43 // Since the title and the shortcut link are both block level elements,
44 // positioning them next to each other is much simpler with a wrapper div.
45 if (!empty($variables['title_suffix']['add_or_remove_shortcut']) && $variables['title']) {
46 // Add a wrapper div using the title_prefix and title_suffix render
48 $variables['title_prefix']['shortcut_wrapper'] = [
49 '#markup' => '<div class="shortcut-wrapper clearfix">',
52 $variables['title_suffix']['shortcut_wrapper'] = [
53 '#markup' => '</div>',
56 // Make sure the shortcut link is the first item in title_suffix.
57 $variables['title_suffix']['add_or_remove_shortcut']['#weight'] = -100;
62 * Implements hook_preprocess_HOOK() for maintenance-page.html.twig.
64 function bartik_preprocess_maintenance_page(&$variables) {
65 // By default, site_name is set to Drupal if no db connection is available
66 // or during site installation. Setting site_name to an empty string makes
67 // the site and update pages look cleaner.
68 // @see template_preprocess_maintenance_page
69 if (!$variables['db_is_active']) {
70 $variables['site_name'] = '';
73 // Bartik has custom styling for the maintenance page.
74 $variables['#attached']['library'][] = 'bartik/maintenance_page';
78 * Implements hook_preprocess_HOOK() for node.html.twig.
80 function bartik_preprocess_node(&$variables) {
81 // Remove the "Add new comment" link on teasers or when the comment form is
82 // displayed on the page.
83 if ($variables['teaser'] || !empty($variables['content']['comments']['comment_form'])) {
84 unset($variables['content']['links']['comment']['#links']['comment-add']);
89 * Implements hook_preprocess_HOOK() for block.html.twig.
91 function bartik_preprocess_block(&$variables) {
92 // Add a clearfix class to system branding blocks.
93 if ($variables['plugin_id'] == 'system_branding_block') {
94 $variables['attributes']['class'][] = 'clearfix';
99 * Implements hook_preprocess_HOOK() for menu.html.twig.
101 function bartik_preprocess_menu(&$variables) {
102 $variables['attributes']['class'][] = 'clearfix';
106 * Implements hook_theme_suggestions_HOOK_alter() for form templates.
108 function bartik_theme_suggestions_form_alter(array &$suggestions, array $variables) {
109 if ($variables['element']['#form_id'] == 'search_block_form') {
110 $suggestions[] = 'form__search_block_form';
115 * Implements hook_form_alter() to add classes to the search form.
117 function bartik_form_alter(&$form, FormStateInterface $form_state, $form_id) {
118 if (in_array($form_id, ['search_block_form', 'search_form'])) {
119 $key = ($form_id == 'search_block_form') ? 'actions' : 'basic';
120 if (!isset($form[$key]['submit']['#attributes'])) {
121 $form[$key]['submit']['#attributes'] = new Attribute();
123 $form[$key]['submit']['#attributes']->addClass('search-form__submit');