4 * Contains hooks implementations, prerender callback and common functions.
8 use Drupal\Component\Serialization\Json;
11 * Implements hook_theme().
13 function layouter_theme($existing, $type, $theme, $path) {
15 'layouter_image_only' => [
16 'variables' => ['image' => NULL],
17 'template' => 'image_only',
19 'layouter_two_columns' => [
20 'variables' => ['text' => NULL],
21 'template' => 'two_columns',
23 'layouter_two_columns_img_left_text_right' => [
29 'template' => 'two_columns_img_left_text_right',
31 'layouter_two_columns_img_right_text_left' => [
37 'template' => 'two_columns_img_right_text_left',
39 'layouter_two_columns_text_img_left' => [
44 'template' => 'two_columns_text_img_left',
46 'layouter_big_img_text_below' => [
51 'template' => 'big_img_text_below',
53 'layouter_big_img_text_above' => [
58 'template' => 'big_img_text_above',
60 'layouter_big_img_two_column_text_below' => [
65 'template' => 'big_img_two_column_text_below',
67 'layouter_big_img_two_column_text_above' => [
72 'template' => 'big_img_two_column_text_above',
78 * Implements hook_layouter_templates_info().
80 function layouter_layouter_templates_info() {
83 'title' => t('One image only'),
87 'title' => t('One image'),
88 'description' => t('Image will be align left.'),
91 'theme' => 'layouter_image_only',
94 'title' => t('Two columns of continuous text'),
98 'title' => t('Two column text'),
99 'description' => t('Text will be divided on two columns.'),
102 'theme' => 'layouter_two_columns',
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'),
109 'description' => t('Text will be will be placed on the right column.'),
116 'title' => t('Caption'),
119 'theme' => 'layouter_two_columns_img_left_text_right',
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'),
132 'title' => t('Caption'),
135 'theme' => 'layouter_two_columns_img_right_text_left',
137 'two_columns_text_img_left' => [
138 'title' => t('Two columns of continuous text with an image on top left'),
147 'theme' => 'layouter_two_columns_text_img_left',
149 'big_img_text_below' => [
150 'title' => t('Big image on top with a text below'),
159 'theme' => 'layouter_big_img_text_below',
161 'big_img_text_above' => [
162 'title' => t('Big image at bottom with a text above'),
171 'theme' => 'layouter_big_img_text_above',
173 'big_img_two_column_text_below' => [
174 'title' => t('Big image on top with a two columns of text below'),
183 'theme' => 'layouter_big_img_two_column_text_below',
185 'big_img_two_column_text_above' => [
186 'title' => t('Big image at bottom with a two columns of text above'),
195 'theme' => 'layouter_big_img_two_column_text_above',
203 * Implements hook_element_info_alter().
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';
212 * Processes textarea element if it is allowed to enable Layouter.
214 * @param array $element
215 * Form element to process.
218 * Element after process.
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');
227 elseif (!is_null($edit_page_type)) {
228 $current_type = $edit_page_type->bundle();
234 $active_content_types = layouter_active_content_types();
235 if (!in_array($current_type, $active_content_types)) {
239 if (isset($element['value'])) {
240 $element['value'] = layouter_load_by_field($element['value']);
243 $element = layouter_load_by_field($element);
250 * Enables Layouter for given textarea field.
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) {
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';
271 if (!isset($processed_ids[$field['#id']])) {
272 $processed_ids[$field['#id']] = [];
274 $textarea_id = $field['#id'];
278 '#url' => Url::fromRoute(
280 ['textarea_id' => $textarea_id]
282 '#title' => t('Select the text template'),
284 'id' => ['layouter-' . $textarea_id],
285 'class' => ['use-ajax', 'layouter-link', $textarea_id],
286 'data-dialog-type' => 'modal',
287 'data-dialog-options' => Json::encode([
289 'title' => t('Layouter'),
290 'dialogClass' => 'no-close',
292 'title' => t('Click to select the text template with a simplified form of layout'),
295 $link = render($link);
296 $suffix = '<div class="filter-wrapper layouter-link-wrapper">'
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;
308 * Gets the list of text formats for which Layouter is enabled.
311 * Array of allowed formats.
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) {
320 $text_formats_enabled[] = $text_format;
324 return $text_formats_enabled;
328 * Gets the list of content types for which Layouter is enabled.
331 * Array of allowed types.
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) {
340 $content_types_enabled[] = $content_type;
344 return $content_types_enabled;