system.module 61.8 KB
Newer Older
1 2
<?php

3 4 5 6 7
/**
 * @file
 * Configuration system that lets administrators modify the workings of the site.
 */

8
use Drupal\Core\Cache\Cache;
9
use Drupal\Core\Extension\Extension;
10
use Drupal\Core\Extension\ExtensionDiscovery;
11
use Drupal\Core\Form\FormStateInterface;
12
use Drupal\Core\Routing\RouteMatchInterface;
13
use Drupal\Core\StringTranslation\TranslationWrapper;
14
use Drupal\Core\Language\LanguageInterface;
15
use Drupal\Core\Menu\MenuTreeParameters;
16
use Drupal\block\BlockPluginInterface;
17
use Drupal\Core\Url;
18
use Drupal\user\UserInterface;
19
use Symfony\Component\HttpFoundation\RedirectResponse;
20
use GuzzleHttp\Exception\RequestException;
21

22 23 24
/**
 * New users will be set to the default time zone at registration.
 */
25
const DRUPAL_USER_TIMEZONE_DEFAULT = 0;
26 27 28 29

/**
 * New users will get an empty time zone at registration.
 */
30
const DRUPAL_USER_TIMEZONE_EMPTY = 1;
31 32 33 34

/**
 * New users will select their own timezone at registration.
 */
35
const DRUPAL_USER_TIMEZONE_SELECT = 2;
36

37
/**
38 39
 * Disabled option on forms and settings
 */
40
const DRUPAL_DISABLED = 0;
41 42 43 44

/**
 * Optional option on forms and settings
 */
45
const DRUPAL_OPTIONAL = 1;
46 47 48 49

/**
 * Required option on forms and settings
 */
50
const DRUPAL_REQUIRED = 2;
51

52
/**
53
 * Return only visible regions.
54 55
 *
 * @see system_region_list()
56
 */
57
const REGIONS_VISIBLE = 'visible';
58 59

/**
60
 * Return all regions.
61 62
 *
 * @see system_region_list()
63
 */
64
const REGIONS_ALL = 'all';
65

66 67 68
/**
 * Defines the max length for an email address
 *
69
 * The maximum length of an email address is 254 characters. RFC 3696
70 71 72 73 74 75 76 77
 * specifies a total length of 320 characters, but mentions that
 * addresses longer than 256 characters are not normally useful. Erratum
 * 1690 was then released which corrected this value to 254 characters.
 * @see http://tools.ietf.org/html/rfc3696#section-3
 * @see http://www.rfc-editor.org/errata_search.php?rfc=3696&eid=1690
 */
const EMAIL_MAX_LENGTH = 254;

Dries's avatar
Dries committed
78
/**
79
 * Implements hook_help().
Dries's avatar
Dries committed
80
 */
