FixedTextFileUrl.php in Fixed text link formatter 8
File
src/Plugin/Field/FieldFormatter/FixedTextFileUrl.php
View source
<?php
namespace Drupal\fixed_text_link_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase;
class FixedTextFileUrl extends FileFormatterBase {
public static function defaultSettings() {
return [
'link_text' => 'Download',
'link_class' => '',
'open_in_new_window' => FALSE,
] + parent::defaultSettings();
}
public function settingsForm(array $parentForm, FormStateInterface $form_state) {
$parentForm = parent::settingsForm($parentForm, $form_state);
$form['link_text'] = [
'#type' => 'textfield',
'#title' => $this
->t('Link text'),
'#default_value' => $this
->getLinkText(),
'#required' => TRUE,
];
$form['link_class'] = [
'#type' => 'textfield',
'#title' => $this
->t('Link class'),
'#default_value' => $this
->getLinkClass(),
'#required' => FALSE,
];
$form['open_in_new_window'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Open in a new window'),
'#default_value' => $this
->openLinkInNewWindow(),
];
return $form + $parentForm;
}
private function getLinkText() {
return $this
->getSettings()['link_text'];
}
private function getLinkClass() {
return $this
->getSettings()['link_class'];
}
private function openLinkInNewWindow() {
return $this
->getSettings()['open_in_new_window'];
}
public function settingsSummary() {
$summary[] = $this
->t('Link text: @text', [
'@text' => $this
->getLinkText(),
]);
if (!empty($settings['link_class'])) {
$summary[] = $this
->t('Link class: @text', [
'@text' => $this
->getLinkClass(),
]);
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $file) {
$text = $this
->getLinkText();
$url = Url::fromUri(file_create_url($file
->getFileUri()));
$link = Link::fromTextAndUrl($text, $url);
$build = $link
->toRenderable();
$build['#attributes']['class'][] = $this
->getLinkClass();
if ($this
->openLinkInNewWindow()) {
$build['#attributes']['target'] = '_blank';
}
$cacheableMetadata = CacheableMetadata::createFromObject($file);
$cacheableMetadata
->applyTo($build);
$elements[$delta] = $build;
}
return $elements;
}
}