I followed the documentation on drupal.org to add a field to the theme-settings.php but there must be a mistake in code.

I have a filed called advertisement and a theme called tv so I tried to copy and paste the code and altered the names where to alter theme the files I created in particular are as follows:


function tv_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     
  if (isset($form_id)) {

$form['tv_advertisement'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Widget'),
    '#default_value' => theme_get_setting('tv_advertisement'),
    '#description'   => t("Place this text in the widget spot on your     
$tv_advertisement = theme_get_setting('tv_advertisement');

in /themes/tv/config/install/tv.settings.yml I put this:

tv_advertisement: blue bikeshed

My /themes/tv/tv.theme file contains:

 * @file
 * Theme functions

// Include all files from the includes directory.
$includes_path = dirname(__FILE__) . '/includes/*.inc';
foreach (glob($includes_path) as $filename) {
  require_once dirname(__FILE__) . '/includes/' . basename($filename);

 function tv_preprocess_node(&$variables) {
 $variables['tv_advertisement'] =     

The last thing I did was

drush cr 

to clear the cache

When I go to the themes settings page there are only the default fieds displayed but the new one does not show up

If anyone has a clue what I am doing wrong and is able to enlighten me I would be very pleased.

Thank you


1 Answer 1


Try to remove this code:

  if (isset($form_id)) {

The hook interface of hook_form_system_theme_settings_alter is hook_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state).

You can remove the third parameter:

 $form_id = NULL
  • even if i remove this the variable still ge's not rendered in a twig.html file The settings page looks good but if I use {{tv_advertisement}} in a twig file for example there is no rendering of the var
    – Josh
    Apr 9, 2017 at 11:39
  • Did you use it in the template node.html.twig or in another template ? I assume if you put it in the preprocess tv_preprocess_node you only get it in node.html.twig.
    – jBlobsmith
    Apr 9, 2017 at 11:53
  • how to get the variable rendered in other templates? What the name suggestions are or how to determine theme by template names?
    – Josh
    Apr 9, 2017 at 12:21
  • You can set your debug variable to true like specified in the Drupal documentation -> drupal.org/docs/8/theming/twig/debugging-twig-templates and after go to the generated html to see HOOK in comments. Also, you can see theme hook preprocess explanation in the Drupal documentation api.drupal.org/api/drupal/…
    – jBlobsmith
    Apr 9, 2017 at 12:31
  • Shorter is better function tv_preprocess_node(&$variables) for node function tv_preprocess_page(&$variables) for page function tv_preprocess_field(&$variables) for field Never thought that this is that simple! :-)
    – Josh
    Apr 10, 2017 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.