81
function system_help($route_name, RouteMatchInterface $route_match) {
82 83
  global $base_url;

84 85
  switch ($route_name) {
    case 'help.page.system':
86 87
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
88
      $output .= '<p>' . t('The System module is integral to the site, and provides basic but extensible functionality for use by other modules and themes. Some integral elements of Drupal are contained in and managed by the System module, including caching, enabling and disabling modules and themes, preparing and displaying the administrative page, and configuring fundamental site settings. A number of key system maintenance operations are also part of the System module. For more information, see the online handbook entry for <a href="@system">System module</a>.', array('@system' => 'http://drupal.org/documentation/modules/system')) . '</p>';
89 90 91
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Managing modules') . '</dt>';
92
      $output .= '<dd>' . t('The System module allows users with the appropriate permissions to enable and disable modules on the <a href="@modules">Modules administration page</a>. Drupal comes with a number of core modules, and each module provides a discrete set of features and may be enabled or disabled depending on the needs of the site. Many additional modules contributed by members of the Drupal community are available for download at the <a href="@drupal-modules">Drupal.org module page</a>.', array('@modules' => url('admin/modules'), '@drupal-modules' => 'http://drupal.org/project/modules')) . '</dd>';
93 94 95 96 97
      $output .= '<dt>' . t('Managing themes') . '</dt>';
      $output .= '<dd>' . t('The System module allows users with the appropriate permissions to enable and disable themes on the <a href="@themes">Appearance administration page</a>. Themes determine the design and presentation of your site. Drupal comes packaged with several core themes, and additional contributed themes are available at the <a href="@drupal-themes">Drupal.org theme page</a>.', array('@themes' => url('admin/appearance'), '@drupal-themes' => 'http://drupal.org/project/themes')) . '</dd>';
      $output .= '<dt>' . t('Managing caching') . '</dt>';
      $output .= '<dd>' . t("The System module allows users with the appropriate permissions to manage caching on the <a href='@cache-settings'>Performance settings page</a>. Drupal has a robust caching system that allows the efficient re-use of previously-constructed web pages and web page components. Pages requested by anonymous users are stored in a compressed format; depending on your site configuration and the amount of your web traffic tied to anonymous visitors, the caching system may significantly increase the speed of your site.", array('@cache-settings' => url('admin/config/development/performance'))) . '</dd>';
      $output .= '<dt>' . t('Performing system maintenance') . '</dt>';
98
      $output .= '<dd>' . t('In order for the site and its modules to continue to operate well, a set of routine administrative operations must run on a regular basis. The System module manages this task by making use of a system cron job. You can verify the status of cron tasks by visiting the <a href="@status">Status report page</a>. For more information, see the online handbook entry for <a href="@handbook">configuring cron jobs</a>. You can set up cron job by visiting <a href="@cron">Cron configuration</a> page', array('@status' => url('admin/reports/status'), '@handbook' => 'http://drupal.org/cron', '@cron' => url('admin/config/system/cron'))) . '</dd>';
99
      $output .= '<dt>' . t('Configuring basic site settings') . '</dt>';
100
      $output .= '<dd>' . t('The System module also handles basic configuration options for your site, including <a href="@date-time-settings">Date and time settings</a>, <a href="@file-system">File system settings</a>, <a href="@site-info">Site name and other information</a>, and a <a href="@maintenance-mode">Maintenance mode</a> for taking your site temporarily offline.', array('@date-time-settings' => url('admin/config/regional/date-time'), '@file-system' => url('admin/config/media/file-system'), '@site-info' => url('admin/config/system/site-information'), '@maintenance-mode' => url('admin/config/development/maintenance'))) . '</dd>';
101 102
      $output .= '<dt>' . t('Providing administrative help') . '</dt>';
      $output .= '<dd>' . t('Page-specific administrative help text that is provided by the System module and other modules is displayed in the System Help block. This block can be placed and configured on the <a href="!blocks">Block layout page</a>.', array('!blocks' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('block.admin_display') : '#')) . '</dd>';
103
      $output .= '</dl>';
104
      return $output;
105 106

    case 'system.admin_index':
107
      return '<p>' . t('This page shows you all available administration tasks for each module.') . '</p>';
108 109

    case 'system.themes_page':
110
      $output = '<p>' . t('Set and configure the default theme for your website.  Alternative <a href="@themes">themes</a> are available.', array('@themes' => 'http://drupal.org/project/themes')) . '</p>';
111
      return $output;
112 113

    case 'system.theme_settings_theme':
114
      $theme_list = list_themes();
115
      $theme = $theme_list[$route_match->getParameter('theme')];
116
      return '<p>' . t('These options control the display settings for the %name theme. When your site is displayed using this theme, these settings will be used.', array('%name' => $theme->info['name'])) . '</p>';
117 118

    case 'system.theme_settings':
119
      return '<p>' . t('These options control the default display settings for your entire site, across all themes. Unless they have been overridden by a specific theme, these settings will be used.') . '</p>';
120 121

    case 'system.modules_list':
122
      $output = '<p>' . t('Download additional <a href="@modules">contributed modules</a> to extend Drupal\'s functionality.', array('@modules' => 'http://drupal.org/project/modules')) . '</p>';
123
      if (\Drupal::moduleHandler()->moduleExists('update')) {
124
        if (update_manager_access()) {
125
          $output .= '<p>' . t('Regularly review and install <a href="@updates">available updates</a> to maintain a secure and current site. Always run the <a href="@update-php">update script</a> each time a module is updated.', array('@update-php' => $base_url . '/core/update.php', '@updates' => url('admin/reports/updates'))) . '</p>';
126 127
        }
        else {
128
          $output .= '<p>' . t('Regularly review <a href="@updates">available updates</a> to maintain a secure and current site. Always run the <a href="@update-php">update script</a> each time a module is updated.', array('@update-php' => $base_url . '/core/update.php', '@updates' => url('admin/reports/updates'))) . '</p>';
129 130 131
        }
      }
      else {
132
        $output .= '<p>' . t('Regularly review available updates to maintain a secure and current site. Always run the <a href="@update-php">update script</a> each time a module is updated. Enable the Update Manager module to update and install modules and themes.', array('@update-php' => $base_url . '/core/update.php')) . '</p>';
133
      }
134
      return $output;
135 136

    case 'system.modules_uninstall':
137
      return '<p>' . t('The uninstall process removes all data related to a module.') . '</p>';
138

139
    case 'entity.block.edit_form':
140
      if (($block = $route_match->getParameter('block')) && $block->get('plugin') == 'system_powered_by_block') {
141
        return '<p>' . t('The <em>Powered by Drupal</em> block is an optional link to the home page of the Drupal project. While there is absolutely no requirement that sites feature this link, it may be used to show support for Drupal.') . '</p>';
142
      }
143
      break;
144 145

    case 'block.admin_add':
146
      if ($route_match->getParameter('plugin_id') == 'system_powered_by_block') {
147 148 149 150 151
        return '<p>' . t('The <em>Powered by Drupal</em> block is an optional link to the home page of the Drupal project. While there is absolutely no requirement that sites feature this link, it may be used to show support for Drupal.') . '</p>';
      }
      break;

    case 'system.site_maintenance_mode':
152
      if (\Drupal::currentUser()->id() == 1) {
153
        return '<p>' . t('Use maintenance mode when making major updates, particularly if the updates could disrupt visitors or the update process. Examples include upgrading, importing or exporting content, modifying a theme, modifying content types, and making backups.') . '</p>';
154
      }
155
      break;
156 157

    case 'system.status':
158
      return '<p>' . t("Here you can find a short overview of your site's parameters as well as any problems detected with your installation. It may be useful to copy and paste this information into support requests filed on drupal.org's support forums and project issue queues. Before filing a support request, ensure that your web server meets the <a href=\"@system-requirements\">system requirements.</a>", array('@system-requirements' => 'http://drupal.org/requirements')) . '</p>';
159
  }
Dries's avatar
Dries committed
160 161
}

