You are here

improved_theme_settings.module in Open Social 8.2

Improved theme settings module file.

File

modules/custom/improved_theme_settings/improved_theme_settings.module
View source
<?php

/**
 * @file
 * Improved theme settings module file.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Render\Markup;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function improved_theme_settings_form_system_theme_settings_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['logo']['default_logo']['#description'] = t('This logo is shown in the platform header and on emails sent to your users. It can be overriden in the above E-mail setting');

  // If user has the improved settings, we don't alter nothing.
  if (\Drupal::currentUser()
    ->hasPermission('administer improved theme settings')) {
    return;
  }

  // Our form contains all the correct id's so we can start the alteration.
  if (!empty($form['open_social_settings']) && !empty($form['os_color_settings']) && !empty($form['color'])) {

    // Unset theme updates.
    $form['update']['#access'] = FALSE;

    // Unset Bootstrap settings, we don't need it.
    $form['bootstrap']['#access'] = FALSE;

    // Unset all the settings that are part of the bootstrap group.
    foreach ($form as $setting => $settings) {
      if (!empty($settings['#group']) && $settings['#group'] == "bootstrap") {
        $form[$setting]['#access'] = FALSE;
      }
    }

    // We want color to be rendered under open social settings in a tab.
    $form['color']['#group'] = "open_social_settings";
  }
}

/**
 * Implements hook_page_attachments().
 */
function improved_theme_settings_page_attachments(array &$page) {

  // Check for the current active theme.
  $active_theme = \Drupal::theme()
    ->getActiveTheme()
    ->getName();

  // Determine the admin theme from config.
  $config = \Drupal::config('system.theme');

  // If we're in the admin theme we need to add a library.
  if ($active_theme == $config
    ->get('admin')) {
    $page['#attached']['library'][] = 'improved_theme_settings/color.admin';
  }

  // If socialblue is our default theme.
  $system_theme_settings = \Drupal::configFactory()
    ->get('system.theme')
    ->get('default');

  // We render our color & font settings in the head. Only if socialblue is
  // the default theme. And only if the current active theme is socialblue.
  // Otherwise we also render it on all admin pages.
  if (array_key_exists('socialbase', \Drupal::service('theme.manager')
    ->getActiveTheme()
    ->getBaseThemes())) {
    if ($active_theme == $system_theme_settings) {
      $style_to_add = '';
      if (\Drupal::moduleHandler()
        ->moduleExists('social_font')) {

        // Render font value here.
        $style_to_add .= social_font_render();
      }
      $card_radius = improved_theme_settings_get_setting('card_radius', $system_theme_settings);
      if ($card_radius >= 0) {
        $style_to_add .= '
        .card,
        .card-radius,
        .hero-canvas {
          border-radius: ' . $card_radius . 'px !important;
        }

        .tabs-left .vertical-tabs-list {
          border-radius: ' . $card_radius . 'px 0 0 ' . $card_radius . 'px !important;
        }

        .teaser--stream:last-child,
        .card__block:last-child,
        .card__nested-section:last-child {
          border-bottom-left-radius: ' . $card_radius . 'px !important;
          border-bottom-right-radius: ' . $card_radius . 'px !important;
        }

        .teaser--stream:last-child .teaser__image {
          border-bottom-left-radius: ' . $card_radius . 'px !important;
        }

        .card__block:first-child {
          border-top-left-radius: ' . $card_radius . 'px !important;
          border-top-right-radius: ' . $card_radius . 'px !important;
        }

        @media (min-width: 600px) {
          .teaser__teaser-type {
            border-radius: 0 0 ' . $card_radius . 'px 0 !important;
          }
        }

        @media (min-width: 900px) {
          .off-canvas-xs-only {
            border-radius: ' . $card_radius . 'px !important;
          }
        }

        .message__message-body {
          border-radius: ' . $card_radius / 2 . 'px !important;
        }
        ';
      }
      $form_control_radius = improved_theme_settings_get_setting('form_control_radius', $system_theme_settings);
      if ($form_control_radius >= 0) {
        $style_to_add .= '
          .form-control-radius,
          .form-control {
            border-radius: ' . $form_control_radius . 'px !important;
          }

          .search-take-over .form-control {
            border-radius: 0 !important;
          }

          .input-group .form-control:first-child, .input-group-addon:first-child {
            border-bottom-right-radius: 0 !important;
            border-top-right-radius: 0 !important;
          }

          .input-group .form-control:last-child, .input-group-addon:last-child {
            border-bottom-left-radius: 0 !important;
            border-top-left-radius: 0 !important;
          }

          .input-group .select-wrapper:first-child .form-control:first-child {
            border-bottom-left-radius: ' . $form_control_radius . 'px !important;
            border-top-left-radius: ' . $form_control_radius . 'px !important;
          }
        ';
      }
      $button_radius = improved_theme_settings_get_setting('button_radius', $system_theme_settings);
      if ($button_radius >= 0) {
        $style_to_add .= '
          :not(.btn-group) > .btn {
            border-radius: ' . $button_radius . 'px !important;
          }

          .btn.btn-floating,
          .btn.btn-icon-toggle {
            border-radius: 50% !important;
          }

        ';
      }
      if (!empty($style_to_add)) {
        $page['#attached']['html_head'][] = [
          [
            '#type' => 'html_tag',
            '#tag' => 'style',
            '#value' => Markup::create($style_to_add),
          ],
          // A key, to make it possible to recognize this HTML element when
          // altering.
          'social_theme_preprocess',
        ];
      }
    }
  }
}

/**
 * Function that returns a valid integer or FALSE.
 */
function improved_theme_settings_get_setting($setting, $themename) {

  // Fetch setting from the theme.
  $themevar = Xss::filter(theme_get_setting($setting, $themename));

  // In this case '' is something different than 0.
  if ($themevar !== trim(intval($themevar))) {
    return FALSE;
  }
  return $themevar;
}