8

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 }}

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
taylor
  • 1,128
  • 1
  • 9
  • 19

2 Answers2

23

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 %}
Douglas McDonald
  • 13,457
  • 24
  • 57
  • You are the man, Douglas! – taylor Feb 10 '15 at 23:33
  • 3
    Alternatively, you can use {% if entry.isEditable() %} as in this answer: http://craftcms.stackexchange.com/questions/130/how-can-i-grant-front-end-access-to-an-entry-that-belongs-to-the-current-user – Phil Gyford Jul 09 '15 at 12:56
  • 2
    Or even shorter: {% if currentUser.admin %} – MISC Aug 02 '15 at 06:50
  • 1
    I 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? – bennobo May 25 '16 at 22:30
1

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?
Piotr Pogorzelski
  • 1,286
  • 7
  • 18