Skip to content
Snippets Groups Projects
bootstrap_barrio.theme 24.4 KiB
Newer Older
Alberto Siles's avatar
Alberto Siles committed
<?php

/**
 * @file
 * Functions to support theming in the Bootstrap Barrio theme.
Alberto Siles's avatar
Alberto Siles committed
 */

use Drupal\Core\Template\Attribute;
use Drupal\Core\Link;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Xss;
use Drupal\node\Entity\Node;
use Drupal\Component\Render\MarkupInterface;
Alberto Siles's avatar
Alberto Siles committed

/**
 * Implements hook_preprocess_HOOK() for HTML document templates.
 *
 * Adds body classes if certain regions have content.
 */
function bootstrap_barrio_preprocess_html(&$variables) {
  if (!isset($variables['attributes']['class'])) {
Alberto Siles's avatar
Alberto Siles committed
  // Add information about the number of sidebars.
  if (!empty($variables['page']['sidebar_first']) && !empty($variables['page']['sidebar_second'])) {
    $variables['attributes']['class'][] = 'layout-two-sidebars';
  }
  elseif (!empty($variables['page']['sidebar_first'])) {
    $variables['attributes']['class'][] = 'layout-one-sidebar';
    $variables['attributes']['class'][] = 'layout-sidebar-first';
  }
  elseif (!empty($variables['page']['sidebar_second'])) {
    $variables['attributes']['class'][] = 'layout-one-sidebar';
    $variables['attributes']['class'][] = 'layout-sidebar-second';
  }
  else {
    $variables['attributes']['class'][] = 'layout-no-sidebars';
  }

  if (!empty($variables['page']['featured_top'])) {
    $variables['attributes']['class'][] = 'has-featured-top';
  }
  // Scroll Spy info added to body.
  if ($scroll_spy = theme_get_setting('bootstrap_barrio_scroll_spy')) {
    $variables['attributes']['data-target'] = $scroll_spy;
    $variables['attributes']['data-spy'] = "scroll";
  }

  $variables['path_info']['args'] = FALSE;
  $path = \Drupal::service('path.current')->getPath();
  $path = ltrim($path, '/');
  $path_args = explode('/', $path);
  if (isset($path_args[0]) && isset($path_args[1]) && ($path_args[0] == 'node') && (is_numeric($path_args[1]))) {
    $variables['attributes']['class'][] = 'page-node-' . $path_args[1];
  }
  if (isset($path_args[0]) && isset($path_args[1]) && ($path_args[0] == 'user')) {
    $variables['attributes']['class'][] = 'page-user-' . $path_args[1];
  }
  if (isset($path_args[0]) && isset($path_args[1]) && isset($path_args[2]) &&
Alberto Siles's avatar
Alberto Siles committed
    $path_args[0] == 'taxonomy' && $path_args[1] == 'term') {
    $variables['attributes']['class'][] = 'page-taxonomy-term-' . $path_args[2];
    $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($path_args[2]);
    if (!empty($term)) {
      $vocabulary_id = $term->bundle();
      $variables['attributes']['class'][] = 'page-vocabulary-' . Html::cleanCssIdentifier($vocabulary_id);
    }
  }
  $view_id = \Drupal::routeMatch()->getParameter('view_id');
  if ($view_id) {
    $variables['attributes']['class'][] = 'page-view-' . Html::cleanCssIdentifier($view_id);
  $http_equiv = [
Alberto Siles's avatar
Alberto Siles committed
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => [
Alberto Siles's avatar
Alberto Siles committed
      'http-equiv' => 'x-ua-compatible',
      'content' => 'ie=edge',
Alberto Siles's avatar
Alberto Siles committed
  $variables['page']['#attached']['html_head'][] = [$http_equiv, 'http-equiv'];
Alberto Siles's avatar
Alberto Siles committed
}

 * Implements hook_page_attachments_alter() for page templates.
function bootstrap_barrio_page_attachments_alter(&$page) {
  $viewport = [
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => [
      'name' => 'viewport',
      'content' => 'width=device-width, initial-scale=1, shrink-to-fit=no',
  $page['#attached']['html_head'][] = [$viewport, 'viewport'];
}

Alberto Siles's avatar
Alberto Siles committed
/**
 * Implements hook_preprocess_HOOK() for page templates.
 */
function bootstrap_barrio_preprocess_page(&$variables) {
  if (\Drupal::moduleHandler()->moduleExists('bootstrap_library')) {
    $library = theme_get_setting('bootstrap_barrio_library');
    switch ($library) {
        $variables['#attached']['library'][] = 'bootstrap_library/bootstrap-cdn';
        break;
      case 'production':
        $variables['#attached']['library'][] = 'bootstrap_library/bootstrap';
        break;
      case 'development':
        $variables['#attached']['library'][] = 'bootstrap_library/bootstrap-dev';
        break;
Alberto Siles's avatar
Alberto Siles committed
  $fonts = theme_get_setting('bootstrap_barrio_google_fonts');
  if ($fonts) {
    $variables['#attached']['library'][] = 'bootstrap_barrio/' . $fonts;
  }

  $icons = theme_get_setting('bootstrap_barrio_bootstrap_icons');
  if ($icons) {
    $variables['#attached']['library'][] = 'bootstrap_barrio/bootstrap-icons';
  $icons = theme_get_setting('bootstrap_barrio_icons');
  if ($icons) {
    $variables['#attached']['library'][] = 'bootstrap_barrio/' . $icons;
  }

  $system_messages = theme_get_setting('bootstrap_barrio_system_messages');
  if ($system_messages) {
    $variables['#attached']['library'][] = 'bootstrap_barrio/' . $system_messages;
  }

  $content_width = 'col';
Alberto Siles's avatar
Alberto Siles committed
  $sidebar_first_width = 'col-md-' . theme_get_setting('bootstrap_barrio_sidebar_first_width');
  $sidebar_second_width = 'col-md-' . theme_get_setting('bootstrap_barrio_sidebar_second_width');

  // Define default classes for containers.
  $variables['navbar_top_attributes'] = [
    'class' => ['navbar'],
    'id' => ['navbar-top'],
  ];
  $variables['navbar_attributes'] = [
    'class' => ['navbar'],
    'id' => ['navbar-main'],
  ];
  $variables['content_attributes'] = [
    'class' => ['main-content', $content_width],
    'id' => ['content'],
    'role' => ['main'],
  ];
  $variables['sidebar_first_attributes'] = [
    'class' => ['sidebar_first', 'sidebar', $sidebar_first_width],
  ];
  $variables['sidebar_second_attributes'] = [
    'class' => ['sidebar_second', 'sidebar', $sidebar_second_width],
Alberto Siles's avatar
Alberto Siles committed

  // Get custom Navbar classes from config and add them to Navbars.
  if (theme_get_setting('bootstrap_barrio_navbar_top_class') !== NULL) {
    $variables['navbar_top_attributes']['class'][] = theme_get_setting('bootstrap_barrio_navbar_top_class');
  }
  if (theme_get_setting('bootstrap_barrio_navbar_class') !== NULL) {
    $variables['navbar_attributes']['class'][] = theme_get_setting('bootstrap_barrio_navbar_class');
  }
Alberto Siles's avatar
Alberto Siles committed

  $affix_navbar_top = theme_get_setting('bootstrap_barrio_navbar_top_affix');
  $affix_navbar = theme_get_setting('bootstrap_barrio_navbar_affix');
  $affix_sidebar_first = theme_get_setting('bootstrap_barrio_sidebar_first_affix');
  $affix_sidebar_second = theme_get_setting('bootstrap_barrio_sidebar_second_affix');
  if ($affix_navbar_top || $affix_navbar || $affix_sidebar_first || $affix_sidebar_second) {
    $variables['#attached']['library'][] = 'bootstrap_barrio/affix';
    if ($affix_navbar_top) {
      $variables['navbar_top_attributes']['data-toggle'] = 'affix';
      $variables['navbar_attributes']['data-toggle'] = 'affix';
    }
    if ($affix_sidebar_first) {
      $variables['sidebar_first_attributes']['data-toggle'] = 'affix';
    }
    if ($affix_sidebar_second) {
      $variables['sidebar_second_attributes']['data-toggle'] = 'affix';
Alberto Siles's avatar
Alberto Siles committed
  if (theme_get_setting('bootstrap_barrio_content_offset')) {
    $variables['content_attributes']['class'][] = 'offset-md-' . theme_get_setting('bootstrap_barrio_content_offset');
Alberto Siles's avatar
Alberto Siles committed
  }
  if (theme_get_setting('bootstrap_barrio_sidebar_first_offset')) {
    $variables['sidebar_first_attributes']['class'][] = 'offset-md-' . theme_get_setting('bootstrap_barrio_sidebar_first_offset');
Alberto Siles's avatar
Alberto Siles committed
  }
  if (theme_get_setting('bootstrap_barrio_sidebar_second_offset')) {
    $variables['sidebar_second_attributes']['class'][] = 'offset-md-' . theme_get_setting('bootstrap_barrio_sidebar_second_offset');
  // Define column layout classes.
Alberto Siles's avatar
Alberto Siles committed
  switch (theme_get_setting('bootstrap_barrio_sidebar_position')) {
    case 'left':
      $variables['sidebar_first_attributes']['class'][] = 'order-first';
      $variables['content_attributes']['class'][] = 'order-last';
      break;

    case 'both':
      $variables['sidebar_first_attributes']['class'][] = 'order-first';
      $variables['sidebar_second_attributes']['class'][] = 'order-last';
      break;

    case 'right':
      $variables['content_attributes']['class'][] = 'order-first';
      $variables['sidebar_second_attributes']['class'][] = 'order-last';
      break;
  // Define navigation position.
  $variables['navbar_top_attributes']['class'][] = theme_get_setting('bootstrap_barrio_navbar_top_position');
  $variables['navbar_attributes']['class'][] = theme_get_setting('bootstrap_barrio_navbar_position');
  // Define navigation color scheme.
  $variables['navbar_top_attributes']['class'][] = theme_get_setting('bootstrap_barrio_navbar_top_color');
  $variables['navbar_attributes']['class'][] = theme_get_setting('bootstrap_barrio_navbar_color');
  // Define navigation background classes.
  $variables['navbar_top_attributes']['class'][] = theme_get_setting('bootstrap_barrio_navbar_top_background');
  $variables['navbar_attributes']['class'][] = theme_get_setting('bootstrap_barrio_navbar_background');

  // Define navigation toggle size.
  switch (theme_get_setting('bootstrap_barrio_navbar_toggle')) {
    case 'navbar-toggleable-lg':
      $variables['navbar_attributes']['class'][] = 'navbar-expand-lg';
      break;
    case 'navbar-toggleable-md':
      $variables['navbar_attributes']['class'][] = 'navbar-expand-md';
      break;
    case 'navbar-toggleable-sm':
      $variables['navbar_attributes']['class'][] = 'navbar-expand-sm';
      break;
    case 'navbar-toggleable-xs':
      $variables['navbar_attributes']['class'][] = 'navbar-expand-xs';
      break;
    default:
      $variables['navbar_attributes']['class'][] = 'navbar-expand-md';
      break;
  if (theme_get_setting('bootstrap_barrio_sidebar_collapse')) {
    $variables['content_attributes']['class'][] = 'sidebar-offcanvas';
    $variables['sidebar_collapse'] = TRUE;
    $variables['#attached']['library'][] = 'bootstrap_barrio/sidebar_collapse';
  }

  // Declare region attributes.
Alberto Siles's avatar
Alberto Siles committed
  $variables['navbar_top_attributes'] = new Attribute($variables['navbar_top_attributes']);
  $variables['navbar_attributes'] = new Attribute($variables['navbar_attributes']);
  $variables['content_attributes'] = new Attribute($variables['content_attributes']);
  $variables['sidebar_first_attributes'] = new Attribute($variables['sidebar_first_attributes']);
  $variables['sidebar_second_attributes'] = new Attribute($variables['sidebar_second_attributes']);

  // Declare container layout.
  $variables['container_navbar'] = theme_get_setting('bootstrap_barrio_navbar_container');
Alberto Siles's avatar
Alberto Siles committed
  $variables['container'] = theme_get_setting('bootstrap_barrio_fluid_container') ? 'container-fluid' : 'container';

}

/**
 * Implements hook_preprocess_HOOK() for page templates.
 */
function bootstrap_barrio_preprocess_page_title(&$variables) {
  // Since the title and the shortcut link are both block level elements,
  // positioning them next to each other is much simpler with a wrapper div.
  if (!empty($variables['title_suffix']['add_or_remove_shortcut']) && $variables['title']) {
    // Add a wrapper div using the title_prefix and title_suffix render
    // elements.
    $variables['title_prefix']['shortcut_wrapper'] = [
Alberto Siles's avatar
Alberto Siles committed
      '#markup' => '<div class="shortcut-wrapper clearfix">',
      '#weight' => 100,
    ];
    $variables['title_suffix']['shortcut_wrapper'] = [
Alberto Siles's avatar
Alberto Siles committed
      '#markup' => '</div>',
      '#weight' => -99,
Alberto Siles's avatar
Alberto Siles committed
    // Make sure the shortcut link is the first item in title_suffix.
    $variables['title_suffix']['add_or_remove_shortcut']['#weight'] = -100;
  }
}

/**
 * Implements hook_preprocess_HOOK() for maintenance-page.html.twig.
 */
function bootstrap_barrio_preprocess_maintenance_page(&$variables) {
  // By default, site_name is set to Drupal if no db connection is available
  // or during site installation. Setting site_name to an empty string makes
  // the site and update pages look cleaner.
  // @see template_preprocess_maintenance_page
  if (!$variables['db_is_active']) {
    $variables['site_name'] = '';
  }

  // Bootstrap Barrio has custom styling for the maintenance page.
  $variables['#attached']['library'][] = 'bootstrap_barrio/maintenance_page';
}

/**
 * Implements hook_preprocess_HOOK() for node.html.twig.
 */
function bootstrap_barrio_preprocess_node(&$variables) {
  // Remove the "Add new comment" link on teasers or when the comment form is
  // displayed on the page.
  if ($variables['teaser'] || !empty($variables['content']['comments']['comment_form'])) {
    unset($variables['content']['links']['comment']['#links']['comment-add']);
  }
}

/**
 * Implements hook_preprocess_HOOK() for region.html.twig.
 */
function bootstrap_barrio_preprocess_region(&$variables) {
  $nowrap = [
    'breadcrumb',
    'highlighted',
    'content',
    'primary_menu',
    'header',
    'sidebar_first',
    'sidebar_second',
  ];
  if (theme_get_setting('bootstrap_barrio_region_class_' . $variables['elements']['#region']) !== NULL) {
    $variables['attributes']['class'][] = theme_get_setting('bootstrap_barrio_region_class_' . $variables['elements']['#region']);
  }
  elseif (!in_array($variables['elements']['#region'], $nowrap)) {
    $variables['attributes']['class'][] = 'row';
Alberto Siles's avatar
Alberto Siles committed
/**
 * Implements hook_preprocess_HOOK() for block.html.twig.
 */
function bootstrap_barrio_preprocess_block(&$variables) {
  if ($variables['base_plugin_id'] == 'system_menu_block' && isset($variables['attributes']['id'])) {
    $variables['content']['#attributes']['block'] = $variables['attributes']['id'];
  }
Alberto Siles's avatar
Alberto Siles committed
  // Add a clearfix class to system branding blocks.
  if ($variables['plugin_id'] == 'system_branding_block') {
    $variables['attributes']['class'][] = 'clearfix';
  }
}

/**
 * Implements hook_preprocess_HOOK() for form elements.
 */
function bootstrap_barrio_preprocess_form_element(&$variables) {
  if (isset($variables['label']['#title'])) {
    if (is_object($variables['label']['#title']) && !$variables['label']['#title'] instanceof MarkupInterface) {
    elseif (is_object($variables['label']['#title'])) {
      $variables['input_title'] = $variables['label']['#title'];
    }
    else {
      $variables['input_title'] = $variables['label']['#title'];
    }
  if (isset($variables['element']['#attributes'])) {
    $variables['input_attributes'] = new Attribute($variables['element']['#attributes']);
  }
  if (isset($variables['type'])) {
    switch ($variables['type']) {
      case 'checkbox':
        $variables['customtype'] = theme_get_setting('bootstrap_barrio_checkbox');
        break;
      case 'radio':
        $variables['customtype'] = theme_get_setting('bootstrap_barrio_radio');
        break;
  }
}

/**
 * Implements hook_preprocess_HOOK() for form-element-label.html.twig.
 */
function bootstrap_barrio_preprocess_form_element_label(&$variables) {
  $element = $variables['element'];
}

/**
 * Implements hook_preprocess_HOOK() for select.html.twig.
 */
function bootstrap_barrio_preprocess_select(&$variables) {
  $variables['customtype'] = theme_get_setting('bootstrap_barrio_select');
}

/**
 * Implements hook_preprocess_HOOK() for file-managed-file.html.twig.
 */
function bootstrap_barrio_preprocess_file_managed_file(&$variables) {
  $variables['customtype'] = theme_get_setting('bootstrap_barrio_file');
Alberto Siles's avatar
Alberto Siles committed
/**
 * Implements hook_preprocess_HOOK() for menu.html.twig.
 */
function bootstrap_barrio_preprocess_menu(&$variables) {
  $variables['attributes']['class'][] = 'clearfix';
}

/**
 * Implements hook_preprocess_HOOK() for menu-local-task.html.twig.
 */
Alberto Siles's avatar
Alberto Siles committed
function bootstrap_barrio_preprocess_menu_local_task(&$variables) {
  $link = $variables['element']['#link'];
  $url = $link['url'];
  $options = $url->getOptions();
  $url->setOptions($options + $link['localized_options']);
  $variables['item'] = Link::fromTextAndUrl($link['title'], $url);
/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function bootstrap_barrio_theme_suggestions_menu_alter(array &$suggestions, array $variables) {
  // Taken from http://kristiankaa.dk/article/drupal8-region-specific-menu-theme-hook-suggestion
  // bootstrap_barrio_preprocess_block(&$variables)
  if (isset($variables['attributes']['block'])) {
    $hook = str_replace(['block-', '-'], ['', '_'], $variables['attributes']['block']);
    $suggestions[] = $variables['theme_hook_original'] . '__' . $hook;
  }
}

Alberto Siles's avatar
Alberto Siles committed
/**
 * Implements hook_preprocess_HOOK() for menu-local-action.html.twig.
 */
function bootstrap_barrio_preprocess_menu_local_action(&$variables) {
  $link = $variables['element']['#link'];
  $link += [
    'localized_options' => [],
  ];
Alberto Siles's avatar
Alberto Siles committed
  $link['localized_options']['attributes']['class'][] = 'btn';
  $link['localized_options']['attributes']['class'][] = 'btn-secondary';
  $link['localized_options']['set_active_class'] = TRUE;
  $variables['link'] = [
Alberto Siles's avatar
Alberto Siles committed
    '#type' => 'link',
    '#title' => $link['title'],
    '#options' => $link['localized_options'],
    '#url' => $link['url'],
/**
 * Implements template_preprocess_table()
 */
function bootstrap_barrio_preprocess_table(&$variables) {
  $variables['table_class'] = theme_get_setting('bootstrap_barrio_table_style');
  $variables['hover'] = theme_get_setting('bootstrap_barrio_table_hover');
  $variables['thead_class'] = theme_get_setting('bootstrap_barrio_table_head');
}

/**
 * Implements template_preprocess_views_view_table()
 */
function bootstrap_barrio_preprocess_views_view_table(&$variables) {
  $variables['table_class'] = theme_get_setting('bootstrap_barrio_table_style');
  $variables['hover'] = theme_get_setting('bootstrap_barrio_table_hover');
  $variables['thead_class'] = theme_get_setting('bootstrap_barrio_table_head');
}

/**
 * Implements hook_theme_suggestions_HOOK_alter() for region templates.
 */
function bootstrap_barrio_theme_suggestions_region_alter(array &$suggestions, array $variables) {
  $nowrap = [
    'breadcrumb',
    'highlighted',
    'primary_menu',
    'sidebar_first',
    'sidebar_second',
  ];
  if (theme_get_setting('bootstrap_barrio_region_clean_' . $variables['elements']['#region']) !== NULL) {
    $region_clean = theme_get_setting('bootstrap_barrio_region_clean_' . $variables['elements']['#region']);
  }
  else {
    $region_clean = in_array($variables['elements']['#region'], $nowrap);
  }
    $suggestions[] = 'region__nowrap';
  }
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function bootstrap_barrio_theme_suggestions_user_alter(array &$suggestions, array $variables, $hook) {
  // Define the view mode.
  $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
  $user_id = \Drupal::service('current_user')->id();
  $suggestions[] = 'user__' . $sanitized_view_mode;
  $suggestions[] = 'user__' . $user_id;
  $suggestions[] = 'user__' . $user_id . '__' . $sanitized_view_mode;

  return $suggestions;
}

 * Implements hook_theme_suggestions_HOOK_alter() for page template.
function bootstrap_barrio_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  // Add a template suggestions for node type.
  /** @var \Drupal\node\NodeInterface $node */
  if ($node = \Drupal::routeMatch()->getParameter('node')) {
    if (is_numeric($node)) {
      $node = Node::load($node);
    }

    $suggestions[] = 'page__node__' . $node->bundle();
  }
}

Alberto Siles's avatar
Alberto Siles committed
/**
 * Implements hook_theme_HOOK_form_alter() for node add/edit.
Alberto Siles's avatar
Alberto Siles committed
 */
function bootstrap_barrio_form_node_form_alter(&$form, FormStateInterface $form_state) {
  $form['#theme'] = ['node_edit_form'];
}

/**
 * Implements hook_theme_suggestions_HOOK_alter() for links templates.
 */
function bootstrap_barrio_theme_suggestions_links_alter(array &$suggestions, array $variables) {
  // ToDo get from core a variable to flag inline links.
  if (isset($variables['attributes']['class'])) {
    if (in_array('inline', $variables['attributes']['class'])) {
      $suggestions[] = 'links__inline';
    }
/**
 * Implements hook_theme_suggestions_HOOK_alter() for status messages templates.
 */
function bootstrap_barrio_theme_suggestions_status_messages_alter(array &$suggestions, array $variables) {
  switch (theme_get_setting('bootstrap_barrio_messages_widget')) {
    case 'toasts':
      $suggestions[] = 'status_messages__toasts';
      break;
    case 'alerts':
      $suggestions[] = 'status_messages__alerts';
      break;
/**
 * Implements hook_preprocess_HOOK() for status_messages.
 */
function bootstrap_barrio_preprocess_status_messages(&$variables) {
  $variables['title_ids'] = [];
  foreach ($variables['message_list'] as $message_type => $messages) {
    $variables['title_ids'][$message_type] = Html::getUniqueId("message-$message_type-title");
  }
}

Alberto Siles's avatar
Alberto Siles committed
/**
 * Implements hook_theme_suggestions_HOOK_alter() for form templates.
 */
function bootstrap_barrio_theme_suggestions_form_alter(array &$suggestions, array $variables) {
  if ($variables['element']['#form_id'] == 'search_block_form') {
    $suggestions[] = 'form__search_block_form';
  }
  if ($variables['element']['#form_id'] == 'node_form') {
    $suggestions[] = 'node_edit_form';
  }
}

Alberto Siles's avatar
Alberto Siles committed
/**
 * Implements hook_theme_suggestions_HOOK_alter() for form templates.
 */
function bootstrap_barrio_theme_suggestions_input_alter(array &$suggestions, array $variables) {
  if ($variables['element']['#type'] == 'submit' && theme_get_setting('bootstrap_barrio_button')) {
    $suggestions[] = 'input__submit_button';
  }
}

Alberto Siles's avatar
Alberto Siles committed
/**
 * Implements hook_form_alter() to add classes to the search form.
 */
/*
 * function bootstrap_barrio_form_alter(
 * &$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
 *  if (in_array($form_id, ['search_block_form', 'search_form'])) {
 *    $key = ($form_id == 'search_block_form') ? 'actions' : 'basic';
 *    if (!isset($form[$key]['submit']['#attributes'])) {
 *      $form[$key]['submit']['#attributes'] = new Attribute();
 *    }
 *    $outline = theme_get_setting('bootstrap_barrio_button_outline') ?
 *    'btn-outline-primary' : 'btn-primary';
 * If use the $outline variable, HTML crashes. Even if use the value directly
 *    $form[$key]['submit']['#attributes']->addClass(
 *    ['btn', 'btn-primary', 'btn-sm']);
Alberto Siles's avatar
Alberto Siles committed
 */
Alberto Siles's avatar
Alberto Siles committed
/**
 * Implements hook_preprocess_input() for adding classes to inputs.
Alberto Siles's avatar
Alberto Siles committed
 */
function bootstrap_barrio_preprocess_input(&$variables) {
  if (isset($variables['attributes']['class'])) {
    if (is_object($variables['attributes']['class'])) {
      if (in_array('error', $variables['attributes']['class']->value())) {
        $variables['attributes']['class']->addClass('is-invalid');
      }
Alberto Siles's avatar
Alberto Siles committed
    }
    elseif (is_array($variables['attributes']['class'])) {
      if (in_array('error', $variables['attributes']['class'])) {
        $variables['attributes']['class'][] = 'is-invalid';
      }
Alberto Siles's avatar
Alberto Siles committed
    }
  }
Alberto Siles's avatar
Alberto Siles committed
  if ($variables['element']['#type'] == 'submit') {
    $button_size = theme_get_setting('bootstrap_barrio_button_size');
    $button_types = [
      'btn-primary',
      'btn-secondary',
      'btn-success',
      'btn-warning',
      'btn-danger',
      'btn-link',
    ];
    $button_sizes = ['btn-sm', 'btn-lg'];
    $button_is_outline = theme_get_setting('bootstrap_barrio_button_outline');
Alberto Siles's avatar
Alberto Siles committed
    if (is_object($variables['attributes']['class'])) {
      \Drupal::logger('bootstrap_barrio')->notice(implode($variables['attributes']['class']->value()));
      if (!in_array('btn', $variables['attributes']['class']->value())) {
        $variables['attributes']['class']->addClass('btn');
      }
      if (empty(array_intersect($button_types, $variables['attributes']['class']->value()))) {
Alberto Siles's avatar
Alberto Siles committed
        if ($button_is_outline) {
          $variables['attributes']['class']->addClass('btn-outline-primary');
Alberto Siles's avatar
Alberto Siles committed
        }
        else {
          $variables['attributes']['class']->addClass('btn-primary');
        }
      }
      if ($button_size && empty(array_intersect($button_sizes, $variables['attributes']['class']->value()))) {
        $variables['attributes']['class']->addClass($button_size);
      if (!in_array('btn', $variables['attributes']['class'])) {
        $variables['attributes']['class'][] = 'btn';
Alberto Siles's avatar
Alberto Siles committed
      }
      if (empty(array_intersect($button_types, $variables['attributes']['class']))) {
        if ($button_is_outline) {
          $variables['attributes']['class'][] = 'btn-outline-primary';
        }
        else {
          $variables['attributes']['class'][] = 'btn-primary';
        }
Alberto Siles's avatar
Alberto Siles committed
      }
      if ($button_size && empty(array_intersect($button_sizes, $variables['attributes']['class']))) {
Alberto Siles's avatar
Alberto Siles committed
        $variables['attributes']['class'][] = $button_size;
      }
      $variables['attributes'] = new Attribute($variables['attributes']);
Alberto Siles's avatar
Alberto Siles committed
    }
    $variables['safe_value_label'] = Xss::filter($variables['attributes']['value']);
Alberto Siles's avatar
Alberto Siles committed
  }
}