diff --git a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php index 7d526df..0a861de 100644 --- a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php @@ -33,6 +33,8 @@ class ImageUrlFormatter extends ImageFormatter { public static function defaultSettings() { return [ 'image_style' => '', + 'url_link' => '', + 'trim_length' => '80', ]; } @@ -48,6 +50,19 @@ public function settingsForm(array $form, FormStateInterface $form_state) { '#description' => $this->t('If checked, the URL to image will get a click-able link otherwise, the plain URL will be displayed.'), '#default_value' => $this->getSetting('url_link'), ]; + $element['trim_length'] = [ + '#type' => 'number', + '#title' => $this->t('Trim link text length'), + '#field_suffix' => $this->t('characters'), + '#default_value' => $this->getSetting('trim_length'), + '#min' => 1, + '#description' => $this->t('Leave blank to allow unlimited link text lengths.'), + '#states' => [ + 'invisible' => [ + ":input[name=\"fields[{$this->fieldDefinition->getName()}][settings_edit_form][settings][url_link]\"]" => ['checked' => FALSE], + ], + ], + ]; unset($element['image_link']);; @@ -67,10 +82,10 @@ public function settingsSummary() { // their styles in code. $image_style_setting = $this->getSetting('image_style'); if (isset($image_styles[$image_style_setting])) { - $summary[] = t('Image style: @style', array('@style' => $image_styles[$image_style_setting])); + $summary[] = $this->t('Image style: @style', ['@style' => $image_styles[$image_style_setting]]); } else { - $summary[] = t('Original image'); + $summary[] = $this->t('Original image'); } return $summary; @@ -81,6 +96,7 @@ public function settingsSummary() { */ public function viewElements(FieldItemListInterface $items, $langcode) { $elements = []; + $settings = $this->getSettings(); /** * @var \Drupal\file\Entity\File[] $images @@ -92,7 +108,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } /** @var \Drupal\image\ImageStyleInterface $image_style */ - $image_style = $this->imageStyleStorage->load($this->getSetting('image_style')); + $image_style = $this->imageStyleStorage->load($settings['image_style']); foreach ($images as $delta => $image) { $image_uri = $image->getFileUri(); @@ -104,8 +120,22 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $cacheable_metadata->addCacheableDependency(CacheableMetadata::createFromObject($image_style)); } - // Plain text URL. - $elements[$delta] = ['#markup' => $url]; + if ($settings['url_link']) { + // Linked url. + $link_title = $url; + if (!empty($settings['trim_length'])) { + $link_title = Unicode::truncate($link_title, $settings['trim_length'], FALSE, TRUE); + } + $elements[$delta] = [ + '#type' => 'link', + '#url' => Url::fromUri(file_create_url($url)), + '#title' => $link_title, + ]; + } + else { + // Plain text URL. + $elements[$delta] = ['#markup' => $url]; + } $cacheable_metadata->applyTo($elements[$delta]); }