I want to add an "Edit entry" button on each page for admin users. Am I overlooking a simple way to output the Edit link to the CP edit page for the current entry? Curious if there is something like {{ entry.editLink }}
2 Answers
There is! It's called cpEditUrl.
{% if currentUser %}
{% if currentUser.isInGroup('admin') %}
<a href="{{ entry.cpEditUrl }}">Edit entry</a>
{% endif %}
{% endif %}
Or shorter (thanks to MISC):
{% if currentUser.admin %}
<a href="{{ entry.cpEditUrl }}">Edit entry</a>
{% endif %}
Or forget user group altogether (thanks to Phil Gyford and bennobo):
{% if entry.isEditable() %}
<a href="{{ entry.cpEditUrl }}">Edit entry</a>
{% endif %}
-
-
3Alternatively, you can use
{% if entry.isEditable() %}
as in this answer: craftcms.stackexchange.com/questions/130/… Commented Jul 9, 2015 at 12:56 -
2
-
1I think that
{% if entry.isEditable() %}
is the way to go as it totally forgets the user/group logic. It's just one simple question: Can you edit?– bennoboCommented May 25, 2016 at 22:30
Here is Craft CMS 3 solution:
http://craftsnippets.com/articles/quick-edit
It works both with entries and categories and it takes multiple things into account while deciding if edit link should be displayed:
- If the current user has access rights to control panel?
- If the current user has rights to edit current entry/category?
- If entry/category is not displayed through live preview?