<?php
/**
 * @file
 * Bootstrap sub-theme.
 *
 * Functions to support theming in the Corporate-Blue theme.
 */

/**
 * Implements hook_form_system_theme_settings_alter().
 */
function corporate_blue_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {

  // Social Icon Link.
  $form['corporate_blue_settings']['social_icon'] = array(
    '#type' => 'details',
    '#title' => t('Social Media Link'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['corporate_blue_settings']['social_icon']['show_social_icon'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Social Icons'),
    '#default_value' => theme_get_setting('show_social_icon'),
    '#description'   => t("Show/Hide social media links"),
  );
  $form['corporate_blue_settings']['social_icon']['facebook_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Facebook Link'),
    '#default_value' => theme_get_setting('facebook_url'),
  );
  $form['corporate_blue_settings']['social_icon']['google_plus_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Google plus Link'),
    '#default_value' => theme_get_setting('theme.settings.google_plus_url'),
  );
  $form['corporate_blue_settings']['social_icon']['twitter_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Twitter Link'),
    '#default_value' => theme_get_setting('twitter_url'),
  );
  $form['corporate_blue_settings']['social_icon']['linkedin_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Linkedin Link'),
    '#default_value' => theme_get_setting('linkedin_url'),
  );
  $form['corporate_blue_settings']['social_icon']['pinterest_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Pinterest Link'),
    '#default_value' => theme_get_setting('pinterest_url'),
  );
  $form['corporate_blue_settings']['social_icon']['rss_url'] = array(
    '#type' => 'textfield',
    '#title' => t('RSS Link'),
    '#default_value' => theme_get_setting('rss_url'),
  );

  // Social Icon Link.
  $form['corporate_blue_settings']['showcase_social_icon'] = array(
    '#type' => 'details',
    '#title' => t('Showcase Social Media Links and Counts'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['corporate_blue_settings']['showcase_social_icon']['showcase_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Showcase Title'),
    '#default_value' => theme_get_setting('showcase_title'),
  );
  $form['corporate_blue_settings']['showcase_social_icon']['showcase_description'] = array(
    '#type' => 'textfield',
    '#title' => t('Showcase Description'),
    '#default_value' => theme_get_setting('showcase_description'),
  );
  $form['corporate_blue_settings']['showcase_social_icon']['showcase_facebook_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Facebook Link'),
    '#default_value' => theme_get_setting('showcase_facebook_url'),
  );
  $form['corporate_blue_settings']['showcase_social_icon']['showcase_facebook_count'] = array(
    '#type' => 'textfield',
    '#title' => t('Facebook Count'),
    '#default_value' => theme_get_setting('showcase_facebook_count'),
  );
  $form['corporate_blue_settings']['showcase_social_icon']['showcase_twitter_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Twitter Link'),
    '#default_value' => theme_get_setting('showcase_twitter_url'),
  );
  $form['corporate_blue_settings']['showcase_social_icon']['showcase_twitter_count'] = array(
    '#type' => 'textfield',
    '#title' => t('Twitter Count'),
    '#default_value' => theme_get_setting('showcase_twitter_count'),
  );
  $form['corporate_blue_settings']['showcase_social_icon']['showcase_linkedin_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Linkedin Link'),
    '#default_value' => theme_get_setting('showcase_linkedin_url'),
  );
  $form['corporate_blue_settings']['showcase_social_icon']['showcase_linkedin_count'] = array(
    '#type' => 'textfield',
    '#title' => t('Linkedin Count'),
    '#default_value' => theme_get_setting('showcase_linkedin_count'),
  );

  // Custom submit to save the file permanent.
  $form['#submit'][] = 'corporate_blue_settings_form_submit';

}

/**
 * Implements hook_preprocess_html().
 */
function corporate_blue_preprocess_html(&$variables) {
  $showcase_title = theme_get_setting('showcase_title', 'corporate_blue');
  $showcase_description = theme_get_setting('showcase_description', 'corporate_blue');
  $showcase_twitter_url = theme_get_setting('showcase_twitter_url', 'corporate_blue');
  $showcase_twitter_count = theme_get_setting('showcase_twitter_count', 'corporate_blue');
  $showcase_facebook_url = theme_get_setting('showcase_facebook_url', 'corporate_blue');
  $showcase_facebook_count = theme_get_setting('showcase_facebook_count', 'corporate_blue');
  $showcase_linkedin_url = theme_get_setting('showcase_linkedin_url', 'corporate_blue');
  $showcase_linkedin_count = theme_get_setting('showcase_linkedin_count', 'corporate_blue');
  if (!empty($showcase_title)) {
    $variables['showcase_title'] = $showcase_title;
  }
  if (!empty($showcase_description)) {
    $variables['showcase_description'] = $showcase_description;
  }
  if (!empty($showcase_twitter_url)) {
    $variables['showcase_twitter_url'] = $showcase_twitter_url;
  }
  if (!empty($showcase_twitter_count)) {
    $variables['showcase_twitter_count'] = $showcase_twitter_count;
  }
  if (!empty($showcase_facebook_url)) {
    $variables['showcase_facebook_url'] = $showcase_facebook_url;
  }
  if (!empty($showcase_facebook_count)) {
    $variables['showcase_facebook_count'] = $showcase_facebook_count;
  }
  if (!empty($showcase_linkedin_url)) {
    $variables['showcase_linkedin_url'] = $showcase_linkedin_url;
  }
  if (!empty($showcase_linkedin_count)) {
    $variables['showcase_linkedin_count'] = $showcase_linkedin_count;
  }

  // Add block in a region.
  $block = \Drupal\block\Entity\Block::load('corporate_blue_footer');
  $block_content = \Drupal::entityManager()->getViewBuilder('block')->view($block);
  $variables['page']['footer'][] = $block_content;
}
