anavdesign
2/12/2017 - 3:20 AM

Drupal Theme Settings

Drupal Theme Settings

# Drupal 8 Custom Theme Settings
https://www.drupal.org/node/2623936
https://www.drupal.org/node/177868
http://tinyurl.com/jxuvvw8

## APPPEARANCE FORM
###################################
- theme-settings.php

  $form['custom'] = array(
    '#type' => 'fieldset',
    '#title' => t('Custom Text'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  $form['custom']['custom_text_display'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display Custom Text'),
    '#default_value' => theme_get_setting('custom_text_display'),
  );

  $form['custom']['custom_text'] = array(
    '#type' => 'textfield',
    '#title' => t('Add custom text'),
    '#default_value' => theme_get_setting('custom_text'),
    '#description' => t('This is a short description.'),
  );

## SET DEFAULT VALUES
###################################
- THEME_NAME/config/install/THEME_NAME.settings.yml

menu_mobile_position: 'menu-mobile-position-right'


## PREPROCESS FUNCTION TO RETRIEVE VALUE
###################################
- THEME_NAME.theme

function andrupal8_preprocess_page(array &$variables) {
  $variables['custom_text'] = theme_get_setting('custom_text');
  $variables['custom_text_display'] = theme_get_setting('custom_text_display');
}

# TWIG TEMPLATE
###################################

{% if custom_text_display %}
    {{ custom_text }}
{% endif %}