5

I need to create a plugin that prevents select user groups from reordering a Structure content type in the Entries listing. I could quickly do this by just hiding the a.move.icon element, but I worry about the longevity of this solution. For example, if this class were to change then the plugin wouldn't hide the element any longer.

Is there a better, more long-term fix that someone might recommend here?

4

You should be able to ensure that the elements aren't moved by using the structures.onBeforeMoveElement hook in a plugin:

public function init()
{
    craft()->on('structures.beforeMoveElement', function(Event $event)
    {
        $user = craft()->userSession->getUser();

        if (!$user->isInGroup('allowedGroup'))
        {
            $event->performAction = false;
        }
    });
}

I've had a quick look through the source but I'm not 100% sure if this is also run when an element's parent is changed. I would think it is.


You could also create your own custom user permission that can be assigned to individual users or multiple groups for more flexibility:

public function init()
{
    craft()->on('structures.beforeMoveElement', function(Event $event)
    {
        $user = craft()->userSession->getUser();

        if (!$user->can('pluginName-moveElements'))
        {
            $event->performAction = false;
        }
    });
}

public function registerUserPermissions()
{
    return array(
        'pluginName-moveElements' => array('label' => Craft::t('Move Elements in Structures')),
    );
 }
  • Thanks so much, Mike! I'll look into this option as it seems a bit more official and less of a hack. – Aaron Bushnell Nov 16 '16 at 21:07
3

Impossible out of the box – there's no permission setting for ordering Structures. For now, I'd add a feature request and hack it in.

The easiest solution is probably to use the CP Body Classes and CP CSS plugins to hide the a.move.icon element (like you say) based on the current user's user group affiliation(s).

Personally I wouldn't worry too much about the longevity of such a hack; if it ever breaks (due to future DOM structure changes in the CP) it's probably not a huge problem, beyond the fact that your user group will be able to reorder Structures while you find another solution. Also, with a little luck your feature request gets implemented sooner or later.

  • Thanks a lot! You make a good point going for the easier solution since it's a quick fix and it might be a while before there are breaking changes to the DOM. I'll probably take the more official option as Mike Pepper suggested, but this is a good alternative, too. – Aaron Bushnell Nov 16 '16 at 21:08
  • Yeah, Mike's answer should be the correct one – you should probably hide the move icon either way, but as long as you're writing your own plugin it makes sense to do it without CP CSS and CP Body Classes :) – Mats Mikkel Rummelhoff Nov 16 '16 at 21:10
  • Has any thing happened with this? Did Craft add this functionality? – mateostabio Mar 25 at 14:23

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.