162
/**
163
 * Implements hook_theme().
164
 */
165
function system_theme() {
166
  return array_merge(drupal_common_theme(), array(
167 168 169 170 171 172 173 174
    // Normally theme suggestion templates are only picked up when they are in
    // themes. We explicitly define the block__system_branding_block theme
    // suggestion here so that the template in core/modules/system/templates
    // is picked up.
    'block__system_branding_block' => array(
      'base hook' => 'block',
      'template' => 'block--system-branding-block',
    ),
175
    'system_themes_page' => array(
176
      'variables' => array(
177 178
        'theme_groups' => array(),
        'theme_group_titles' => array(),
179
      ),
180
      'file' => 'system.admin.inc',
181
      'template' => 'system-themes-page',
182
    ),
183
    'system_config_form' => array(
184
      'render element' => 'form',
185
      'template' => 'system-config-form',
186 187
    ),
    'confirm_form' => array(
188
      'render element' => 'form',
189
      'template' => 'confirm-form',
190
    ),
191
    'system_modules_details' => array(
192
      'render element' => 'form',
193
      'file' => 'system.admin.inc',
194 195
    ),
    'system_modules_uninstall' => array(
196
      'render element' => 'form',
197
      'file' => 'system.admin.inc',
198 199
    ),
    'status_report' => array(
200
      'variables' => array('requirements' => NULL),
201
      'file' => 'system.admin.inc',
202
      'template' => 'status-report',
203 204
    ),
    'admin_page' => array(
205
      'variables' => array('blocks' => NULL),
206
      'file' => 'system.admin.inc',
207
      'template' => 'admin-page',
208 209
    ),
    'admin_block' => array(
210
      'variables' => array('block' => NULL),
211
      'file' => 'system.admin.inc',
212
      'template' => 'admin-block',
213 214
    ),
    'admin_block_content' => array(
215
      'variables' => array('content' => NULL),
216
      'file' => 'system.admin.inc',
217
      'template' => 'admin-block-content',
218
    ),
219
    'system_admin_index' => array(
220
      'variables' => array('menu_items' => NULL),
221
      'file' => 'system.admin.inc',
222
      'template' => 'system-admin-index',
223
    ),
224 225
    'system_compact_link' => array(
      'variables' => array(),
226
    ),
227 228
  ));
}
229

Dries's avatar
Dries committed
230
/**
231
 * Implements hook_permission().
Dries's avatar
Dries committed
232
 */
233
function system_permission() {
234
  return array(
235 236 237
    'administer modules' => array(
      'title' => t('Administer modules'),
    ),
238 239
    'administer site configuration' => array(
      'title' => t('Administer site configuration'),
240
      'restrict access' => TRUE,
241
    ),
242 243 244
    'administer themes' => array(
      'title' => t('Administer themes'),
    ),
245
    'administer software updates' => array(
246 247
      'title' => t('Administer software updates'),
      'restrict access' => TRUE,
248
    ),
249
    'access administration pages' => array(
250
      'title' => t('Use the administration pages and help'),
251
    ),
252
    'access site in maintenance mode' => array(
253
      'title' => t('Use the site in maintenance mode'),
254
    ),
255 256
    'view the administration theme' => array(
      'title' => t('View the administration theme'),
257
      'description' => t('This is only used when the site is configured to use a separate administration theme on the <a href="@appearance-url">Appearance</a> page.', array('@appearance-url' => url('admin/appearance'))),
258
    ),
259
    'access site reports' => array(
260
      'title' => t('View site reports'),
261
      'restrict access' => TRUE,
262
    ),
263
  );
Dries's avatar
Dries committed
264 265
}

266
/**
267
 * Implements hook_hook_info().
268 269 270 271 272
 */
