Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / system / system.admin.inc
1 <?php
2
3 /**
4  * @file
5  * Admin page callbacks for the system module.
6  */
7
8 use Drupal\Component\Utility\Html;
9 use Drupal\Core\Render\Element;
10 use Drupal\Core\Template\Attribute;
11
12 /**
13  * Prepares variables for administrative content block templates.
14  *
15  * Default template: admin-block-content.html.twig.
16  *
17  * @param $variables
18  *   An associative array containing:
19  *   - content: An array containing information about the block. Each element
20  *     of the array represents an administrative menu item, and must at least
21  *     contain the keys 'title', 'link_path', and 'localized_options', which are
22  *     passed to l(). A 'description' key may also be provided.
23  */
24 function template_preprocess_admin_block_content(&$variables) {
25   if (!empty($variables['content'])) {
26     $variables['compact'] = system_admin_compact_mode();
27     foreach ($variables['content'] as $key => $item) {
28       $variables['content'][$key]['link'] = \Drupal::l($item['title'], $item['url']);
29       if (!$variables['compact'] && isset($item['description'])) {
30         $variables['content'][$key]['description'] = ['#markup' => $item['description']];
31       }
32       else {
33         $variables['content'][$key]['description'] = FALSE;
34       }
35     }
36   }
37 }
38
39 /**
40  * Prepares variables for administrative index page templates.
41  *
42  * Default template: admin-page.html.twig.
43  *
44  * @param $variables
45  *   An associative array containing:
46  *   - blocks: An array of blocks to display. Each array should include a
47  *     'title', a 'description', a formatted 'content' and a 'position' which
48  *     will control which container it will be in. This is usually 'left' or
49  *     'right'.
50  */
51 function template_preprocess_admin_page(&$variables) {
52   $variables['system_compact_link'] = [
53     '#type' => 'system_compact_link',
54   ];
55   $variables['containers'] = [];
56   $stripe = 0;
57   foreach ($variables['blocks'] as $block) {
58     if (!empty($block['content']['#content'])) {
59       if (empty($block['position'])) {
60         // Perform automatic striping.
61         $block['position'] = ++$stripe % 2 ? 'left' : 'right';
62       }
63       $variables['containers'][$block['position']]['blocks'][] = [
64         '#theme' => 'admin_block',
65         '#block' => $block,
66       ];
67     }
68   }
69 }
70
71 /**
72  * Prepares variables for admin index templates.
73  *
74  * Default template: system-admin-index.html.twig.
75  *
76  * @param $variables
77  *   An associative array containing:
78  *   - menu_items: An array of modules to be displayed.
79  */
80 function template_preprocess_system_admin_index(&$variables) {
81   $variables['system_compact_link'] = [
82     '#type' => 'system_compact_link',
83   ];
84   $variables['containers'] = [];
85   $stripe = 0;
86   // Iterate over all modules.
87   foreach ($variables['menu_items'] as $module => $block) {
88     list($description, $items) = $block;
89     $position = ++$stripe % 2 ? 'left' : 'right';
90     // Output links.
91     if (count($items)) {
92       $variables['containers'][$position][] = [
93         '#theme' => 'admin_block',
94         '#block' => [
95           'position' => $position,
96           'title' => $module,
97           'content' => [
98             '#theme' => 'admin_block_content',
99             '#content' => $items,
100           ],
101           'description' => t($description),
102         ],
103       ];
104     }
105   }
106 }
107
108 /**
109  * Prepares variables for the module details templates.
110  *
111  * Default template: system-modules-details.html.twig.
112  *
113  * @param $variables
114  *   An associative array containing:
115  *   - form: A render element representing the form. The main form element
116  *     represents a package, and child elements of the form are individual
117  *     projects. Each project (or module) is an associative array containing the
118  *     following elements:
119  *     - name: The name of the module.
120  *     - enable: A checkbox for enabling the module.
121  *     - description: A description of the module.
122  *     - version: The version of the module.
123  *     - links: Administration links provided by the module.
124  *     - #requires: A list of modules that the project requires.
125  *     - #required_by: A list of modules that require the project.
126  *     - #attributes: A list of attributes for the module wrapper.
127  *
128  * @see \Drupal\system\Form\ModulesListForm
129  */
130 function template_preprocess_system_modules_details(&$variables) {
131   $form = $variables['form'];
132
133   $variables['modules'] = [];
134   // Iterate through all the modules, which are children of this element.
135   foreach (Element::children($form) as $key) {
136     // Stick the key into $module for easier access.
137     $module = $form[$key];
138     unset($module['enable']['#title']);
139     $module['#requires'] = array_filter($module['#requires']);
140     $module['#required_by'] = array_filter($module['#required_by']);
141     // Add the checkbox to allow installing new modules and to show the
142     // installation status of the module.
143     $module['checkbox'] = $module['enable'];
144
145     // Add the module label and expand/collapse functionality.
146     $id = Html::getUniqueId('module-' . $key);
147     $module['id'] = $id;
148     $module['enable_id'] = $module['enable']['#id'];
149
150     // @todo Remove early rendering and use safe_join in the Twig template once
151     //   https://www.drupal.org/node/2579091 is fixed.
152     $renderer = \Drupal::service('renderer');
153     $machine_name_render = [
154       '#prefix' => '<span dir="ltr" class="table-filter-text-source">',
155       '#plain_text' => $key,
156       '#suffix' => '</span>',
157     ];
158     $module['machine_name'] = $renderer->render($machine_name_render);
159
160     if (!empty($module['#requires'])) {
161       $requires = [
162         '#theme' => 'item_list',
163         '#items' => $module['#requires'],
164         '#context' => ['list_style' => 'comma-list'],
165       ];
166       $module['requires'] = $renderer->render($requires);
167     }
168     if (!empty($module['#required_by'])) {
169       $required_by = [
170         '#theme' => 'item_list',
171         '#items' => $module['#required_by'],
172         '#context' => ['list_style' => 'comma-list'],
173       ];
174       $module['required_by'] = $renderer->render($required_by);
175     }
176
177     if (!empty($module['version'])) {
178       $module['version'] = $renderer->render($module['version']);
179     }
180
181     $module['attributes'] = new Attribute($module['#attributes']);
182     $variables['modules'][] = $module;
183   }
184 }
185
186 /**
187  * Prepares variables for module uninstall templates.
188  *
189  * Default template: system-modules-uninstall.html.twig.
190  *
191  * @param $variables
192  *   An associative array containing:
193  *   - form: A render element representing the form. Child elements of the form
194  *     are individual modules. Each module is an associative array containing
195  *     the following elements:
196  *     - #module_name: The name of the module as a string.
197  *     - name: The name of the module in a renderable array.
198  *     - description: A description of the module.
199  *     - #required_by: (optional) A list of modules that require the module.
200  *     - #validation_reasons: (optional) Additional reasons why the module
201  *       cannot be uninstalled.
202  *     - #attributes: A list of attributes for the module wrapper.
203  *
204  * @ingroup themeable
205  */
206 function template_preprocess_system_modules_uninstall(&$variables) {
207   $form = $variables['form'];
208   $variables['modules'] = [];
209
210   // Iterate through all the modules, which are children of this element.
211   foreach (Element::children($form['modules']) as $key) {
212     $module = $form['modules'][$key];
213     $module['module_name'] = $module['#module_name'];
214     $module['checkbox'] = $form['uninstall'][$key];
215     $module['checkbox_id'] = $form['uninstall'][$key]['#id'];
216
217     if (!empty($module['#validation_reasons'])) {
218       $module['validation_reasons'] = $module['#validation_reasons'];
219       $module['reasons_count'] = count($module['validation_reasons']);
220     }
221     else {
222       $module['reasons_count'] = 0;
223     }
224     if (!empty($module['#required_by'])) {
225       $module['required_by'] = $module['#required_by'];
226       $module['reasons_count'] = $module['reasons_count'] + 1;
227     }
228     $module['attributes'] = new Attribute($module['#attributes']);
229     $variables['modules'][] = $module;
230   }
231 }
232
233 /**
234  * Prepares variables for appearance page templates.
235  *
236  * Default template: system-themes-page.html.twig.
237  *
238  * @param $variables
239  *   An associative array containing:
240  *   - theme_groups: An associative array containing groups of themes.
241  *   - theme_group_titles: An associative array containing titles of themes.
242  */
243 function template_preprocess_system_themes_page(&$variables) {
244   $groups = [];
245   $theme_groups = $variables['theme_groups'];
246   $variables['attributes']['id'] = 'system-themes-page';
247
248   foreach ($variables['theme_group_titles'] as $state => $title) {
249     if (!count($theme_groups[$state])) {
250       // Skip this group of themes if no theme is there.
251       continue;
252     }
253     // Start new theme group.
254     $theme_group = [];
255     $theme_group['state'] = $state;
256     $theme_group['title'] = $title;
257     $theme_group['themes'] = [];
258     $theme_group['attributes'] = new Attribute();
259
260     foreach ($theme_groups[$state] as $theme) {
261       $current_theme = [];
262
263       // Screenshot depicting the theme.
264       if ($theme->screenshot) {
265         $current_theme['screenshot'] = [
266           '#theme' => 'image',
267           '#uri' => $theme->screenshot['uri'],
268           '#alt' => $theme->screenshot['alt'],
269           '#title' => $theme->screenshot['title'],
270           '#attributes' => $theme->screenshot['attributes'],
271         ];
272       }
273       else {
274         $current_theme['screenshot'] = [
275           '#theme' => 'image',
276           '#uri' => drupal_get_path('module', 'system') . '/images/no_screenshot.png',
277           '#alt' => t('No screenshot'),
278           '#title' => t('No screenshot'),
279           '#attributes' => new Attribute(['class' => ['no-screenshot']]),
280         ];
281       }
282
283       // Localize the theme description.
284       $current_theme['description'] = t($theme->info['description']);
285
286       $current_theme['attributes'] = new Attribute();
287       $current_theme['name'] = $theme->info['name'];
288       $current_theme['version'] = isset($theme->info['version']) ? $theme->info['version'] : '';
289       $current_theme['notes'] = $theme->notes;
290       $current_theme['is_default'] = $theme->is_default;
291       $current_theme['is_admin'] = $theme->is_admin;
292
293       // Make sure to provide feedback on compatibility.
294       $current_theme['incompatible'] = '';
295       if (!empty($theme->incompatible_core)) {
296         $current_theme['incompatible'] = t("This theme is not compatible with Drupal @core_version. Check that the .info.yml file contains the correct 'core' value.", ['@core_version' => \Drupal::CORE_COMPATIBILITY]);
297       }
298       elseif (!empty($theme->incompatible_region)) {
299         $current_theme['incompatible'] = t("This theme is missing a 'content' region.");
300       }
301       elseif (!empty($theme->incompatible_php)) {
302         if (substr_count($theme->info['php'], '.') < 2) {
303           $theme->info['php'] .= '.*';
304         }
305         $current_theme['incompatible'] = t('This theme requires PHP version @php_required and is incompatible with PHP version @php_version.', ['@php_required' => $theme->info['php'], '@php_version' => phpversion()]);
306       }
307       elseif (!empty($theme->incompatible_base)) {
308         $current_theme['incompatible'] = t('This theme requires the base theme @base_theme to operate correctly.', ['@base_theme' => $theme->info['base theme']]);
309       }
310       elseif (!empty($theme->incompatible_engine)) {
311         $current_theme['incompatible'] = t('This theme requires the theme engine @theme_engine to operate correctly.', ['@theme_engine' => $theme->info['engine']]);
312       }
313
314       // Build operation links.
315       $current_theme['operations'] = [
316         '#theme' => 'links',
317         '#links' => $theme->operations,
318         '#attributes' => [
319           'class' => ['operations', 'clearfix'],
320         ],
321       ];
322       $theme_group['themes'][] = $current_theme;
323     }
324     $groups[] = $theme_group;
325   }
326   $variables['theme_groups'] = $groups;
327 }