Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / layouter / layouter.module
1 <?php
2 /**
3  * @file
4  * Contains hooks implementations, prerender callback and common functions.
5  */
6
7 use Drupal\Core\Url;
8 use Drupal\Component\Serialization\Json;
9
10 /**
11  * Implements hook_theme().
12  */
13 function layouter_theme($existing, $type, $theme, $path) {
14   return [
15     'layouter_image_only' => [
16       'variables' => ['image' => NULL],
17       'template' => 'image_only',
18     ],
19     'layouter_two_columns' => [
20       'variables' => ['text' => NULL],
21       'template' => 'two_columns',
22     ],
23     'layouter_two_columns_img_left_text_right' => [
24       'variables' => [
25         'text' => NULL,
26         'image' => NULL,
27         'caption' => NULL,
28       ],
29       'template' => 'two_columns_img_left_text_right',
30     ],
31     'layouter_two_columns_img_right_text_left' => [
32       'variables' => [
33         'text' => NULL,
34         'image' => NULL,
35         'caption' => NULL,
36       ],
37       'template' => 'two_columns_img_right_text_left',
38     ],
39     'layouter_two_columns_text_img_left' => [
40       'variables' => [
41         'text' => NULL,
42         'image' => NULL,
43       ],
44       'template' => 'two_columns_text_img_left',
45     ],
46     'layouter_big_img_text_below' => [
47       'variables' => [
48         'text' => NULL,
49         'image' => NULL,
50       ],
51       'template' => 'big_img_text_below',
52     ],
53     'layouter_big_img_text_above' => [
54       'variables' => [
55         'text' => NULL,
56         'image' => NULL,
57       ],
58       'template' => 'big_img_text_above',
59     ],
60     'layouter_big_img_two_column_text_below' => [
61       'variables' => [
62         'text' => NULL,
63         'image' => NULL,
64       ],
65       'template' => 'big_img_two_column_text_below',
66     ],
67     'layouter_big_img_two_column_text_above' => [
68       'variables' => [
69         'text' => NULL,
70         'image' => NULL,
71       ],
72       'template' => 'big_img_two_column_text_above',
73     ],
74   ];
75 }
76
77 /**
78  * Implements hook_layouter_templates_info().
79  */
80 function layouter_layouter_templates_info() {
81   $templates = [
82     'image_only' => [
83       'title' => t('One image only'),
84       'fields' => [
85         'image' => [
86           'type' => 'image',
87           'title' => t('One image'),
88           'description' => t('Image will be align left.'),
89         ],
90       ],
91       'theme' => 'layouter_image_only',
92     ],
93     'two_columns' => [
94       'title' => t('Two columns of continuous text'),
95       'fields' => [
96         'text' => [
97           'type' => 'text',
98           'title' => t('Two column text'),
99           'description' => t('Text will be divided on two columns.'),
100         ],
101       ],
102       'theme' => 'layouter_two_columns',
103     ],
104     'two_columns_img_left_text_right' => [
105       'title' => t('Two columns with an image (with an optional description) on the left side and a text on the right'),
106       'fields' => [
107         'text' => [
108           'type' => 'text',
109           'description' => t('Text will be will be placed on the right column.'),
110         ],
111         'image' => [
112           'type' => 'image',
113         ],
114         'caption' => [
115           'type' => 'text',
116           'title' => t('Caption'),
117         ],
118       ],
119       'theme' => 'layouter_two_columns_img_left_text_right',
120     ],
121     'two_columns_img_right_text_left' => [
122       'title' => t('Two columns with an image (with an optional description) on the right side and a text on the left'),
123       'fields' => [
124         'text' => [
125           'type' => 'text',
126         ],
127         'image' => [
128           'type' => 'image',
129         ],
130         'caption' => [
131           'type' => 'text',
132           'title' => t('Caption'),
133         ],
134       ],
135       'theme' => 'layouter_two_columns_img_right_text_left',
136     ],
137     'two_columns_text_img_left' => [
138       'title' => t('Two columns of continuous text with an image on top left'),
139       'fields' => [
140         'text' => [
141           'type' => 'text',
142         ],
143         'image' => [
144           'type' => 'image',
145         ],
146       ],
147       'theme' => 'layouter_two_columns_text_img_left',
148     ],
149     'big_img_text_below' => [
150       'title' => t('Big image on top with a text below'),
151       'fields' => [
152         'text' => [
153           'type' => 'text',
154         ],
155         'image' => [
156           'type' => 'image',
157         ],
158       ],
159       'theme' => 'layouter_big_img_text_below',
160     ],
161     'big_img_text_above' => [
162       'title' => t('Big image at bottom with a text above'),
163       'fields' => [
164         'text' => [
165           'type' => 'text',
166         ],
167         'image' => [
168           'type' => 'image',
169         ],
170       ],
171       'theme' => 'layouter_big_img_text_above',
172     ],
173     'big_img_two_column_text_below' => [
174       'title' => t('Big image on top with a two columns of text below'),
175       'fields' => [
176         'text' => [
177           'type' => 'text',
178         ],
179         'image' => [
180           'type' => 'image',
181         ],
182       ],
183       'theme' => 'layouter_big_img_two_column_text_below',
184     ],
185     'big_img_two_column_text_above' => [
186       'title' => t('Big image at bottom with a two columns of text above'),
187       'fields' => [
188         'text' => [
189           'type' => 'text',
190         ],
191         'image' => [
192           'type' => 'image',
193         ],
194       ],
195       'theme' => 'layouter_big_img_two_column_text_above',
196     ],
197   ];
198
199   return $templates;
200 }
201
202 /**
203  * Implements hook_element_info_alter().
204  */
205 function layouter_element_info_alter(array &$types) {
206   if (\Drupal::currentUser()->hasPermission('use layouter')) {
207     $types['text_format']['#pre_render'][] = 'layouter_text_format_pre_render';
208   }
209 }
210
211 /**
212  * Processes textarea element if it is allowed to enable Layouter.
213  *
214  * @param array $element
215  *   Form element to process.
216  *
217  * @return mixed
218  *   Element after process.
219  */
220 function layouter_text_format_pre_render($element) {
221   // Check current content type is in module active types list.
222   $create_page_type = \Drupal::routeMatch()->getParameter('node_type');
223   $edit_page_type = \Drupal::routeMatch()->getParameter('node');
224   if (!is_null($create_page_type)) {
225     $current_type = $create_page_type->get('type');
226   }
227   elseif (!is_null($edit_page_type)) {
228     $current_type = $edit_page_type->bundle();
229   }
230   else {
231     return $element;
232   }
233
234   $active_content_types = layouter_active_content_types();
235   if (!in_array($current_type, $active_content_types)) {
236     return $element;
237   }
238
239   if (isset($element['value'])) {
240     $element['value'] = layouter_load_by_field($element['value']);
241   }
242   else {
243     $element = layouter_load_by_field($element);
244   }
245
246   return $element;
247 }
248
249 /**
250  * Enables Layouter for given textarea field.
251  *
252  * @param $field
253  * @return mixed
254  */
255 function layouter_load_by_field($field) {
256   static $processed_ids = [];
257   $processed = !isset($field['#id']) || isset($processed_ids[$field['#id']]) || $field['#id'] == 'edit-log';
258   $not_accessible = isset($field['#access']) && !$field['#access'];
259   $disabled = isset($field['#attributes']['disabled']) && $field['#attributes']['disabled'] == 'disabled';
260   if ($processed || $not_accessible || $disabled) {
261     return $field;
262   }
263
264   $active_text_formats = layouter_active_text_formats();
265   $js_settings['window']['active_text_formats'] = $active_text_formats;
266   $js_settings['window']['textareas_id'][$field['#id']] = $field['#id'];
267   $field['#attached']['drupalSettings']['layouter'] = $js_settings;
268   $field['#attached']['library'][] = 'layouter/form';
269   $field['#attached']['library'][] = 'core/drupal.dialog.ajax';
270
271   if (!isset($processed_ids[$field['#id']])) {
272     $processed_ids[$field['#id']] = [];
273   }
274   $textarea_id = $field['#id'];
275   $class = 'layouter';
276   $link = [
277     '#type' => 'link',
278     '#url' => Url::fromRoute(
279       'layouter.form',
280       ['textarea_id' => $textarea_id]
281     ),
282     '#title' => t('Select the text template'),
283     '#attributes' => [
284       'id' => ['layouter-' . $textarea_id],
285       'class' => ['use-ajax', 'layouter-link', $textarea_id],
286       'data-dialog-type' => 'modal',
287       'data-dialog-options' => Json::encode([
288         'width' => '75%',
289         'title' => t('Layouter'),
290         'dialogClass' => 'no-close',
291       ]),
292       'title' => t('Click to select the text template with a simplified form of layout'),
293     ],
294   ];
295   $link = render($link);
296   $suffix = '<div class="filter-wrapper layouter-link-wrapper">'
297     . $link . '</div>';
298
299   // Remember extra information and reuse it during "Preview".
300   $processed_ids[$field['#id']]['suffix'] = $suffix;
301   $processed_ids[$field['#id']]['class'][] = $class;
302   $field['#suffix'] = (empty($field['#suffix'])) ? $suffix : $field['#suffix'] . $suffix;
303   $field['#attributes']['class'][] = $class;
304   return $field;
305 }
306
307 /**
308  * Gets the list of text formats for which Layouter is enabled.
309  *
310  * @return array
311  *   Array of allowed formats.
312  */
313 function layouter_active_text_formats() {
314   $layouter_text_formats = \Drupal::config('layouter.settings')
315     ->get('text_formats');
316   $text_formats_enabled = [];
317   if ($layouter_text_formats) {
318     foreach ($layouter_text_formats as $text_format) {
319       if ($text_format) {
320         $text_formats_enabled[] = $text_format;
321       }
322     }
323   }
324   return $text_formats_enabled;
325 }
326
327 /**
328  * Gets the list of content types for which Layouter is enabled.
329  *
330  * @return array
331  *   Array of allowed types.
332  */
333 function layouter_active_content_types() {
334   $layouter_content_types = \Drupal::config('layouter.settings')
335     ->get('content_types');
336   $content_types_enabled = [];
337   if ($layouter_content_types) {
338     foreach ($layouter_content_types as $content_type) {
339       if ($content_type) {
340         $content_types_enabled[] = $content_type;
341       }
342     }
343   }
344   return $content_types_enabled;
345 }