It's technically possible to link to anchors in the following way:

  • If linking to an anchor on the same page, just use #anchor-name as the URL. LinkIt searcher will say that no content has been found matching that URL and it will be placed as is.
  • If linking to an anchor on some internal path that you just searched for, you can also manually append the #anchor-name to the end of the URL

The 2nd way is the problematic one. The problem is that when you append the fragment to the path, LinkIt will no longer return the data-entity-type and data-entity-uuid attribues for the <a&rt; tag that it inserts. This means that any substitution plugins (like one that converts an internal node path to the proper path alias) will no longer work.

I wonder if there's a way to improve this behavior so that fragments manually added to a URL do not interfere with the process.

CommentFileSizeAuthor
#88 linkit-3022261-88.patch13.84 KBalexander tallqvist
#76 3022261-76.patch13.96 KBboromino
#75 linkit7.gif351.58 KBboromino
#52 linkit_strips_anchor_portion_when_trying_to_save.png1.4 MBbyronveale
#52 linkit_matches_content.png1.42 MBbyronveale
#50 linkit-anchors-demo.gif5.56 MBmark_fullmer
#41 interdiff-39-41.diff6.7 KBjurgenhaas
#41 3022261-41.patch18.75 KBjurgenhaas
#39 3022261-39.patch18.88 KBmrinalini9
#37 interdiff-34-36.txt1.5 KBsleitner
#36 3022261-36.patch18.46 KBsleitner
#36 3022261-36.patch18.46 KBsleitner
#35 3022261-35.patch18.6 KBsleitner
#35 3022261-35.patch18.6 KBsleitner
#34 3022261-34.patch18.64 KBsleitner
#34 3022261-34.patch18.64 KBsleitner
#33 3022261-33.patch18.45 KBsleitner
#33 3022261-33.patch18.45 KBsleitner
#31 3022261-31.patch18.47 KBcasey
#29 3022261-29.patch17.65 KBcasey
#28 3022261-26.patch15.73 KBsleitner
#27 3022261-22-8.x-5.x.patch15.69 KBsleitner
#26 3022261-26.patch15.73 KBsleitner
#22 3022261_linkit_add-better-support-for-linking-to-anchors_22.patch15.69 KBsleitner
#21 3022261_linkit_add-better-support-for-linking-to-anchors_21.patch15.68 KBsleitner
#19 3022261_linkit_add-better-support-for-linking-to-anchors_18.patch8.94 KBsleitner
#17 3022261-17.linkit.better-support-for-linking-to-anchors.patch15.66 KBsime
#16 3022261-16.linkit.Add-better-support-for-linking-to-anchors.patch14.98 KBsleitner
#15 3022261-9.linkit.Add-better-support-for-linking-to-anchors.patch1.45 KBseutje
#8 3022261-8.linkit.Add-better-support-for-linking-to-anchors.patch6.39 KBjurgenhaas
#5 linkit-better_support_for_anchors-3022261-5.patch6.27 KBckaotik
#4 linkit-better_support_for_anchors-with_2979652-3022261-4.patch6.85 KBckaotik
#4 linkit-better_support_for_anchors-3022261-4.patch6.39 KBckaotik

Issue fork linkit-3022261

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

bkosborne created an issue. See original summary.

bkosborne’s picture

Note that this was requested twice before in the issue queue, but was added a support request instead of a feature request.

ckaotik’s picture

I've been working on a "views display" plugin set, but run into this as well. The problem is, that the Substitution plugin only recieves the entity that is stored in the link attributes, not the path or any other info. I see two ways to work with this:

  1. Add some attributes of the <a> tag as argument to the Substitution plugin's getUrl method, especially href from which the Substitution can extract the fragment, query etc.
  2. Provide all attributes (or the DomElement itself) to the Substitution, and move the entity handling into the Substitution plugin.
    That way, the default canonical plugin could load the entity, get the translation etc. and other substitutions that are not (only) based on entities still have a way to say "I know this, it's a link to XYZ!".

Personally, I'd much prefer option 2. This would also vastly extend what matchers/substitutions can do. Maybe the Suggestion plugins (which already set which substitution plugin should be used) can be used to also specify which attributes should be passed to the Substitution plugin, e.g. the keys used within jsonSerialize

ckaotik’s picture

I've attached a patch that adds the link's attributes for use by the Substitution plugin. Also, the EntityMatcher will now retain fragment and query strings. I've also attached a version that works with #2979652-2: Substitution broken for links to non-file based media.

Is the changed approach I suggested in 2. something that should be considered? If so, it should probably get its own issue.

ckaotik’s picture

Updated the patch for current dev. We should probably also add tests for this new behavior.

Status: Needs review » Needs work

The last submitted patch, 5: linkit-better_support_for_anchors-3022261-5.patch, failed testing. View results

