Skip to content

Latest commit

 

History

History
72 lines (53 loc) · 2.24 KB

exercise_18-theme-settings1.md

File metadata and controls

72 lines (53 loc) · 2.24 KB

Exercise 18:

Creating a theme settings file

Often we need to create small, simple little settings for our theme that can make it more reusable and customizable across sites. We can create new theme settings on our theme's settings page that we can use to help other users configure and customize our theme. For this, we will utilize two additional files. acme.settings.yml and theme-settings.php file.

  1. Go to MYDRUPAL.LOCAL/admin/appearance/settings/acme in your browser to see what settings are available by default.

  2. Create a new file called acme.settings.yml in your theme root and open in your preferred code editor.

    $ cd MYDRUPAL
    $ touch themes/custom/acme/acme.settings.yml
  3. Add the following code:

    features:
      copyright_holder: ''
      search_placeholder: 'Search site'
    
  4. Create a new file in your theme directory called theme-settings.php

    $ cd MYDRUPAL
    $ touch themes/custom/acme/theme-settings.php
  5. Add the following code to that file.

<?php

function acme_form_system_theme_settings_alter(&$form, $form_state, $form_id = NULL) {

  $form['copyright_holder'] = [
    '#type' => 'textfield',
    '#title' => t('Copyright Holder'),
    '#default_value' => theme_get_setting('copyright_holder'),
    '#description' => t('This appears in the footer.'),
    '#weight' => -10,
  ];
  $form['color'] = [
    '#type' => 'select',
    '#title' => t('Color'),
    '#options' => [
      'blue' => t('Blue'),
      'green' => t('Green'),
      'yellow' => t('Yellow')
    ],
    '#default_value' => theme_get_setting('color'),
    '#description' => t('Choose a color'),
    '#weight' => -10,
  ];
}
  1. Clear cache, then go to our Appearance page. Click on Settings for your custom theme. You should see two new options for the settings we added.

  2. Feel free to customize those values. We will use them in the next exercises.

Questions you may have...

  • What is features in the *.settings.yml?
  • Where do I find documentation for creating forms?

Done ☺

One more to go! Exercise 19 - Custom Theme Settings 2