Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Administration pages for image settings.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 use Drupal\Core\Render\Element;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Prepares variables for image style preview templates.
|
Chris@0
|
12 *
|
Chris@0
|
13 * Default template: image-style-preview.html.twig.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @param array $variables
|
Chris@0
|
16 * An associative array containing:
|
Chris@0
|
17 * - style: \Drupal\image\ImageStyleInterface image style being previewed.
|
Chris@0
|
18 */
|
Chris@0
|
19 function template_preprocess_image_style_preview(&$variables) {
|
Chris@0
|
20 // Style information.
|
Chris@0
|
21 $style = $variables['style'];
|
Chris@0
|
22 $variables['style_id'] = $style->id();
|
Chris@0
|
23 $variables['style_name'] = $style->label();
|
Chris@0
|
24
|
Chris@0
|
25 // Cache bypass token.
|
Chris@0
|
26 $variables['cache_bypass'] = REQUEST_TIME;
|
Chris@0
|
27
|
Chris@0
|
28 // Sample image info.
|
Chris@0
|
29 $sample_width = 160;
|
Chris@0
|
30 $sample_height = 160;
|
Chris@0
|
31 $image_factory = \Drupal::service('image.factory');
|
Chris@0
|
32
|
Chris@0
|
33 // Set up original file information.
|
Chris@0
|
34 $original_path = \Drupal::config('image.settings')->get('preview_image');
|
Chris@0
|
35 $original_image = $image_factory->get($original_path);
|
Chris@0
|
36 $variables['original'] = [
|
Chris@0
|
37 'url' => file_url_transform_relative(file_create_url($original_path)),
|
Chris@0
|
38 'width' => $original_image->getWidth(),
|
Chris@0
|
39 'height' => $original_image->getHeight(),
|
Chris@0
|
40 ];
|
Chris@0
|
41 if ($variables['original']['width'] > $variables['original']['height']) {
|
Chris@0
|
42 $variables['preview']['original']['width'] = min($variables['original']['width'], $sample_width);
|
Chris@0
|
43 $variables['preview']['original']['height'] = round($variables['preview']['original']['width'] / $variables['original']['width'] * $variables['original']['height']);
|
Chris@0
|
44 }
|
Chris@0
|
45 else {
|
Chris@0
|
46 $variables['preview']['original']['height'] = min($variables['original']['height'], $sample_height);
|
Chris@0
|
47 $variables['preview']['original']['width'] = round($variables['preview']['original']['height'] / $variables['original']['height'] * $variables['original']['width']);
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 // Set up derivative file information.
|
Chris@0
|
51 $preview_file = $style->buildUri($original_path);
|
Chris@0
|
52 // Create derivative if necessary.
|
Chris@0
|
53 if (!file_exists($preview_file)) {
|
Chris@0
|
54 $style->createDerivative($original_path, $preview_file);
|
Chris@0
|
55 }
|
Chris@0
|
56 $preview_image = $image_factory->get($preview_file);
|
Chris@0
|
57 $variables['derivative'] = [
|
Chris@0
|
58 'url' => file_url_transform_relative(file_create_url($preview_file)),
|
Chris@0
|
59 'width' => $preview_image->getWidth(),
|
Chris@0
|
60 'height' => $preview_image->getHeight(),
|
Chris@0
|
61 ];
|
Chris@0
|
62 if ($variables['derivative']['width'] > $variables['derivative']['height']) {
|
Chris@0
|
63 $variables['preview']['derivative']['width'] = min($variables['derivative']['width'], $sample_width);
|
Chris@0
|
64 $variables['preview']['derivative']['height'] = round($variables['preview']['derivative']['width'] / $variables['derivative']['width'] * $variables['derivative']['height']);
|
Chris@0
|
65 }
|
Chris@0
|
66 else {
|
Chris@0
|
67 $variables['preview']['derivative']['height'] = min($variables['derivative']['height'], $sample_height);
|
Chris@0
|
68 $variables['preview']['derivative']['width'] = round($variables['preview']['derivative']['height'] / $variables['derivative']['height'] * $variables['derivative']['width']);
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 // Build the preview of the original image.
|
Chris@0
|
72 $variables['original']['rendered'] = [
|
Chris@0
|
73 '#theme' => 'image',
|
Chris@0
|
74 '#uri' => $original_path,
|
Chris@0
|
75 '#alt' => t('Sample original image'),
|
Chris@0
|
76 '#title' => '',
|
Chris@0
|
77 '#attributes' => [
|
Chris@0
|
78 'width' => $variables['original']['width'],
|
Chris@0
|
79 'height' => $variables['original']['height'],
|
Chris@0
|
80 'style' => 'width: ' . $variables['preview']['original']['width'] . 'px; height: ' . $variables['preview']['original']['height'] . 'px;',
|
Chris@0
|
81 ],
|
Chris@0
|
82 ];
|
Chris@0
|
83
|
Chris@0
|
84 // Build the preview of the image style derivative. Timestamps are added
|
Chris@0
|
85 // to prevent caching of images on the client side.
|
Chris@0
|
86 $variables['derivative']['rendered'] = [
|
Chris@0
|
87 '#theme' => 'image',
|
Chris@0
|
88 '#uri' => $variables['derivative']['url'] . '?cache_bypass=' . $variables['cache_bypass'],
|
Chris@0
|
89 '#alt' => t('Sample modified image'),
|
Chris@0
|
90 '#title' => '',
|
Chris@0
|
91 '#attributes' => [
|
Chris@0
|
92 'width' => $variables['derivative']['width'],
|
Chris@0
|
93 'height' => $variables['derivative']['height'],
|
Chris@0
|
94 'style' => 'width: ' . $variables['preview']['derivative']['width'] . 'px; height: ' . $variables['preview']['derivative']['height'] . 'px;',
|
Chris@0
|
95 ],
|
Chris@0
|
96 ];
|
Chris@0
|
97
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 /**
|
Chris@0
|
101 * Prepares variables for image anchor templates.
|
Chris@0
|
102 *
|
Chris@0
|
103 * Default template: image-anchor.html.twig.
|
Chris@0
|
104 *
|
Chris@0
|
105 * @param array $variables
|
Chris@0
|
106 * An associative array containing:
|
Chris@0
|
107 * - element: An associative array containing the image.
|
Chris@0
|
108 */
|
Chris@0
|
109 function template_preprocess_image_anchor(&$variables) {
|
Chris@0
|
110 $element = $variables['element'];
|
Chris@0
|
111
|
Chris@0
|
112 $rows = [];
|
Chris@0
|
113 $row = [];
|
Chris@0
|
114 foreach (Element::children($element) as $n => $key) {
|
Chris@0
|
115 $element[$key]['#attributes']['title'] = $element[$key]['#title'];
|
Chris@0
|
116 unset($element[$key]['#title']);
|
Chris@0
|
117 $row[] = [
|
Chris@0
|
118 'data' => $element[$key],
|
Chris@0
|
119 ];
|
Chris@0
|
120 if ($n % 3 == 3 - 1) {
|
Chris@0
|
121 $rows[] = $row;
|
Chris@0
|
122 $row = [];
|
Chris@0
|
123 }
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 $variables['table'] = [
|
Chris@0
|
127 '#type' => 'table',
|
Chris@0
|
128 '#header' => [],
|
Chris@0
|
129 '#rows' => $rows,
|
Chris@0
|
130 '#attributes' => [
|
Chris@0
|
131 'class' => ['image-anchor'],
|
Chris@0
|
132 ],
|
Chris@0
|
133 ];
|
Chris@0
|
134 }
|