I have a headline global.

I am trying to see if headline is editable by using this code block, but it is not working.

Can I use isEditable() with my headline global?

{% if headline.isEditable() %}
    <div class="admin-edit-button-wrapper">
        <a href="{{ headline.cpEditUrl }}" class="admin-edit-button">Edit Headline</a>
{% endif %}

isEditable() would be a method of the content model, not of a field. Unfortunately, this is currently only useful for EntryModel objects; It is not presently implemented for GlobalSetModel objects, so using .isEditable on a global set will always return false.

However, you could use the .can() method on a user, and check for the permission 'editglobalset:X' (where X is the ID of your Global Set element).

For example — assuming header is a GlobalSetObject you have fetched elsewhere in your template):

{% if currentUser and currentUser.can('editglobalset:' ~ header.id) %}
{% endif %}
  • Good call on the currentUser.can approach – definitely easier than creating a custom plugin! Nov 2 '15 at 20:32

No, the isEditable property isn't supported for Global Sets.

The confusing thing is that the GlobalSetModel class does actually have an isEditable property, however it will always return false. The reason is that this property is inherited from the BaseElementModel class, where it will return false unless the element type overrides it to (potentially) return something else. An example of the latter is the EntryModel class, which also inherits BaseElementModel but overrides isEditable to test if the current user is allowed to edit the entry.

It'd be interesting to hear P&T's reasoning for not "enabling" isEditable for Global Sets, although to be honest – needing to give users the ability to edit global variables from the frontend sounds like an edge case at best, and probably not a very good idea.

On that note, my best advice for a workaround would be to simply use a Single instead of a Global Set.

However, if you need/want to use a Global Set, you can easily work around the issue by creating a custom plugin exposing a isGlobalSetEditable template variable method. Something like this should work:

public function isGlobalSetEditable(GlobalSetModel $globalSet)
    return craft()->userSession->checkPermission('editGlobalSet:'.$globalSet->id);

In your template, you could then do something like this:

{% if craft.myPlugin.isGlobalSetEditable(headline) %}
    <a href="{{ headline.cpEditUrl }}">Edit me</a>
{% endif %}

If you're not familiar with plugin development, I'd advice you to check out the official docs on the subject, and to download the excellent Business Logic Plugin as a starting point – it even has a Variable class where the above isGlobalSetEditable method could be dropped right in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.