function system_hook_info() {
  $hooks['token_info'] = array(
    'group' => 'tokens',
  );
273 274 275
  $hooks['token_info_alter'] = array(
    'group' => 'tokens',
  );
276 277 278
  $hooks['tokens'] = array(
    'group' => 'tokens',
  );
279 280 281 282
  $hooks['tokens_alter'] = array(
    'group' => 'tokens',
  );

283 284 285
  return $hooks;
}

286
/**
287
 * Implements hook_element_info().
288
 */
289 290
function system_element_info() {
  // Top level elements.
291 292 293 294 295 296 297 298
  $types['html'] = array(
    '#theme' => 'html',
    '#pre_render' => array('drupal_pre_render_html'),
    // HTML5 Shiv
    '#attached' => array(
      'library' => array('core/html5shiv'),
    ),
  );
299
  $types['form'] = array(
300 301
    '#method' => 'post',
    '#action' => request_uri(),
302
    '#theme_wrappers' => array('form'),
303
  );
304
  $types['page'] = array(
305
    '#show_messages' => TRUE,
306
    '#pre_render' => array('drupal_pre_render_page'),
307
    '#theme' => 'page',
308
    '#title' => '',
309
  );
310 311 312 313 314
  $types['inline_template'] = array(
    '#pre_render' => array('drupal_render_inline_template'),
    '#template' => '',
    '#context' => array(),
  );
315
  // By default, we don't want Ajax commands being rendered in the context of an
316 317
  // HTML page, so we don't provide defaults for #theme or #theme_wrappers.
  // However, modules can set these properties (for example, to provide an HTML
318
  // debugging page that displays rather than executes Ajax commands).
319 320 321 322
  $types['ajax'] = array(
    '#header' => TRUE,
    '#commands' => array(),
    '#error' => NULL,
323
  );
324
  $types['html_tag'] = array(
325
    '#pre_render' => array('drupal_pre_render_conditional_comments', 'drupal_pre_render_html_tag'),
326 327 328
    '#attributes' => array(),
    '#value' => NULL,
  );
329 330 331 332
  $types['styles'] = array(
    '#items' => array(),
    '#pre_render' => array('drupal_pre_render_styles'),
  );
333 334 335 336
  $types['scripts'] = array(
    '#items' => array(),
    '#pre_render' => array('drupal_pre_render_scripts'),
  );
337

338 339
  // Input elements.
  $types['submit'] = array(
340 341
    '#input' => TRUE,
    '#name' => 'op',
342
    '#is_button' => TRUE,
343
    '#executes_submit_callback' => TRUE,
344
    '#limit_validation_errors' => FALSE,
345
    '#process' => array('form_process_button', 'ajax_process_form'),
346 347
    '#pre_render' => array('form_pre_render_button'),
    '#theme_wrappers' => array('input__submit'),
348
  );
349
  $types['button'] = array(
350 351
    '#input' => TRUE,
    '#name' => 'op',
352
    '#is_button' => TRUE,
353
    '#executes_submit_callback' => FALSE,
354
    '#limit_validation_errors' => FALSE,
355
    '#process' => array('form_process_button', 'ajax_process_form'),
356 357
    '#pre_render' => array('form_pre_render_button'),
    '#theme_wrappers' => array('input__button'),
358
  );
359
  $types['image_button'] = array(
360
    '#input' => TRUE,
361
    '#is_button' => TRUE,
362
    '#executes_submit_callback' => TRUE,
363
    '#limit_validation_errors' => FALSE,
364
    '#process' => array('form_process_button', 'ajax_process_form'),
365 366 367
    '#return_value' => TRUE,
    '#has_garbage_value' => TRUE,
    '#src' => NULL,
368 369
    '#pre_render' => array('form_pre_render_image_button'),
    '#theme_wrappers' => array('input__image_button'),
370
  );
371 372 373 374
  $types['tel'] = array(
    '#input' => TRUE,
    '#size' => 30,
    '#maxlength' => 128,
375
    '#autocomplete_route_name' => FALSE,
376
    '#process' => array('form_process_autocomplete', 'ajax_process_form', 'form_process_pattern'),
377 378
    '#pre_render' => array('form_pre_render_tel'),
    '#theme' => 'input__tel',
379 380
    '#theme_wrappers' => array('form_element'),
  );
381 382 383
  $types['email'] = array(
    '#input' => TRUE,
    '#size' => 60,
384
    '#maxlength' => EMAIL_MAX_LENGTH,
385
    '#autocomplete_route_name' => FALSE,
386
    '#process' => array('form_process_autocomplete', 'ajax_process_form', 'form_process_pattern'),
387
    '#element_validate' => array('form_validate_email'),
388 389
    '#pre_render' => array('form_pre_render_email'),
    '#theme' => 'input__email',
390 391
    '#theme_wrappers' => array('form_element'),
  );
392 393 394 395
  $types['url'] = array(
    '#input' => TRUE,
    '#size' => 60,
    '#maxlength' => 255,
396
    '#autocomplete_route_name' => FALSE,
397
    '#process' => array('form_process_autocomplete', 'ajax_process_form', 'form_process_pattern'),
398
    '#element_validate' => array('form_validate_url'),
399 400
    '#pre_render' => array('form_pre_render_url'),
    '#theme' => 'input__url',
401 402
    '#theme_wrappers' => array('form_element'),
  );
403 404 405 406
  $types['search'] = array(
    '#input' => TRUE,
    '#size' => 60,
    '#maxlength' => 128,
407
    '#autocomplete_route_name' => FALSE,
408
    '#process' => array('form_process_autocomplete', 'ajax_process_form'),
409 410
    '#pre_render' => array('form_pre_render_search'),
    '#theme' => 'input__search',
411 412
    '#theme_wrappers' => array('form_element'),
  );
413 414 415 416 417
  $types['number'] = array(
    '#input' => TRUE,
    '#step' => 1,
    '#process' => array('ajax_process_form'),
    '#element_validate' => array('form_validate_number'),
418 419
    '#pre_render' => array('form_pre_render_number'),
    '#theme' => 'input__number',
420 421
    '#theme_wrappers' => array('form_element'),
  );
422 423 424 425 426 427 428
  $types['range'] = array(
    '#input' => TRUE,
    '#step' => 1,
    '#min' => 0,
    '#max' => 100,
    '#process' => array('ajax_process_form'),
    '#element_validate' => array('form_validate_number'),
429 430
    '#pre_render' => array('form_pre_render_range'),
    '#theme' => 'input__range',
431 432
    '#theme_wrappers' => array('form_element'),
  );
433 434 435 436
  $types['color'] = array(
    '#input' => TRUE,
    '#process' => array('ajax_process_form'),
    '#element_validate' => array('form_validate_color'),
437 438
    '#pre_render' => array('form_pre_render_color'),
    '#theme' => 'input__color',
439 440
    '#theme_wrappers' => array('form_element'),
  );
441
  $types['password'] = array(
442 443 444
    '#input' => TRUE,
    '#size' => 60,
    '#maxlength' => 128,
445
    '#process' => array('ajax_process_form', 'form_process_pattern'),
446 447
    '#pre_render' => array('form_pre_render_password'),
    '#theme' => 'input__password',
448
    '#theme_wrappers' => array('form_element'),
449
  );
450
  $types['password_confirm'] = array(
451
    '#input' => TRUE,
452
    '#process' => array('form_process_password_confirm', 'user_form_process_password_confirm'),
453
    '#theme_wrappers' => array('form_element'),
454
  );
455
  $types['textarea'] = array(
456 457 458
    '#input' => TRUE,
    '#cols' => 60,
    '#rows' => 5,
459
    '#resizable' => 'vertical',
460 461
    '#process' => array('ajax_process_form', 'form_process_group'),
    '#pre_render' => array('form_pre_render_group'),
462
    '#theme' => 'textarea',
463
    '#theme_wrappers' => array('form_element'),
464
  );
465
  $types['radios'] = array(
466
    '#input' => TRUE,
467
    '#process' => array('form_process_radios'),
468
    '#theme_wrappers' => array('radios'),
469
    '#pre_render' => array('form_pre_render_conditional_form_element'),
470
  );
471
  $types['radio'] = array(
472 473
    '#input' => TRUE,
    '#default_value' => NULL,
474
    '#process' => array('ajax_process_form'),
475 476
    '#pre_render' => array('form_pre_render_radio'),
    '#theme' => 'input__radio',
477
    '#theme_wrappers' => array('form_element'),
478
    '#title_display' => 'after',
479
  );
480
  $types['checkboxes'] = array(
481
    '#input' => TRUE,
482
    '#process' => array('form_process_checkboxes'),
483
    '#pre_render' => array('form_pre_render_conditional_form_element'),
484
    '#theme_wrappers' => array('checkboxes'),
485
  );
486
  $types['checkbox'] = array(
487 488
    '#input' => TRUE,
    '#return_value' => 1,
489 490
    '#process' => array('form_process_checkbox', 'ajax_process_form', 'form_process_group'),
    '#pre_render' => array('form_pre_render_checkbox', 'form_pre_render_group'),
491
    '#theme' => 'input__checkbox',
492
    '#theme_wrappers' => array('form_element'),
493
    '#title_display' => 'after',
494
  );
495
  $types['select'] = array(
496 497
    '#input' => TRUE,
    '#multiple' => FALSE,
498
    '#process' => array('form_process_select', 'ajax_process_form'),
499
    '#theme' => 'select',
500
    '#theme_wrappers' => array('form_element'),
501
    '#options' => array(),
502
  );
503 504
  $types['language_select'] = array(
    '#input' => TRUE,
505
    '#default_value' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
506
  );
507
  $types['weight'] = array(
508 509 510
    '#input' => TRUE,
    '#delta' => 10,
    '#default_value' => 0,
511
    '#process' => array('form_process_weight', 'ajax_process_form'),
512
  );
513
  $types['date'] = array(
514
    '#input' => TRUE,
515 516
    '#theme' => 'input__date',
    '#pre_render' => array('form_pre_render_date'),
517
    '#theme_wrappers' => array('form_element'),
518
  );
519
  $types['file'] = array(
520
    '#input' => TRUE,
521 522
    '#multiple' => FALSE,
    '#process' => array('form_process_file'),
523
    '#size' => 60,
524 525
    '#pre_render' => array('form_pre_render_file'),
    '#theme' => 'input__file',
526
    '#theme_wrappers' => array('form_element'),
527
  );
528
  $types['tableselect'] = array(
529 530 531 532 533 534
    '#input' => TRUE,
    '#js_select' => TRUE,
    '#multiple' => TRUE,
    '#process' => array('form_process_tableselect'),
    '#options' => array(),
    '#empty' => '',
535
    '#theme' => 'tableselect',
536
  );
537

538
  // Form structure.
539 540 541
  $types['label'] = array(
    '#theme' => 'form_element_label',
  );
542
  $types['item'] = array(
543 544 545 546 547 548
    // Forms that show author fields to both anonymous and authenticated users
    // need to dynamically switch between #type 'textfield' and #type 'item' to
    // automatically take over the authenticated user's information. Therefore,
    // we allow #type 'item' to receive input, which is internally assigned by
    // Form API based on the #default_value or #value properties.
    '#input' => TRUE,
549
    '#markup' => '',
550
    '#theme_wrappers' => array('form_element'),
551
  );
552
  $types['hidden'] = array(
553
    '#input' => TRUE,
554
    '#process' => array('ajax_process_form'),
555 556 557 558 559 560 561
    '#pre_render' => array('form_pre_render_hidden'),
    '#theme' => 'input__hidden',
  );
  $types['token'] = array(
    '#input' => TRUE,
    '#pre_render' => array('form_pre_render_hidden'),
    '#theme' => 'input__hidden',
562
  );
563
  $types['value'] = array(
564 565
    '#input' => TRUE,
  );
566
  $types['fieldset'] = array(
567
    '#value' => NULL,
568 569
    '#process' => array('form_process_group', 'ajax_process_form'),
    '#pre_render' => array('form_pre_render_group'),
570 571
    '#theme_wrappers' => array('fieldset'),
  );
572 573 574
  $types['fieldgroup'] = $types['fieldset'] + array(
    '#attributes' => array('class' => array('fieldgroup')),
  );
575
  $types['details'] = array(
576
    '#open' => FALSE,
577
    '#value' => NULL,
578 579
    '#process' => array('form_process_group', 'ajax_process_form'),
    '#pre_render' => array('form_pre_render_details', 'form_pre_render_group'),
580
    '#theme_wrappers' => array('details'),
581
  );
582
  $types['vertical_tabs'] = array(
583 584
    '#default_tab' => '',
    '#process' => array('form_process_vertical_tabs'),
585 586
    '#pre_render' => array('form_pre_render_vertical_tabs'),
    '#theme_wrappers' => array('vertical_tabs', 'form_element'),
587
  );
588 589
  $types['dropbutton'] = array(
    '#pre_render' => array('drupal_pre_render_dropbutton'),
590
    '#theme' => 'links__dropbutton',
591 592 593
  );
  $types['operations'] = array(
    '#pre_render' => array('drupal_pre_render_dropbutton'),
594
    '#theme' => 'links__dropbutton__operations',
595
  );
596 597

  $types['container'] = array(
598 599
    '#process' => array('form_process_group', 'form_process_container'),
    '#pre_render' => array('form_pre_render_group'),
600
    '#theme_wrappers' => array('container'),
601
  );
602
  $types['actions'] = array(
603
    '#process' => array('form_pre_render_actions_dropbutton', 'form_process_actions', 'form_process_container'),
604
    '#weight' => 100,
605
    '#theme_wrappers' => array('container'),
606 607
  );

608 609 610 611 612 613 614 615
  $types['table'] = array(
    '#header' => array(),
    '#rows' => array(),
    '#empty' => '',
    // Properties for tableselect support.
    '#input' => TRUE,
    '#tree' => TRUE,
    '#tableselect' => FALSE,
616 617
    '#sticky' => FALSE,
    '#responsive' => TRUE,
618 619 620 621 622 623
    '#multiple' => TRUE,
    '#js_select' => TRUE,
    '#value_callback' => 'form_type_table_value',
    '#process' => array('form_process_table'),
    '#element_validate' => array('form_validate_table'),
    // Properties for tabledrag support.
624 625 626 627
    // The value is a list of arrays that are passed to
    // drupal_attach_tabledrag(). drupal_pre_render_table() prepends the HTML ID
    // of the table to each set of options.
    // @see drupal_attach_tabledrag()
628 629 630 631 632 633
    '#tabledrag' => array(),
    // Render properties.
    '#pre_render' => array('drupal_pre_render_table'),
    '#theme' => 'table',
  );

634
  return $types;
635 636
}

