Skip to content

Commit

Permalink
Issue 137 - Allow formatter to set url_only and url_plain. (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
rosiel authored Feb 24, 2025
1 parent 4cef02d commit 05a0811
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Plugin/Field/FieldFormatter/AuthorityLinkFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\Component\Utility\Unicode;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter;

/**
Expand All @@ -29,6 +30,15 @@ public static function defaultSettings() {
return $settings;
}

/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['url_only']['#access'] = $form['url_plain']['#access'] = ($this->getPluginId() == 'authority_formatter_default');
return $form;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -66,9 +76,13 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
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'])) {
$link_title = $url->toString();
}

if (!empty($settings['url_only']) && !empty($settings['url_plain'])) {
$element[$delta] = [
'#plain_text' => $link_title,
'#plain_text' => $url->toString(),
];
if (!empty($item->_attributes)) {

Expand Down

0 comments on commit 05a0811

Please sign in to comment.