Edit link on node teasers¶
Version v2
You can apply this model as a recipe (Drupal 10.3 or later) to your own Drupal site:
1 2 3 4 5 6 7 8 9 10 11 | |
Purpose¶
Editors maintain a lot of content through teaser lists. This model puts the edit
screen one click away: whenever a node is rendered in the teaser view mode, an
edit link is added to the rendered output for users who are allowed to edit that
node. The link points to the node's edit form.
How it works¶
- The
eca_render:entityevent fires whenever a node is rendered, and provides the render array and the active view mode. - A scalar condition checks that
[event:mode]equalsteaser. Every other view mode stops the model here, so full pages, search results and entity references render unchanged. - The
eca_render_cacheabilityaction adds theuser.permissionscache context. This runs before the access check, so both outcomes — link and no link — are cached against the same context. - An access condition (
eca_entity_is_accessible,update) lets only users who may actually edit the node through to the final step. - The
eca_render_linkaction adds the link, using[node:edit-url]for the target andEdit [node:title]as the link text.
Points worth copying¶
- The render event fires for every node on the site, so the view-mode condition is what keeps the change teaser-only.
- Anything user-dependent in a render array needs cache metadata. Node teasers
are render-cached, so without the
user.permissionscache context the first variant rendered would be served to everyone — an editor's link shown to anonymous visitors, or editors never seeing it at all. - Add the cache context before the access check, not inside the allowed branch, so the negative outcome carries it too.
- Access is checked explicitly. A render-time addition does not inherit any access check, so without the condition every visitor would see a link that returns 403 when followed.
- The link text is
Edit [node:title]rather than an icon, so it has an accessible name that says which node it edits. - The model only alters the in-memory render array, so no node is ever saved and the model cannot recurse.
Requirements¶
This recipe installs the eca, eca_base, eca_content, eca_render and
modeler_api modules. It works with any node content type and needs no
particular field.
Note that the link is added as its own element in the rendered teaser, next to
the title rather than inside the title's own link — nesting a link inside the
title's <a> would produce invalid markup.
Dependencies¶
- module
- eca_base
- eca_content
- eca_render
- modeler_api
Used plugins¶
Events¶
Conditions¶
Actions¶
Changelog¶
Replaces the raw markup appended to the node title with an access-checked, cache-aware render link.