- Issue created by @sbubaron
- Status changed to Needs review
9 months ago 10:49pm 14 February 2024 - π¦πΊAustralia larowlan π¦πΊπ.au GMT+10
You will most likely need a custom formatter that extends from LinkFormatter
In the :viewElements method you have access to $url - and on that
$url->getOption('attributes')
will be the attributes, you can adapt them there and then callsetOption
Something like this:
namespace Drupal\YOURMODULE\Plugin\Field\FieldFormatter; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Render\Element; use Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter; /** * Plugin implementation of custom icon formatter. * * @FieldFormatter( * id = "MY_MODULE_link", * label = @Translation("Link w/ icon support"), * field_types = { * "link" * } * ) */ class LinkIconFormatter extends LinkClassFormatter { /** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode) { $build = parent::viewElements($items, $langcode); foreach (Element::children($build) as $delta) { if (!empty($build[$delta]['#options']['attributes']['icon'])) { // Modify $build[$delta] here. } } return $build; } }