637 638 639 640
/**
 * Implements hook_theme_suggestions_HOOK().
 */
function system_theme_suggestions_html(array $variables) {
641 642
  $path_args = explode('/', current_path());
  return theme_get_suggestions($path_args, 'html');
643 644 645 646 647 648
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function system_theme_suggestions_page(array $variables) {
649 650
  $path_args = explode('/', current_path());
  return theme_get_suggestions($path_args, 'page');
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function system_theme_suggestions_maintenance_page(array $variables) {
  $suggestions = array();

  // Dead databases will show error messages so supplying this template will
  // allow themers to override the page and the content completely.
  $offline = defined('MAINTENANCE_MODE');
  try {
    drupal_is_front_page();
  }
  catch (Exception $e) {
    // The database is not yet available.
    $offline = TRUE;
  }
  if ($offline) {
    $suggestions[] = 'maintenance_page__offline';
  }

  return $suggestions;
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function system_theme_suggestions_region(array $variables) {
  $suggestions = array();
  if (!empty($variables['elements']['#region'])) {
    $suggestions[] = 'region__' . $variables['elements']['#region'];
  }
  return $suggestions;
}

687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
/**
 * Implements hook_theme_suggestions_HOOK().
 */
function system_theme_suggestions_field(array $variables) {
  $suggestions = array();
  $element = $variables['element'];

  $suggestions[] = 'field__' . $element['#field_type'];
  $suggestions[] = 'field__' . $element['#field_name'];
  $suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#bundle'];
  $suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#field_name'];
  $suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#field_name'] . '__' . $element['#bundle'];

  return $suggestions;
}

703
/**
704
 * Implements hook_stream_wrappers().
705 706
 */
function system_stream_wrappers() {
707
  $wrappers = array(
708
    'public' => array(
709
      'name' => new TranslationWrapper('Public files'),
webchick's avatar
webchick committed
710
      'class' => 'Drupal\Core\StreamWrapper\PublicStream',
711
      'description' => new TranslationWrapper('Public local files served by the webserver.'),
712
      'type' => STREAM_WRAPPERS_LOCAL_NORMAL,
713 714
    ),
    'temporary' => array(
715
      'name' => new TranslationWrapper('Temporary files'),
webchick's avatar
webchick committed
716
      'class' => 'Drupal\Core\StreamWrapper\TemporaryStream',
717
      'description' => new TranslationWrapper('Temporary local files for upload and previews.'),
718
      'type' => STREAM_WRAPPERS_LOCAL_HIDDEN,
719
    ),
720
  );
721 722

  // Only register the private file stream wrapper if a file path has been set.
723
  if (\Drupal::config('system.file')->get('path.private')) {
724
    $wrappers['private'] = array(
725
      'name' => new TranslationWrapper('Private files'),
webchick's avatar
webchick committed
726
      'class' => 'Drupal\Core\StreamWrapper\PrivateStream',
727
      'description' => new TranslationWrapper('Private local files served by Drupal.'),
728
      'type' => STREAM_WRAPPERS_LOCAL_NORMAL,
729 730 731 732
    );
  }

  return $wrappers;
733 734
}

735
/**
736
 * Menu item access callback - only enabled themes can be accessed.
737 738
 */
function _system_themes_access($theme) {
739
  return \Drupal::currentUser()->hasPermission('administer themes') && drupal_theme_access($theme);
740 741
}

742
/**
743 744 745
 * @defgroup authorize Authorized operations
 * @{
 * Functions to run operations with elevated privileges via authorize.php.
746
 *
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
 * Because of the Update manager functionality included in Drupal core, there
 * is a mechanism for running operations with elevated file system privileges,
 * the top-level authorize.php script. This script runs at a reduced Drupal
 * bootstrap level so that it is not reliant on the entire site being
 * functional. The operations use a FileTransfer class to manipulate code
 * installed on the system as the user that owns the files, not the user that
 * the httpd is running as.
 *
 * The first setup is to define a callback function that should be authorized
 * to run with the elevated privileges. This callback should take a
 * FileTransfer as its first argument, although you can define an array of
 * other arguments it should be invoked with. The callback should be placed in
 * a separate .inc file that will be included by authorize.php.
 *
 * To run the operation, certain data must be saved into the SESSION, and then
 * the flow of control should be redirected to the authorize.php script. There
 * are two ways to do this, either to call system_authorized_run() directly,
 * or to call system_authorized_init() and then redirect to authorize.php,
 * using the URL from system_authorized_get_url(). Redirecting yourself is
 * necessary when your authorized operation is being triggered by a form
767
 * submit handler, since calling redirecting in a submit handler is a bad
768
 * idea, and you should instead use $form_state->setRedirect().
769 770 771 772 773 774 775 776 777 778
 *
 * Once the SESSION is setup for the operation and the user is redirected to
 * authorize.php, they will be prompted for their connection credentials (core
 * provides FTP and SSH by default, although other connection classes can be
 * added via contributed modules). With valid credentials, authorize.php will
 * instantiate the appropriate FileTransfer object, and then invoke the
 * desired operation passing in that object. The authorize.php script can act
 * as a Batch API processing page, if the operation requires a batch.
 *
 * @see authorize.php
779
 * @see \Drupal\Core\FileTransfer\FileTransfer
780
 * @see hook_filetransfer_info()
781 782 783 784 785
 */

/**
 * Setup a given callback to run via authorize.php with elevated privileges.
 *
786 787 788 789 790 791 792 793 794
 * To use authorize.php, certain variables must be stashed into $_SESSION. This
 * function sets up all the necessary $_SESSION variables. The calling function
 * should then redirect to authorize.php, using the full path returned by
 * system_authorized_get_url(). That initiates the workflow that will eventually
 * lead to the callback being invoked. The callback will be invoked at a low
 * bootstrap level, without all modules being invoked, so it needs to be careful
 * not to assume any code exists. Example (system_authorized_run()):
 * @code
 *   system_authorized_init($callback, $file, $arguments, $page_title);
795
 *   return new RedirectResponse(system_authorized_get_url()->toString());
796 797 798 799 800 801
 * @endcode
 * Example (update_manager_install_form_submit()):
 * @code
 *  system_authorized_init('update_authorize_run_install',
 *    drupal_get_path('module', 'update') . '/update.authorize.inc',
 *    $arguments, t('Update manager'));
802
 *  $form_state->setRedirectUrl(system_authorized_get_url());
803
 * @endcode
804 805
 *
 * @param $callback
806
 *   The name of the function to invoke once the user authorizes the operation.
807 808 809 810 811 812 813 814 815
 * @param $file
 *   The full path to the file where the callback function is implemented.
 * @param $arguments
 *   Optional array of arguments to pass into the callback when it is invoked.
 *   Note that the first argument to the callback is always the FileTransfer
 *   object created by authorize.php when the user authorizes the operation.
 * @param $page_title
 *   Optional string to use as the page title once redirected to authorize.php.
 * @return
816
 *   Nothing, this function just initializes variables in the user's session.
817
 */
818
function system_authorized_init($callback, $file, $arguments = array(), $page_title = NULL) {
819 820 821
  // First, figure out what file transfer backends the site supports, and put
  // all of those in the SESSION so that authorize.php has access to all of
  // them via the class autoloader, even without a full bootstrap.
822
  $_SESSION['authorize_filetransfer_info'] = drupal_get_filetransfer_info();
823 824 825 826 827 828 829 830 831

  // Now, define the callback to invoke.
  $_SESSION['authorize_operation'] = array(
    'callback' => $callback,
    'file' => $file,
    'arguments' => $arguments,
  );

  if (isset($page_title)) {
832
    $_SESSION['authorize_page_title'] = $page_title;
833
  }
834
}
835

836 837
/**
 * Return the URL for the authorize.php script.
838 839 840
 *
 * @param array $options
 *   Optional array of options to pass to url().
841
 * @return \Drupal\Core\Url
842
 *   The full URL to authorize.php, using HTTPS if available.
843 844
 *
 * @see system_authorized_init()
845
 */
846
function system_authorized_get_url(array $options = array()) {
847
  global $base_url;
848
  // Force HTTPS if available, regardless of what the caller specifies.
849
  $options['https'] = TRUE;
850
  // Prefix with $base_url so url() treats it as an external link.
851 852 853 854
  $url = Url::createFromPath($base_url . '/core/authorize.php');
  $url_options = $url->getOptions();
  $url->setOptions($options + $url_options);
  return $url;
855 856
}

857 858
/**
 * Returns the URL for the authorize.php script when it is processing a batch.
859 860 861
 *
 * @param array $options
 *   Optional array of options to pass to url().
862 863
 *
 * @return \Drupal\Core\Url
864
 */
865 866 867
function system_authorized_batch_processing_url(array $options = array()) {
  $options['query'] = array('batch' => '1');
  return system_authorized_get_url($options);
868 869
}

870 871 872
/**
 * Setup and invoke an operation using authorize.php.
 *
873
 * @see system_authorized_init()
874 875 876
 */
function system_authorized_run($callback, $file, $arguments = array(), $page_title = NULL) {
  system_authorized_init($callback, $file, $arguments, $page_title);
877
  return new RedirectResponse(system_authorized_get_url()->toString());
878 879
}

880 881 882 883 884 885 886
/**
 * Use authorize.php to run batch_process().
 *
 * @see batch_process()
 */
function system_authorized_batch_process() {
  $finish_url = system_authorized_get_url();
887
  $process_url = system_authorized_batch_processing_url();