Skip to content
Snippets Groups Projects
wcm8_theme.theme 5.54 KiB
Newer Older
M Miller's avatar
M Miller committed
<?php

/**
 * @file
 * Functions to support theming in the wcm8_theme theme.
 */

/**
  * Adds additional theme settings.
  */
  function wcm8_theme_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id = NULL) {
    // Work-around for a core bug affecting admin themes. See issue #943212.
    if (isset($form_id)) {
      return;
    }

    $form['theme_customization'] = array (
      '#type'     => 'fieldset',
      '#title'    => t('Theme Customization'),
      '#markup'   => t('Customize the look and feel of the site.')
    );

    $form['theme_customization']['header'] = array(
      '#type'          => 'fieldset',
      '#title'         => t('Header')
    );

    $navbar_color_options = [
      'light' => t('Light'),
      'dark'  => t('Dark')
    ];

    $form['theme_customization']['header']['osu_navbar_color'] = array(
      '#type'          => 'radios',
      '#title'         => t('OSU Navbar Color'),
      '#default_value' => theme_get_setting('osu_navbar_color'),
      '#options'       => $navbar_color_options
    );

   $masthead_variant_options = [
     'standard' => t('Standard'),
     'slim'     => t('Slim')
   ];

   $form['theme_customization']['header']['masthead_variant'] = array(
     '#type'          => 'radios',
     '#title'         => t('Masthead Variant'),
     '#default_value' => theme_get_setting('masthead_variant'),
     '#options'       => $masthead_variant_options
   );

    $masthead_color_options = [
      'dk-gray' => t('Dark Gray'),
      'md-gray' => t('Medium Gray'),
      'lt-gray' => t('Light Gray'),
      'white'   => t('White')
    ];

    $form['theme_customization']['header']['masthead_color'] = array(
      '#type'          => 'radios',
      '#title'         => t('Masthead Color'),
      '#default_value' => theme_get_setting('masthead_color'),
      '#options'       => $masthead_color_options
    );

    $form['theme_customization']['footer'] = array(
      '#type'          => 'fieldset',
      '#title'         => t('Footer')
    );

    $footer_color_options = [
      'dk-gray' => t('Dark Gray'),
      'md-gray' => t('Medium Gray'),
      'lt-gray' => t('Light Gray'),
      'white'   => t('White')
    ];

    $form['theme_customization']['footer']['footer_color'] = array(
      '#type'          => 'radios',
      '#title'         => t('Footer Color'),
      '#default_value' => theme_get_setting('footer_color'),
      '#options'       => $footer_color_options
    );

  }

  /**
   * Clear caches when settings change.
   */
  function wcm8_theme_settings_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    drupal_flush_all_caches();
  }

  /**
   * Implements hook_preprocess().
   */
  function wcm8_theme_preprocess(array &$variables, $hook) {
    //all-purpose variables
    $variables['base_path'] = base_path();

    //theme setting variables
    $variables['osu_navbar_color'] = theme_get_setting('osu_navbar_color');
    $variables['masthead_variant'] = theme_get_setting('masthead_variant');
    $variables['masthead_color'] = theme_get_setting('masthead_color');
    $variables['footer_color'] = theme_get_setting('footer_color');

    // Load the site name out of configuration.
    $config = \Drupal::config('system.site');
    $variables['site_name'] = $config->get('name');

    // Load variables from Site Information module.
    $siteinfo_config = \Drupal::config('wcm8_siteinfo.settings');
    $siteinfo_vars = [
      'site_name_prefix',
      'admin_link',
      'footer_text',
      'address_1',
      'address_2',
      'city',
      'state',
      'zip',
      'phone',
      'fax',
      'contact_email',
      'ada_email',
      'twitter',
      'facebook',
      'linkedin',
      'youtube',
      'instagram',
      'photos'
    ];
    foreach ($siteinfo_vars as $siteinfo_var) {
      $variables[$siteinfo_var] = $siteinfo_config->get($siteinfo_var);
    }
  }



/**
 * Implements hook_preprocess_html().
 */
function wcm8_theme_preprocess_html(array &$variables) {
  // Add footer color to body classes.
  $variables['attributes']['class'][] = 'footer-color--' . theme_get_setting('footer_color');
  // Add node id to body classes.
M Miller's avatar
M Miller committed
  if (\Drupal::routeMatch()->getRouteName() == 'entity.node.canonical') {
    $node = \Drupal::routeMatch()->getParameter('node');
    $variables['attributes']['class'][] = 'nid--' . $node->id();
M Miller's avatar
M Miller committed
  }
}

/**
 * Implements hook_preprocess_page().
 */
function wcm8_theme_preprocess_page(&$variables) {
  $variables['site_name'] = \Drupal::config('system.site')->get('name');
}

/**
 * Implements hook_theme_suggestions_alter().
 */
function wcm8_theme_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  if ($node = \Drupal::routeMatch()->getParameter('node')) {
    $content_type = $node->bundle();
  }

  if ($hook == 'form' & !empty($variables['element']['#id'])) {
    $suggestions[] = 'form__' . str_replace('-', '_', $variables['element']['#id']);
  }

  if ($hook == 'details' & !empty($variables['element']['#id'])) {
    $suggestions[] = 'details__'. $content_type;
  }
}

/**
 * Implements hook_theme_suggestions_block_alter().
 */
function wcm8_theme_theme_suggestions_block_alter(array &$suggestions, array $variables) {
  if (isset($variables['elements']['content']['#block_content'])) {
    array_splice($suggestions, 1, 0, 'block__' . $variables['elements']['content']['#block_content']->bundle());
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
 function wcm8_theme_preprocess_details(array &$variables, $hook) {
   if ($node = \Drupal::request()->attributes->get('node')) {
     $variables['nid'] = $node->id();
   }
 }