cb39ccfe6964290fe726532f521154b61749c3f0
[yaffs-website] / web / modules / contrib / linkit / src / Plugin / Linkit / Matcher / FileMatcher.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Plugin\Linkit\Matcher\FileMatcher.
6  */
7
8 namespace Drupal\linkit\Plugin\Linkit\Matcher;
9
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\image\Entity\ImageStyle;
12 use Drupal\linkit\Utility\LinkitXss;
13
14 /**
15  * @Matcher(
16  *   id = "entity:file",
17  *   target_entity = "file",
18  *   label = @Translation("File"),
19  *   provider = "file"
20  * )
21  */
22 class FileMatcher extends EntityMatcher {
23
24   /**
25    * {@inheritdoc}
26    */
27   public function getSummary() {
28     $summery = parent::getSummary();
29
30     $summery[] = $this->t('Show image dimensions: @show_image_dimensions', [
31       '@show_image_dimensions' => $this->configuration['images']['show_dimensions'] ? $this->t('Yes') : $this->t('No'),
32     ]);
33
34     $summery[] = $this->t('Show image thumbnail: @show_image_thumbnail', [
35       '@show_image_thumbnail' => $this->configuration['images']['show_thumbnail'] ? $this->t('Yes') : $this->t('No'),
36     ]);
37
38     if ($this->moduleHandler->moduleExists('image') && $this->configuration['images']['show_thumbnail']) {
39       $image_style = ImageStyle::load($this->configuration['images']['thumbnail_image_style']);
40         if (!is_null($image_style)) {
41           $summery[] = $this->t('Thumbnail style: @thumbnail_style', [
42           '@thumbnail_style' =>  $image_style->label(),
43         ]);
44       }
45     }
46
47     return $summery;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function defaultConfiguration() {
54     return parent::defaultConfiguration() + [
55       'images' => [
56         'show_dimensions' => FALSE,
57         'show_thumbnail' => FALSE,
58         'thumbnail_image_style' => 'linkit_result_thumbnail',
59       ],
60     ];
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function calculateDependencies() {
67     $dependencies = parent::calculateDependencies() + [
68       'module' => ['file'],
69     ];
70
71     if ($this->configuration['images']['show_thumbnail']) {
72       $dependencies['module'][] = 'image';
73       $dependencies['config'][] = 'image.style.' . $this->configuration['images']['thumbnail_image_style'];
74     }
75
76     return $dependencies;
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
83     $form = parent::buildConfigurationForm($form, $form_state);
84
85     $form['images'] = array(
86       '#type' => 'details',
87       '#title' => t('Image file settings'),
88       '#description' => t('Extra settings for image files in the result.'),
89       '#tree' => TRUE,
90     );
91
92     $form['images']['show_dimensions'] = [
93       '#title' => t('Show pixel dimensions'),
94       '#type' => 'checkbox',
95       '#default_value' => $this->configuration['images']['show_dimensions'],
96     ];
97
98     if ($this->moduleHandler->moduleExists('image')) {
99       $form['images']['show_thumbnail'] = [
100         '#title' => t('Show thumbnail'),
101         '#type' => 'checkbox',
102         '#default_value' => $this->configuration['images']['show_thumbnail'],
103       ];
104
105       $form['images']['thumbnail_image_style'] = [
106         '#title' => t('Thumbnail image style'),
107         '#type' => 'select',
108         '#default_value' => $this->configuration['images']['thumbnail_image_style'],
109         '#options' => image_style_options(FALSE),
110         '#states' => [
111           'visible' => [
112             ':input[name="images[show_thumbnail]"]' => ['checked' => TRUE],
113           ],
114         ],
115       ];
116     }
117
118     return $form;
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
125     parent::submitConfigurationForm($form, $form_state);
126
127     $values = $form_state->getValue('images');
128     if (!$values['show_thumbnail']) {
129       $values['thumbnail_image_style'] = NULL;
130     }
131
132     $this->configuration['images'] = $values;
133   }
134
135   /**
136    * {@inheritdoc}
137    */
138   protected function buildEntityQuery($match) {
139     $query = parent::buildEntityQuery($match);
140     $query->condition('status', FILE_STATUS_PERMANENT);
141
142     return $query;
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   protected function buildDescription($entity) {
149     $description_array = array();
150
151     $description_array[] = parent::buildDescription($entity);
152
153     /** @var \Drupal\file\FileInterface $entity */
154     $file = $entity->getFileUri();
155
156     /** @var \Drupal\Core\Image\ImageInterface $image */
157     $image = \Drupal::service('image.factory')->get($file);
158     if ($image->isValid()) {
159       if ($this->configuration['images']['show_dimensions']) {
160         $description_array[] = $image->getWidth() . 'x' . $image->getHeight() . 'px';
161       }
162
163       if ($this->configuration['images']['show_thumbnail'] && $this->moduleHandler->moduleExists('image')) {
164         $image_element = array(
165           '#weight' => -10,
166           '#theme' => 'image_style',
167           '#style_name' => $this->configuration['images']['thumbnail_image_style'],
168           '#uri' => $entity->getFileUri(),
169         );
170
171         $description_array[] = (string) \Drupal::service('renderer')->render($image_element);
172       }
173     }
174
175     $description = implode('<br />' , $description_array);
176     return LinkitXss::descriptionFilter($description);
177   }
178
179   /**
180    * {@inheritdoc}
181    *
182    * The file entity still uses url() even though it's deprecated in the
183    * entity interface.
184    */
185   protected function buildPath($entity) {
186     /** @var \Drupal\file\FileInterface $entity */
187     return file_url_transform_relative(file_create_url($entity->getFileUri()));
188   }
189 }