View source
<?php
namespace Drupal\link\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Path\PathValidatorInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\link\LinkItemInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LinkFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
protected $pathValidator;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('path.validator'));
}
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, PathValidatorInterface $path_validator) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->pathValidator = $path_validator;
}
public static function defaultSettings() {
return array(
'trim_length' => '80',
'url_only' => '',
'url_plain' => '',
'rel' => '',
'target' => '',
) + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['trim_length'] = array(
'#type' => 'number',
'#title' => t('Trim link text length'),
'#field_suffix' => t('characters'),
'#default_value' => $this
->getSetting('trim_length'),
'#min' => 1,
'#description' => t('Leave blank to allow unlimited link text lengths.'),
);
$elements['url_only'] = array(
'#type' => 'checkbox',
'#title' => t('URL only'),
'#default_value' => $this
->getSetting('url_only'),
'#access' => $this
->getPluginId() == 'link',
);
$elements['url_plain'] = array(
'#type' => 'checkbox',
'#title' => t('Show URL as plain text'),
'#default_value' => $this
->getSetting('url_plain'),
'#access' => $this
->getPluginId() == 'link',
'#states' => array(
'visible' => array(
':input[name*="url_only"]' => array(
'checked' => TRUE,
),
),
),
);
$elements['rel'] = array(
'#type' => 'checkbox',
'#title' => t('Add rel="nofollow" to links'),
'#return_value' => 'nofollow',
'#default_value' => $this
->getSetting('rel'),
);
$elements['target'] = array(
'#type' => 'checkbox',
'#title' => t('Open link in new window'),
'#return_value' => '_blank',
'#default_value' => $this
->getSetting('target'),
);
return $elements;
}
public function settingsSummary() {
$summary = array();
$settings = $this
->getSettings();
if (!empty($settings['trim_length'])) {
$summary[] = t('Link text trimmed to @limit characters', array(
'@limit' => $settings['trim_length'],
));
}
else {
$summary[] = t('Link text not trimmed');
}
if ($this
->getPluginId() == 'link' && !empty($settings['url_only'])) {
if (!empty($settings['url_plain'])) {
$summary[] = t('Show URL only as plain-text');
}
else {
$summary[] = t('Show URL only');
}
}
if (!empty($settings['rel'])) {
$summary[] = t('Add rel="@rel"', array(
'@rel' => $settings['rel'],
));
}
if (!empty($settings['target'])) {
$summary[] = t('Open link in new window');
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = array();
$entity = $items
->getEntity();
$settings = $this
->getSettings();
foreach ($items as $delta => $item) {
$url = $this
->buildUrl($item);
$link_title = $url
->toString();
if (empty($settings['url_only']) && !empty($item->title)) {
$link_title = \Drupal::token()
->replace($item->title, [
$entity
->getEntityTypeId() => $entity,
], [
'clear' => TRUE,
]);
}
if (!empty($settings['trim_length'])) {
$link_title = Unicode::truncate($link_title, $settings['trim_length'], FALSE, TRUE);
}
if (!empty($settings['url_only']) && !empty($settings['url_plain'])) {
$element[$delta] = array(
'#plain_text' => $link_title,
);
if (!empty($item->_attributes)) {
$content = str_replace('internal:/', '', $item->uri);
$item->_attributes += array(
'content' => $content,
);
}
}
else {
$element[$delta] = array(
'#type' => 'link',
'#title' => $link_title,
'#options' => $url
->getOptions(),
);
$element[$delta]['#url'] = $url;
if (!empty($item->_attributes)) {
$element[$delta]['#options'] += array(
'attributes' => array(),
);
$element[$delta]['#options']['attributes'] += $item->_attributes;
unset($item->_attributes);
}
}
}
return $element;
}
protected function buildUrl(LinkItemInterface $item) {
$url = $item
->getUrl() ?: Url::fromRoute('<none>');
$settings = $this
->getSettings();
$options = $item->options;
if (!empty($settings['rel'])) {
$options['attributes']['rel'] = $settings['rel'];
}
if (!empty($settings['target'])) {
$options['attributes']['target'] = $settings['target'];
}
$url
->setOptions($options);
return $url;
}
}