system.module
Same filename and directory in other branches
File
-
core/
modules/ system/ system.module
View source
<?php
/**
* @file
*/
use Drupal\Component\FileSecurity\FileSecurity;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Form\FormStateInterface;
use Drupal\system\Theme\SystemAdminThemePreprocess;
use Drupal\system\Hook\SystemThemeHooks;
/**
* Disabled option on forms and settings.
*
* @deprecated in drupal:11.3.0 and is removed from drupal:13.0.0.
* Instead, use the enums provided by the relevant modules.
*
* @see https://www.drupal.org/node/3448089
*/
const DRUPAL_DISABLED = 0;
/**
* Optional option on forms and settings.
*
* @deprecated in drupal:11.3.0 and is removed from drupal:13.0.0.
* Instead, use the enums provided by the relevant modules.
*
* @see https://www.drupal.org/node/3448089
*/
const DRUPAL_OPTIONAL = 1;
/**
* Required option on forms and settings.
*
* @deprecated in drupal:11.3.0 and is removed from drupal:13.0.0.
* Instead, use the enums provided by the relevant modules.
*
* @see https://www.drupal.org/node/3448089
*/
const DRUPAL_REQUIRED = 2;
/**
* Return only visible regions.
*
* @see system_region_list()
*/
const REGIONS_VISIBLE = 'visible';
/**
* Return all regions.
*
* @see system_region_list()
*/
const REGIONS_ALL = 'all';
/**
* Prepares variables for the list of available bundles.
*
* Default template: entity-add-list.html.twig.
*
* @param array $variables
* An associative array containing:
* - bundles: An array of bundles with the label, description, add_link keys.
* - add_bundle_message: The message shown when there are no bundles. Only
* available if the entity type uses bundle entities.
*
* @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial
* template_preprocess functions are registered directly in hook_theme().
*
* @see https://www.drupal.org/node/3504125
*/
function template_preprocess_entity_add_list(&$variables) : void {
@trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
\Drupal::service(SystemThemeHooks::class)->preprocessEntityAddList($variables);
}
/**
* Checks the existence of the directory specified in $form_element.
*
* This function is called from the system_settings form to check all core
* file directories (file_public_path, file_private_path, file_temporary_path).
*
* @param array $form_element
* The form element containing the name of the directory to check.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
function system_check_directory($form_element, FormStateInterface $form_state) {
$directory = $form_element['#value'];
if (strlen($directory) == 0) {
return $form_element;
}
$logger = \Drupal::logger('file system');
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
if (!is_dir($directory) && !$file_system->mkdir($directory, NULL, TRUE)) {
// If the directory does not exist and cannot be created.
$form_state->setErrorByName($form_element['#parents'][0], t('The directory %directory does not exist and could not be created.', [
'%directory' => $directory,
]));
$logger->error('The directory %directory does not exist and could not be created.', [
'%directory' => $directory,
]);
}
if (is_dir($directory) && !is_writable($directory) && !$file_system->chmod($directory)) {
// If the directory is not writable and cannot be made so.
$form_state->setErrorByName($form_element['#parents'][0], t('The directory %directory exists but is not writable and could not be made writable.', [
'%directory' => $directory,
]));
$logger->error('The directory %directory exists but is not writable and could not be made writable.', [
'%directory' => $directory,
]);
}
elseif (is_dir($directory)) {
if ($form_element['#name'] == 'file_public_path') {
// Create public .htaccess file.
FileSecurity::writeHtaccess($directory, FALSE);
}
else {
// Create private .htaccess file.
FileSecurity::writeHtaccess($directory);
}
}
return $form_element;
}
/**
* Get a list of available regions from a specified theme.
*
* @param \Drupal\Core\Extension\Extension|string $theme
* A theme extension object, or the name of a theme.
* @param string $show
* Possible values: REGIONS_ALL or REGIONS_VISIBLE. Visible excludes hidden
* regions.
*
* @return string[]
* An array of regions in the form $region['name'] = 'description'.
*/
function system_region_list($theme, $show = REGIONS_ALL) : array {
if (!$theme instanceof Extension) {
$themes = \Drupal::service('theme_handler')->listInfo();
if (!isset($themes[$theme])) {
return [];
}
$theme = $themes[$theme];
}
$list = [];
$info = $theme->info;
// If requested, suppress hidden regions. See block_admin_display_form().
foreach ($info['regions'] as $name => $label) {
if ($show == REGIONS_ALL || !isset($info['regions_hidden']) || !in_array($name, $info['regions_hidden'])) {
// phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
$list[$name] = t($label);
}
}
return $list;
}
/**
* Sorts themes by their names, with the default theme listed first.
*
* Callback for uasort() within
* \Drupal\system\Controller\SystemController::themesPage().
*
* @see \Drupal\Core\Extension\Extension::sortByName()
*/
function system_sort_themes($a, $b) {
if ($a->is_default) {
return -1;
}
if ($b->is_default) {
return 1;
}
return strcasecmp($a->info['name'], $b->info['name']);
}
/**
* Gets the name of the default region for a given theme.
*
* @param string $theme
* The name of a theme.
*
* @return string
* A string that is the region name.
*/
function system_default_region($theme) {
$regions = array_keys(system_region_list($theme, REGIONS_VISIBLE));
return $regions[0] ?? '';
}
/**
* Determines whether the current user is in compact mode.
*
* Compact mode shows certain administration pages with less description text,
* such as the configuration page and the permissions page.
*
* Whether the user is in compact mode is determined by a cookie, which is set
* for the user by \Drupal\system\Controller\SystemController::compactPage().
*
* If the user does not have the cookie, the default value is given by the
* configuration variable 'system.site.admin_compact_mode', which itself
* defaults to FALSE. This does not have a user interface to set it: it is a
* hidden variable which can be set in the settings.php file.
*
* @return bool
* TRUE when in compact mode, FALSE when in expanded mode.
*/
function system_admin_compact_mode() {
// PHP converts dots into underscores in cookie names to avoid problems with
// its parser, so we use a converted cookie name.
return \Drupal::request()->cookies
->get('Drupal_visitor_admin_compact_mode', \Drupal::config('system.site')->get('admin_compact_mode'));
}
/**
* Determines if Claro is the admin theme but not the active theme.
*
* @return bool
* TRUE if Claro is the admin theme but not the active theme.
*/
function _system_is_claro_admin_and_not_active() {
$admin_theme = \Drupal::configFactory()->get('system.theme')
->get('admin');
$active_theme = \Drupal::theme()->getActiveTheme()
->getName();
return $active_theme !== 'claro' && $admin_theme === 'claro';
}
/**
* Prepares variables for administrative content block templates.
*
* Default template: admin-block-content.html.twig.
*
* @param array $variables
* An associative array containing:
* - content: List of administrative menu items. Each menu item contains:
* - url: Path to the admin section.
* - title: Short name of the section.
* - description: Description of the administrative menu item.
* - options: URL options. See \Drupal\Core\Url::fromUri() for details.
*
* @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial
* template_preprocess functions are registered directly in hook_theme().
*
* @see https://www.drupal.org/node/3504125
*/
function template_preprocess_admin_block_content(&$variables) : void {
@trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
\Drupal::service(SystemAdminThemePreprocess::class)->preprocessAdminBlockContent($variables);
}
/**
* Prepares variables for administrative index page templates.
*
* Default template: admin-page.html.twig.
*
* @param array $variables
* An associative array containing:
* - blocks: An array of blocks to display. Each array should include a
* 'title', a 'description', a formatted 'content' and a 'position' which
* will control which container it will be in. This is usually 'left' or
* 'right'.
*
* @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial
* template_preprocess functions are registered directly in hook_theme().
*
* @see https://www.drupal.org/node/3504125
*/
function template_preprocess_admin_page(&$variables) : void {
@trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
\Drupal::service(SystemAdminThemePreprocess::class)->preprocessAdminPage($variables);
}
/**
* Prepares variables for admin index templates.
*
* Default template: system-admin-index.html.twig.
*
* @param array $variables
* An associative array containing:
* - menu_items: An array of modules to be displayed.
*
* @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial
* template_preprocess functions are registered directly in hook_theme().
*
* @see https://www.drupal.org/node/3504125
*/
function template_preprocess_system_admin_index(&$variables) : void {
@trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
\Drupal::service(SystemAdminThemePreprocess::class)->preprocessAdminIndex($variables);
}
/**
* Prepares variables for the module details templates.
*
* Default template: system-modules-details.html.twig.
*
* @param array $variables
* An associative array containing:
* - form: A render element representing the form. The main form element
* represents a package, and child elements of the form are individual
* projects. Each project (or module) is an associative array containing the
* following elements:
* - name: The name of the module.
* - enable: A checkbox for enabling the module.
* - description: A description of the module.
* - version: The version of the module.
* - links: Administration links provided by the module.
* - #requires: A list of modules that the project requires.
* - #required_by: A list of modules and themes that require the project.
* - #attributes: A list of attributes for the module wrapper.
*
* @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial
* template_preprocess functions are registered directly in hook_theme().
*
* @see https://www.drupal.org/node/3504125
* @see \Drupal\system\Form\ModulesListForm
*/
function template_preprocess_system_modules_details(&$variables) : void {
@trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
\Drupal::service(SystemAdminThemePreprocess::class)->preprocessSystemModulesDetails($variables);
}
/**
* Prepares variables for module uninstall templates.
*
* Default template: system-modules-uninstall.html.twig.
*
* @param array $variables
* An associative array containing:
* - form: A render element representing the form. Child elements of the form
* are individual modules. Each module is an associative array containing
* the following elements:
* - #module_name: The name of the module as a string.
* - name: The name of the module in a renderable array.
* - description: A description of the module.
* - #required_by: (optional) A list of modules that require the module.
* - #validation_reasons: (optional) Additional reasons why the module
* cannot be uninstalled.
* - #attributes: A list of attributes for the module wrapper.
*
* @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial
* template_preprocess functions are registered directly in hook_theme().
*
* @see https://www.drupal.org/node/3504125
* @ingroup themeable
*/
function template_preprocess_system_modules_uninstall(&$variables) : void {
@trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
\Drupal::service(SystemAdminThemePreprocess::class)->preprocessSystemModulesUninstall($variables);
}
/**
* Prepares variables for appearance page templates.
*
* Default template: system-themes-page.html.twig.
*
* @param array $variables
* An associative array containing:
* - theme_groups: An associative array containing groups of themes.
* - theme_group_titles: An associative array containing titles of themes.
*
* @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial
* template_preprocess functions are registered directly in hook_theme().
*
* @see https://www.drupal.org/node/3504125
*/
function template_preprocess_system_themes_page(&$variables) : void {
@trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
\Drupal::service(SystemAdminThemePreprocess::class)->preprocessSystemThemesPage($variables);
}
/**
* Prepares variables for security advisories fetch error message templates.
*
* Default template: system-security-advisories-fetch-error-message.html.twig.
*
* @param array $variables
* An associative array of template variables.
*
* @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial
* template_preprocess functions are registered directly in hook_theme().
*
* @see https://www.drupal.org/node/3504125
*/
function template_preprocess_system_security_advisories_fetch_error_message(array &$variables) : void {
@trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
\Drupal::service(SystemAdminThemePreprocess::class)->preprocessSystemSecurityAdvisoriesFetchErrorMessage($variables);
}
Functions
| Title | Deprecated | Summary |
|---|---|---|
| system_admin_compact_mode | Determines whether the current user is in compact mode. | |
| system_check_directory | Checks the existence of the directory specified in $form_element. | |
| system_default_region | Gets the name of the default region for a given theme. | |
| system_region_list | Get a list of available regions from a specified theme. | |
| system_sort_themes | Sorts themes by their names, with the default theme listed first. | |
| template_preprocess_admin_block_content | in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). |
Prepares variables for administrative content block templates. |
| template_preprocess_admin_page | in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). |
Prepares variables for administrative index page templates. |
| template_preprocess_entity_add_list | in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). |
Prepares variables for the list of available bundles. |
| template_preprocess_system_admin_index | in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). |
Prepares variables for admin index templates. |
| template_preprocess_system_modules_details | in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). |
Prepares variables for the module details templates. |
| template_preprocess_system_modules_uninstall | in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). |
Prepares variables for module uninstall templates. |
| template_preprocess_system_security_advisories_fetch_error_message | in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). |
Prepares variables for security advisories fetch error message templates. |
| template_preprocess_system_themes_page | in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). |
Prepares variables for appearance page templates. |
| _system_is_claro_admin_and_not_active | Determines if Claro is the admin theme but not the active theme. |
Constants
| Title | Deprecated | Summary |
|---|---|---|
| DRUPAL_DISABLED | in drupal:11.3.0 and is removed from drupal:13.0.0. Instead, use the enums provided by the relevant modules. |
Disabled option on forms and settings. |
| DRUPAL_OPTIONAL | in drupal:11.3.0 and is removed from drupal:13.0.0. Instead, use the enums provided by the relevant modules. |
Optional option on forms and settings. |
| DRUPAL_REQUIRED | in drupal:11.3.0 and is removed from drupal:13.0.0. Instead, use the enums provided by the relevant modules. |
Required option on forms and settings. |
| REGIONS_ALL | Return all regions. | |
| REGIONS_VISIBLE | Return only visible regions. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.