Giter Site home page Giter Site logo

Comments (5)

brandonkelly avatar brandonkelly commented on August 24, 2024 1

Just made a change for Craft 4.11 and 5.3 that makes it possible to provide a custom message, by throwing an exception rather than setting $event->isValid = false.

use yii\base\UserException;

Event::on(
    Structures::class,
    Structures::EVENT_BEFORE_MOVE_ELEMENT,
    function (MoveElementEvent $event) {
        // stop the move happening
        throw new UserException('This move isn’t allowed.');      
    }
);

Note that the exception must be an instance/subclass of yii\base\UserException for the message to be shown when Craft isn’t running in Dev Mode.

Thinking about this some more. When you try to drag an entry to a illegal position, such as level 3 of a structure with a max of 2 levels, the system will block it silently, with no Server Error message. So that's what I'm trying to achieve.

In that case, the drop target isn’t available to begin with. So there’s no actual move operation that was ever attempted, as far as the back end knows.

from cms.

brandonkelly avatar brandonkelly commented on August 24, 2024

The “server error” message is a bit misleading. All the JS code knows is that the server didn’t accept the new location, so it shows that message and reloads the elements so they revert to their actual positions. That could be due to an actual error that gets logged, but in your case it’s just the result of you setting $event->isValid = false.

from cms.

rogerdawkins avatar rogerdawkins commented on August 24, 2024

@brandonkelly OK, that's good to know that I'm not doing anything wrong. Can I replace the 'server error' message with something more meaningful for my editors like 'Move not allowed'. I tried adding a setNotice but it doesn't display until the page reloads again.

from cms.

rogerdawkins avatar rogerdawkins commented on August 24, 2024

@brandonkelly Thinking about this some more. When you try to drag an entry to a illegal position, such as level 3 of a structure with a max of 2 levels, the system will block it silently, with no Server Error message. So that's what I'm trying to achieve. Effectively setting isValid to false I think should replicate an attempted illegal move. Do you think that could be possible?

from cms.

rogerdawkins avatar rogerdawkins commented on August 24, 2024

@brandonkelly Nice one! That makes it much more friendly, trying to tell users to ignore a 'server error' was going to be a hard sell.

from cms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.