alison’s picture

This functionality would be super great.

What if, instead of query/fragment info being entered into the URL field (with the URL itself), what if they each got their own fields...? I feel like it could be a better UX for content editors.

jurgenhaas’s picture

Status: Needs work » Needs review
StatusFileSize
new6.39 KB

Re-rolled the patch for latest beta version.

sleitner’s picture

works perfect with Linkit 8.x-5.0-beta9

sleitner’s picture

Status: Needs review » Reviewed & tested by the community
anon’s picture

Status: Reviewed & tested by the community » Needs work

as @ckaotik stated "We should probably also add tests for this new behavior."

ckaotik’s picture

Issue tags: +Needs tests
damondt’s picture

Patch in #8 does not apply to 5.0.0-beta9 or 5.0.0-beta10, I'm not sure what version it will apply to.

smustgrave’s picture

The patch here worked for me initially https://www.drupal.org/project/anchor_link/issues/2958532. Would be nice for linkit and anchor link to coordinate a fix for this.

Big change with the patch is that it doesn't work with the linkit buttons but ties linkit into Drupal link.

seutje’s picture

Partial re-roll of patch #8 for 6.0.0-beta2
This only contains the autocomplete part for fragments.

sleitner’s picture

Status: Needs work » Needs review
StatusFileSize
new14.98 KB

Rerolled for current dev with additional test

sime’s picture

Updated version of #16 that fixes a duplicated anchor for me.

Status: Needs review » Needs work

The last submitted patch, 17: 3022261-17.linkit.better-support-for-linking-to-anchors.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

sleitner’s picture

Status: Needs work » Needs review
StatusFileSize
new8.94 KB

Patch for the failing test.

Replaced $parse_url = parse_url($attributes['href']); with $parse_url = parse_url($element->getAttribute('href'));

jurgenhaas’s picture

@sleitner your latest patch is only half the size than the previous one, as if a lot of things were missing?

sleitner’s picture

@jurgenhaas you are right, the new test was untracked.

sleitner’s picture

Fixes some coding standard warnings

jurgenhaas’s picture

Status: Needs review » Reviewed & tested by the community

Looking good on 6.0.0-beta3

steveg.’s picture

Also tested in 8.x-5.x-dev and seems working.

sleitner’s picture

Status: Reviewed & tested by the community » Needs work
sleitner’s picture

StatusFileSize
new15.73 KB

Reroll without changing anything

sleitner’s picture

#22/27 designed for 8.x-5.x test fail because ckeditor does not find the toolbar settings

#26 works with 6.0.x

sleitner’s picture

Version: 8.x-5.x-dev » 6.0.x-dev
Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs tests
StatusFileSize
new15.73 KB
casey’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new17.65 KB

What about passing just query and fragment. This keeps the plugin interface much cleaner.

Status: Needs review » Needs work

The last submitted patch, 29: 3022261-29.patch, failed testing. View results

casey’s picture

Status: Needs work » Needs review
StatusFileSize
new18.47 KB
sleitner’s picture

Status: Needs review » Needs work

Patch does not apply anymore

sleitner’s picture

Version: 6.0.x-dev » 6.1.x-dev
StatusFileSize
new18.45 KB
new18.45 KB
sleitner’s picture

StatusFileSize
new18.64 KB
new18.64 KB
sleitner’s picture

StatusFileSize
new18.6 KB
new18.6 KB
sleitner’s picture

StatusFileSize
new18.46 KB
new18.46 KB
sleitner’s picture

Status: Needs work » Needs review
StatusFileSize
new1.5 KB

Please review #36

sleitner’s picture

Status: Needs review » Needs work

patch does not apply

mrinalini9’s picture

Status: Needs work » Needs review
StatusFileSize
new18.88 KB

Rerolled patch #36, please review it.

Thanks!

sleitner’s picture

Status: Needs review » Reviewed & tested by the community
jurgenhaas’s picture

StatusFileSize
new18.75 KB
new6.7 KB

Rerolled the patch for latest changes.

dbielke1986’s picture

+1 - would be great to get this feature in a stable release :-)

mark_fullmer made their first commit to this issue’s fork.

mark_fullmer’s picture

I've taken the patch in #41 and put it into a merge request to demonstrate passing test coverage against the latest development in Linkit.

mark_fullmer’s picture

Version: 6.1.x-dev » 7.x-dev
boromino’s picture

Status: Reviewed & tested by the community » Needs work
Related issues: +#3443845: When [Enter] key is used to replace a link, the previous link's attributes are not removed

Data attributes are currently removed again when adding a URL fragment manually. It seems that this is caused by https://www.drupal.org/project/linkit/issues/3443845.

Analyzing it further, it turns out that the patch actually works, hence setting it back to reviewed & tested. My issue is caused by a custom NodeMatcher::buildPath(), which does not consider the URL options. Sorry for the confusion.

