drupal-eca-recipe / eca_lib_0039
Add an edit icon to node titles in teasers
Package info
gitlab.lakedrops.com/drupal/recipes/eca_lib_0039/
Type:drupal-recipe
pkg:composer/drupal-eca-recipe/eca_lib_0039
Requires
- drupal/eca: *
- drupal/modeler_api: *
README
ID: eca_lib_0039
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.
Installation
## Import recipe
composer require drupal-eca-recipe/eca_lib_0039
# Apply recipe with Drush (requires version 13 or later):
drush recipe ../recipes/eca_lib_0039
# Apply recipe without Drush:
cd web && php core/scripts/drupal recipe ../recipes/eca_lib_0039
# Rebuilding caches is optional, sometimes required:
drush cr