ThemeSettingsForm::buildForm

public ThemeSettingsForm::buildForm(array $form, FormStateInterface $form_state, $theme = '')

Parameters

string $theme: The theme name.

Overrides ConfigFormBase::buildForm

File

core/modules/system/src/Form/ThemeSettingsForm.php, line 101

Class

ThemeSettingsForm
Displays theme configuration for entire site and individual themes.

Namespace

Drupal\system\Form

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
public function buildForm(array $form, FormStateInterface $form_state, $theme = '') {
  $form = parent::buildForm($form, $form_state);
 
  $themes = $this->themeHandler->listInfo();
 
  // Default settings are defined in theme_get_setting() in includes/theme.inc
  if ($theme) {
    if (!$this->themeHandler->hasUi($theme)) {
      throw new NotFoundHttpException();
    }
    $var = 'theme_' . $theme . '_settings';
    $config_key = $theme . '.settings';
    $themes = $this->themeHandler->listInfo();
    $features = $themes[$theme]->info['features'];
  }
  else {
    $var = 'theme_settings';
    $config_key = 'system.theme.global';
  }
  // @todo this is pretty meaningless since we're using theme_get_settings
  //   which means overrides can bleed into active config here. Will be fixed
  $this->editableConfig = [$config_key];
 
  $form['var'] = array(
    '#type' => 'hidden',
    '#value' => $var
  );
  $form['config_key'] = array(
    '#type' => 'hidden',
    '#value' => $config_key
  );
 
  // Toggle settings
  $toggles = array(
    'node_user_picture' => t('User pictures in posts'),
    'comment_user_picture' => t('User pictures in comments'),
    'comment_user_verification' => t('User verification status in comments'),
    'favicon' => t('Shortcut icon'),
  );
 
  // Some features are not always available
  $disabled = array();
  if (!user_picture_enabled()) {
    $disabled['toggle_node_user_picture'] = TRUE;
    $disabled['toggle_comment_user_picture'] = TRUE;
  }
  if (!$this->moduleHandler->moduleExists('comment')) {
    $disabled['toggle_comment_user_picture'] = TRUE;
    $disabled['toggle_comment_user_verification'] = TRUE;
  }
 
  $form['theme_settings'] = array(
    '#type' => 'details',
    '#title' => t('Page element display'),
    '#open' => TRUE,
  );
  foreach ($toggles as $name => $title) {
    if ((!$theme) || in_array($name, $features)) {
      $form['theme_settings']['toggle_' . $name] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => theme_get_setting('features.' . $name, $theme));
      // Disable checkboxes for features not supported in the current configuration.
      if (isset($disabled['toggle_' . $name])) {
        $form['theme_settings']['toggle_' . $name]['#disabled'] = TRUE;
      }
    }
  }
 
  if (!Element::children($form['theme_settings'])) {
    // If there is no element in the theme settings details then do not show
    // it -- but keep it in the form if another module wants to alter.
    $form['theme_settings']['#access'] = FALSE;
  }
 
  // Logo settings, only available when file.module is enabled.
  if ((!$theme || in_array('logo', $features)) && $this->moduleHandler->moduleExists('file')) {
    $form['logo'] = array(
      '#type' => 'details',
      '#title' => t('Logo image'),
      '#open' => TRUE,
    );
    $form['logo']['default_logo'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use the logo supplied by the theme'),
      '#default_value' => theme_get_setting('logo.use_default', $theme),
      '#tree' => FALSE,
    );
    $form['logo']['settings'] = array(
      '#type' => 'container',
      '#states' => array(
        // Hide the logo settings when using the default logo.
        'invisible' => array(
          'input[name="default_logo"]' => array('checked' => TRUE),
        ),
      ),
    );
    $form['logo']['settings']['logo_path'] = array(
      '#type' => 'textfield',
      '#title' => t('Path to custom logo'),
      '#default_value' => theme_get_setting('logo.path', $theme),
    );
    $form['logo']['settings']['logo_upload'] = array(
      '#type' => 'file',
      '#title' => t('Upload logo image'),
      '#maxlength' => 40,
      '#description' => t("If you don't have direct file access to the server, use this field to upload your logo.")
    );
  }
 
  if (((!$theme) || in_array('favicon', $features)) && $this->moduleHandler->moduleExists('file')) {
    $form['favicon'] = array(
      '#type' => 'details',
      '#title' => t('Favicon'),
      '#open' => TRUE,
      '#description' => t("Your shortcut icon, or favicon, is displayed in the address bar and bookmarks of most browsers."),
      '#states' => array(
        // Hide the shortcut icon settings fieldset when shortcut icon display
        // is disabled.
        'invisible' => array(
          'input[name="toggle_favicon"]' => array('checked' => FALSE),
        ),
      ),
    );
    $form['favicon']['default_favicon'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use the favicon supplied by the theme'),
      '#default_value' => theme_get_setting('favicon.use_default', $theme),
    );
    $form['favicon']['settings'] = array(
      '#type' => 'container',
      '#states' => array(
        // Hide the favicon settings when using the default favicon.
        'invisible' => array(
          'input[name="default_favicon"]' => array('checked' => TRUE),
        ),
      ),
    );
    $form['favicon']['settings']['favicon_path'] = array(
      '#type' => 'textfield',
      '#title' => t('Path to custom icon'),
      '#default_value' => theme_get_setting('favicon.path', $theme),
    );
    $form['favicon']['settings']['favicon_upload'] = array(
      '#type' => 'file',
      '#title' => t('Upload favicon image'),
      '#description' => t("If you don't have direct file access to the server, use this field to upload your shortcut icon.")
    );
  }
 
  // Inject human-friendly values and form element descriptions for logo and
  // favicon.
  foreach (array('logo' => 'logo.svg', 'favicon' => 'favicon.ico') as $type => $default) {
    if (isset($form[$type]['settings'][$type . '_path'])) {
      $element = &$form[$type]['settings'][$type . '_path'];
 
      // If path is a public:// URI, display the path relative to the files
      // directory; stream wrappers are not end-user friendly.
      $original_path = $element['#default_value'];
      $friendly_path = NULL;
      if (file_uri_scheme($original_path) == 'public') {
        $friendly_path = file_uri_target($original_path);
        $element['#default_value'] = $friendly_path;
      }
 
      // Prepare local file path for description.
      if ($original_path && isset($friendly_path)) {
        $local_file = strtr($original_path, array('public:/' => PublicStream::basePath()));
      }
      elseif ($theme) {
        $local_file = drupal_get_path('theme', $theme) . '/' . $default;
      }
      else {
        $local_file = \Drupal::theme()->getActiveTheme()->getPath() . '/' . $default;
      }
 
      $element['#description'] = t('Examples: <code>@implicit-public-file</code> (for a file in the public filesystem), <code>@explicit-file</code>, or <code>@local-file</code>.', array(
        '@implicit-public-file' => isset($friendly_path) ? $friendly_path : $default,
        '@explicit-file' => file_uri_scheme($original_path) !== FALSE ? $original_path : 'public://' . $default,
        '@local-file' => $local_file,
      ));
    }
  }
 
  if ($theme) {
    // Call engine-specific settings.
    $function = $themes[$theme]->prefix . '_engine_settings';
    if (function_exists($function)) {
      $form['engine_specific'] = array(
        '#type' => 'details',
        '#title' => t('Theme-engine-specific settings'),
        '#open' => TRUE,
        '#description' => t('These settings only exist for the themes based on the %engine theme engine.', array('%engine' => $themes[$theme]->prefix)),
      );
      $function($form, $form_state);
    }
 
    // Create a list which includes the current theme and all its base themes.
    if (isset($themes[$theme]->base_themes)) {
      $theme_keys = array_keys($themes[$theme]->base_themes);
      $theme_keys[] = $theme;
    }
    else {
      $theme_keys = array($theme);
    }
 
    // Save the name of the current theme (if any), so that we can temporarily
    // override the current theme and allow theme_get_setting() to work
    // without having to pass the theme name to it.
    $default_active_theme = \Drupal::theme()->getActiveTheme();
    $default_theme = $default_active_theme->getName();
    /** @var \Drupal\Core\Theme\ThemeInitialization $theme_initialization */
    $theme_initialization = \Drupal::service('theme.initialization');
    \Drupal::theme()->setActiveTheme($theme_initialization->getActiveThemeByName($theme));
 
    // Process the theme and all its base themes.
    foreach ($theme_keys as $theme) {
      // Include the theme-settings.php file.
      $filename = DRUPAL_ROOT . '/' . $themes[$theme]->getPath() . '/theme-settings.php';
      if (file_exists($filename)) {
        require_once $filename;
      }
 
      // Call theme-specific settings.
      $function = $theme . '_form_system_theme_settings_alter';
      if (function_exists($function)) {
        $function($form, $form_state);
      }
    }
 
    // Restore the original current theme.
    if (isset($default_theme)) {
      \Drupal::theme()->setActiveTheme($default_active_theme);
    }
    else {
      \Drupal::theme()->resetActiveTheme();
    }
  }
 
  return $form;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.