Version 1
[yaffs-website] / web / core / modules / language / language.admin.inc
1 <?php
2
3 /**
4  * @file
5  * Administration functions for language.module.
6  */
7
8 use Drupal\Core\Render\Element;
9 use Drupal\Core\Template\Attribute;
10
11 /**
12  * Prepares variables for language negotiation configuration form.
13  *
14  * Default template: language-content-configuration-form.html.twig.
15  *
16  * @param array $variables
17  *   An associative array containing:
18  *   - form: A render element representing the form.
19  */
20 function template_preprocess_language_negotiation_configure_form(&$variables) {
21   $form =& $variables['form'];
22   $variables['language_types'] = [];
23
24   foreach ($form['#language_types'] as $type) {
25     $header = [
26       t('Detection method'),
27       t('Description'),
28       t('Enabled'),
29       t('Weight'),
30     ];
31
32     // If there is at least one operation enabled show the operation column.
33     if ($form[$type]['#show_operations']) {
34       $header[] = t('Operations');
35     }
36
37     $table = [
38       '#type' => 'table',
39       '#header' => $header,
40       '#attributes' => ['id' => 'language-negotiation-methods-' . $type],
41       '#tabledrag' => [
42         [
43           'action' => 'order',
44           'relationship' => 'sibling',
45           'group' => 'language-method-weight-' . $type,
46         ],
47       ],
48     ];
49
50     foreach ($form[$type]['title'] as $id => $element) {
51       // Do not take form control structures.
52       if (is_array($element) && Element::child($id)) {
53         $table[$id]['#attributes']['class'][] = 'draggable';
54         $table[$id]['#weight'] = $element['#weight'];
55
56         $table[$id]['title'] = [
57           '#prefix' => '<strong>',
58           $form[$type]['title'][$id],
59           '#suffix' => '</strong>',
60         ];
61         $table[$id]['description'] = $form[$type]['description'][$id];
62         $table[$id]['enabled'] = $form[$type]['enabled'][$id];
63         $table[$id]['weight'] = $form[$type]['weight'][$id];
64         if ($form[$type]['#show_operations']) {
65           $table[$id]['operation'] = $form[$type]['operation'][$id];
66         }
67         // Unset to prevent rendering along with children.
68         unset($form[$type]['title'][$id]);
69         unset($form[$type]['description'][$id]);
70         unset($form[$type]['enabled'][$id]);
71         unset($form[$type]['weight'][$id]);
72         unset($form[$type]['operation'][$id]);
73       }
74     }
75
76     // Unset configurable to prevent rendering twice with children.
77     $configurable = isset($form[$type]['configurable']) ? $form[$type]['configurable'] : NULL;
78     unset($form[$type]['configurable']);
79
80     $variables['language_types'][] = [
81       'type' => $type,
82       'title' => $form[$type]['#title'],
83       'description' => $form[$type]['#description'],
84       'configurable' => $configurable,
85       'table' => $table,
86       'children' => $form[$type],
87       'attributes' => new Attribute(),
88     ];
89     // Prevent the type from rendering with the remaining form child elements.
90     unset($form[$type]);
91   }
92
93   $variables['children'] = $form;
94 }
95
96 /**
97  * Prepares variables for language content settings table templates.
98  *
99  * Default template: language-content-settings-table.html.twig.
100  *
101  * @param array $variables
102  *   An associative array containing:
103  *   - element: An associative array containing the properties of the element.
104  *     Properties used: #bundle_label, #title.
105  */
106 function template_preprocess_language_content_settings_table(&$variables) {
107   // Add a render element representing the bundle language settings table.
108   $element = $variables['element'];
109
110   $header = [
111     [
112       'data' => $element['#bundle_label'],
113       'class' => ['bundle'],
114     ],
115     [
116       'data' => t('Configuration'),
117       'class' => ['operations'],
118     ],
119   ];
120
121   $rows = [];
122   foreach (Element::children($element) as $bundle) {
123     $rows[$bundle] = [
124       'data' => [
125         [
126           'data' => [
127             '#prefix' => '<label>',
128             '#suffix' => '</label>',
129             '#plain_text' => $element[$bundle]['settings']['#label'],
130           ],
131           'class' => ['bundle'],
132         ],
133         [
134           'data' => $element[$bundle]['settings'],
135           'class' => ['operations'],
136         ],
137       ],
138       'class' => ['bundle-settings'],
139     ];
140   }
141
142   $variables['title'] = $element['#title'];
143   $variables['build'] = [
144     '#header' => $header,
145     '#rows' => $rows,
146     '#type' => 'table',
147   ];
148 }