Is it possible to automatically use the name of the target entity as the anchor text for a Linkit link?

For example, I know I want a link to the taxonomy term Entacmaea quadricolor, but I can only remember that it starts with "Ent". When I type "Ent" and I choose Entacmaea quadricolor from the Linkit drop-down menu, the anchor text for the link becomes /taxonomy/term/637. Instead I would like it to insert the words Entacmaea quadricolor and use them as the anchor text of the link, so I can avoid the extra steps of looking up the spelling of the term and modifying the anchor text.

To be clear, I am looking for this functionality when inserting a link via the "Link" button in CK Editor, without first selecting text to add a link to.

CommentFileSizeAuthor
#8 label_as_text_filter-3014297-8.patch1023 byteskasey_mk

Comments

Nick Hope created an issue. See original summary.

anon’s picture

Currently this is not possible.

anon’s picture

Status: Active » Closed (works as designed)
nick hope’s picture

Category: Support request » Feature request
nick hope’s picture

Status: Closed (works as designed) » Active

Re-opening after changing this to a feature request.

3cwebdev’s picture

Any update on this? Is it not possible to automatically populate the anchor text with the node title?

kasey_mk’s picture

In linkit/src/Plugin/Filter/LinkitFilter.php adding this bit just after $element->setAttribute('href', $url->getGeneratedUrl() . $query . $anchor); will replace the link text with the entity label if the link text matches the href attribute, but it only works on the page output, not in the WYSIWYG, and I don't know anything about translations:

if ($element->getAttribute('href') == $element->nodeValue) {
  $access = $entity->access('view', NULL, TRUE);
  if (!$access->isForbidden()) {
    $element->nodeValue = Html::escape($entity->label());
  }
  // Cache the linked entity access for the current user.
  $result->addCacheableDependency($access);
}
kasey_mk’s picture

StatusFileSize
new1023 bytes

In case that much is helpful to anyone else, here's a patch to LinkitFIlter.php to replace the link text with the entity label if the link text matches the href attribute on page output.

nick hope’s picture

Issue summary: View changes
nick hope’s picture

Thank you @Kasey_MK. I am not sure if your patch was supposed to achieve exactly the feature I was hoping for, but unfortunately it does not. If I insert a link, without first selecting anchor text, and choose an item from the drop-down menu, the anchor text remains the URL, even after saving the article etc.. I was hoping for the anchor text to become the entity name (in my case a taxonomy term name).

To be clear, I am looking for this functionality when clicking the "Link" button in CK Editor, without first selecting text to add a link to. I have added that to the issue description.

Further, if it were only to work on page output, not in the WYSIWYG, I doubt I would enable it on a site due to the potential for editors to get unexpected results when content is published.

klidifia’s picture

I updated this issue with some code that will use the entity title (already obtained via Linkit) as the default link text for a brand new inserted link without a selection range: #3388565: Populate displayedText with entity title when left blank

nick hope’s picture

Version: 8.x-5.x-dev » 7.x-dev