boromino’s picture

Status: Needs work » Reviewed & tested by the community
Related issues: -#3443845: When [Enter] key is used to replace a link, the previous link's attributes are not removed
frankdesign’s picture

Patch at #41 works perfectly for me with Linkit 6.1.4 and Drupal 10.3

Thanks

mark_fullmer’s picture

Status: Reviewed & tested by the community » Postponed (maintainer needs more info)
StatusFileSize
new5.56 MB

The problem is that when you append the fragment to the path, LinkIt will no longer return the data-entity-type and data-entity-uuid attributes for the tag that it inserts. This means that any substitution plugins (like one that converts an internal node path to the proper path alias) will no longer work.

Testing this today with the 7.x branch, it appears that the data-entity-type and data-entity-uuid attributes are preserved just fine when an anchor/fragment is present:

Screencast of linkit anchor working

Is there another scenario where this is still problematic?

thatguy’s picture

Patch from #41 works and fixes the issue on 6.1.4. I also tested on 7.0.0-alpha1 version and there the issue still remains but on dev-7.x it is fixed and doesn't need patching.

Atleast for our projects, we'll continue using the 6.1.4 version with the patch until issue is fixed in a stable version.

byronveale’s picture

Maybe I have a unique configuration, but the patch in #41 does not work for me.

Drupal 10.3.6
Linkit 6.1.4
CKEditor Anchor Link 3.0.0-beta1

