Rendering a Drupal 8 Link with Anchor and Destination

I’m working on a site that contains several complicated views (many exposed filters). The views render entities using a couple different view modes. These view modes use Display Suite. Within these view modes I’ve leveraged DS custom fields for several needs.
I needed to render a node “Edit” link within some of these view modes. The user would click the edit link, edit the node, then click “Save”. The system would then bring the user to the exact position of the page they were on before, filters and settings intact.
This type of solution would work without Display Suite (leveraging other methods). You’re getting a DS-based example because that’s what the site called for.
In my particular case I needed to separate the anchor from the edit link. If you’re okay having them in the same place you could skip Step 1 and just include the anchor within the markup for the edit link.

Step 1: Create a DS field for the anchor link

Step 2: Create a DS field for the edit link


Step 3: Add the new DS fields to the appropriate view modes

This is a matter of drag-and-drop in the “Manage Display” screens.


Step 4: Test

To test I made sure the “Edit” links were working properly in every context. Some scenarios to test:

  • Do I end up back on the same page within the View I was in?
  • Do I end up at the exact spot on the page (where the anchor is)?
  • Do all of the other exposed filters stay the same when I return to the Views page?

Leave a Reply

Your email address will not be published.