5 * Contains \Drupal\linkit\Plugin\Linkit\Matcher\FileMatcher.
8 namespace Drupal\linkit\Plugin\Linkit\Matcher;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\image\Entity\ImageStyle;
12 use Drupal\linkit\Utility\LinkitXss;
17 * target_entity = "file",
18 * label = @Translation("File"),
22 class FileMatcher extends EntityMatcher {
27 public function getSummary() {
28 $summery = parent::getSummary();
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'),
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'),
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(),
53 public function defaultConfiguration() {
54 return parent::defaultConfiguration() + [
56 'show_dimensions' => FALSE,
57 'show_thumbnail' => FALSE,
58 'thumbnail_image_style' => 'linkit_result_thumbnail',
66 public function calculateDependencies() {
67 $dependencies = parent::calculateDependencies() + [
71 if ($this->configuration['images']['show_thumbnail']) {
72 $dependencies['module'][] = 'image';
73 $dependencies['config'][] = 'image.style.' . $this->configuration['images']['thumbnail_image_style'];
82 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
83 $form = parent::buildConfigurationForm($form, $form_state);
85 $form['images'] = array(
87 '#title' => t('Image file settings'),
88 '#description' => t('Extra settings for image files in the result.'),
92 $form['images']['show_dimensions'] = [
93 '#title' => t('Show pixel dimensions'),
94 '#type' => 'checkbox',
95 '#default_value' => $this->configuration['images']['show_dimensions'],
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'],
105 $form['images']['thumbnail_image_style'] = [
106 '#title' => t('Thumbnail image style'),
108 '#default_value' => $this->configuration['images']['thumbnail_image_style'],
109 '#options' => image_style_options(FALSE),
112 ':input[name="images[show_thumbnail]"]' => ['checked' => TRUE],
124 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
125 parent::submitConfigurationForm($form, $form_state);
127 $values = $form_state->getValue('images');
128 if (!$values['show_thumbnail']) {
129 $values['thumbnail_image_style'] = NULL;
132 $this->configuration['images'] = $values;
138 protected function buildEntityQuery($match) {
139 $query = parent::buildEntityQuery($match);
140 $query->condition('status', FILE_STATUS_PERMANENT);
148 protected function buildDescription($entity) {
149 $description_array = array();
151 $description_array[] = parent::buildDescription($entity);
153 /** @var \Drupal\file\FileInterface $entity */
154 $file = $entity->getFileUri();
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';
163 if ($this->configuration['images']['show_thumbnail'] && $this->moduleHandler->moduleExists('image')) {
164 $image_element = array(
166 '#theme' => 'image_style',
167 '#style_name' => $this->configuration['images']['thumbnail_image_style'],
168 '#uri' => $entity->getFileUri(),
171 $description_array[] = (string) \Drupal::service('renderer')->render($image_element);
175 $description = implode('<br />' , $description_array);
176 return LinkitXss::descriptionFilter($description);
182 * The file entity still uses url() even though it's deprecated in the
185 protected function buildPath($entity) {
186 /** @var \Drupal\file\FileInterface $entity */
187 return file_url_transform_relative(file_create_url($entity->getFileUri()));