When I go to save the link, either by selecting the matched node or tabbing out of the URL field, the anchor portion of the link (“#foo”) is stripped away.


mark_fullmer’s picture

Status: Postponed (maintainer needs more info) » Needs work
byronveale’s picture

Sorry, please disregard, Linkit wasn’t being patched properly in my local environment. Patch in #41 worked for me too.

byronveale’s picture

Status: Needs work » Reviewed & tested by the community

I believe RTBC is the proper status for this…

mark_fullmer’s picture

Version: 7.x-dev » 6.1.x-dev

  • mark_fullmer committed 94126420 on 6.1.x
    Issue #3022261 by sleitner, ckaotik, jurgenhaas, mrinalini9, anon,...
mark_fullmer’s picture

Status: Reviewed & tested by the community » Fixed

Okay, thanks for the work and the information on which branch this is still needed for.

I also tested on 7.0.0-alpha1 version and there the issue still remains but on dev-7.x it is fixed and doesn't need patching.

I've released a new version of the 7.x branch, 7.0.0-alpha2.

Patch from #41 works and fixes the issue on 6.1.4.

I've merged this into the 6.1.x branch and released 6.1.5.

giordy’s picture

- Drupal 10.3.6
- Linkit 7.0.0-alpha2
- CKEditor Anchor Link 3.0.0-beta1

I have the same problem as #52: when I insert the anchor it is deleted.

berdir’s picture

This issue introduced a breaking change:

  public function getUrl(EntityInterface $entity, array $options = []);

As a result, media_entity_download for example has a fatal error again with this.

This was also proposed in another issue where I had already provided feedback about that.

IMHO this should be reverted, or a follow-up should be done to remove the argument from the interface.

mark_fullmer’s picture

Status: Fixed » Active

IMHO this should be reverted, or a follow-up should be done to remove the argument from the interface.

Re-opening this issue, with the plan to revert the change so that the 6.1.x branch does not introduce BC breaking changes...

  • 49576baf committed on 6.1.x
    Revert "Issue #3022261 by sleitner, ckaotik, jurgenhaas, mrinalini9,...
mark_fullmer’s picture

Status: Active » Needs work

Okay, release 6.1.6, published on 2024-10-17, reverts this change: https://www.drupal.org/project/linkit/releases/6.1.6

Setting this back to "Needs work" for an implementation that avoids BC-breaking changes.

kim.pepper’s picture

I think you can only do this in the 6.1.x to provide a warning that a new param will be added in the next major:

public function getUrl(EntityInterface $entity /*, array $options = [] */);

casey’s picture

Status: Needs work » Needs review

We already updated to 6.1.5 :(

Created a new MR containing the suggested change of kim.pepper

mark_fullmer’s picture

I think you can only do this in the 6.1.x to provide a warning that a new param will be added in the next major:

Based on my testing in comment #50 and confirmation from another in #51, this is effectively resolved in the next major version. The intent in the recent merge here was to provide equivalent functionality for folks who still cannot upgrade to Linkit 7.x due to API changes introduced there.

Thanks @casey for providing the MR that does not include the second parameter to getUrl(). Folks who are interested in this change in 6.1.x, please review!

berdir’s picture

> this is effectively resolved in the next major version

Do we know *why* it was resolved there? 7.x doesn't have $options, so apparently there are other solutions that don't require this.

Even if it's fixed there, the test coverage should be ported to 7.x to verify that there.

This issue and patches here likely predate the change to return URL objects. Just like #3041045: Allow "Linkit URL converter" filter to generate links based on the language of the current page, rather than the language of the referenced entity, where I made the same arguments, why are we passing in $options, set it on the URL object that we then return again? Can't we just set it on the returned URL object (while keeping BC for 6.x where it might not be a URL object yet)

That other issue moved away from this approach too, didn't test it yet, but apparently that worked there too, with a very similar requirement.

mrshowerman made their first commit to this issue’s fork.

mrshowerman’s picture

The current MR produced a fatal error when using the LinkitFormatter:

ValueError: func_get_arg(): Argument #1 ($position) must be less than the number of the arguments passed to the currently executed function in func_get_arg() (line 24 of modules/contrib/linkit/src/Plugin/Linkit/Substitution/Canonical.php).

That's because the formatter does not pass $options, so I quickly added sanity checks to all occurrences of func_get_arg(1).

Apart from that, I agree with @berdir. I don't understand why we are passing the options to the substitution plugin that creates the URL, and still process the fragment and query parameters afterwards. I also think this processing code has a bug (left a comment on the MR).

mark_fullmer’s picture

Assigned: Unassigned » mark_fullmer

Assigning myself to review questions raised in #70 & #72...

alexander tallqvist’s picture

Running into an issue on version 7.0.4 with Drupal 11.1.7 where anchor links don't quite work as I expected. As described in #52, I am able to:

  1. Add a node URL to the link tool, e.g. node/123, and click the found node.
  2. Add the desired anchor, e.g. #test to the URL.
  3. Click the green OK sign -> Everything works..

I am also able to:

  1. Add a node URL with an anochor, e.g. node/123#test
  2. Click the green OK sign twice -> Everything works.

I am however NOT able to:

  1. Add a node URL with an anochor, e.g. node/123#test
  2. Click the first suggested node -> Anchor disappears.

The last option is the one I'm wishing would work, but currently it does not.

boromino’s picture

StatusFileSize
new351.58 KB

I can not get anchor links to work properly in standard installation of Drupal 10.5.2 with only linkit 7.0.8 installed additionally:

Linkit removing data attributes

When adding the url fragment linkit removes the data attributes. It actually is caused by https://www.drupal.org/project/linkit/issues/3443845. Commenting out those lines fixes the issue:

linkFormView.urlInputView.fieldView.element.addEventListener('input', function (evt) {
     //linkit.set('entityType', null);
     //linkit.set('entityUuid', null);
     //linkit.set('entitySubstitution', null);
});

Changing a link to a node to an external url as described in that issue still works correctly, if one clicks on the "No content suggestion found" autocomplete dropdown before clicking on Update.

boromino’s picture

StatusFileSize
new13.96 KB

This patch fixed it for me in 7.0.8.

mark_fullmer’s picture

Version: 6.1.x-dev » 7.1.x-dev
Assigned: mark_fullmer » Unassigned

When adding the url fragment linkit removes the data attributes. It actually is caused by https://www.drupal.org/project/linkit/issues/3443845. Commenting out those lines fixes the issue:

Agreed that this was a consequence of that change. I've updated the MR to clear out external URLs' data attributes later on in the execution, making it compatible with the logic for updating link anchors here.

The latest changes in the MR are compatible with Linkit 7.1.x.

From #74:

I am however NOT able to:

Add a node URL with an anochor, e.g. node/123#test
Click the first suggested node -> Anchor disappears.

With the latest changes, I am able to perform the steps above and have the anchor persist. Please confirm.

This is ready for further review!

mark_fullmer’s picture

Version: 7.1.x-dev » 7.x-dev
mark_fullmer’s picture

Version: 7.x-dev » 7.1.x-dev

mark_fullmer changed the visibility of the branch 8.x-5.x to hidden.

mark_fullmer changed the visibility of the branch 7.1.x to hidden.

mark_fullmer changed the visibility of the branch 3022261-add-better-support to hidden.

mark_fullmer changed the visibility of the branch 3022261-anchor-support-bc to hidden.

boromino’s picture

Left a few comments for MR!74, but the code works including changing to an external url. Above all, clicking twice to insert the anchor is not necessary, anymore.

mark_fullmer’s picture

Version: 7.1.x-dev » 7.x-dev
berdir’s picture

Status: Needs review » Needs work

It doesn't look like #70 was considered so far? This targets 7.x now and adding a parameter to an exist method on an interface is still a breaking change.

There are means to do in a BC way now with commented properties and stuff, but I'm still fairly certain that this is not required to pass $options through because we can apply options to the returned URL object?

Also seems to need a rebase.

alexander tallqvist’s picture

StatusFileSize
new13.84 KB

Re-rolled #76 against 7.0.15.