Comments (5)
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.
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.
@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.
@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.
@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)
- [4.x]: Date Field isn't filtering >= now correctly HOT 1
- [5.x]: GraphQL - Setting unknown property: craft\elements\db\EntryQuery::volumeId HOT 1
- [5.x]: Adding Two-Step Verification with a Permissions-Policy that does not allow synchronous requests HOT 3
- [5.x]: Two-step verification set-up key has extra whitespace at the end when copied HOT 2
- [5.x] / [4.x]: CP Slow with two or more users logged in HOT 5
- [5.x]: Modified matrix entries don't get updated after adding or deleting an entry to the matrix (Copy of #15069) HOT 2
- [5.x]: Error in CraftCMS: prepareForJsonColumn() Argument Type Mismatch in FindAndReplace.php HOT 1
- [4.x]: Migration throws error relating to UserGroupEvent when removing a user group HOT 5
- [4.x]: Links in release note alerts not rendered HOT 1
- [5.x]: Migration `m230511_215903_content_refactor` fails HOT 3
- Revisions for singles - Remove other sites from the author view (multisite) HOT 6
- [4.x]: error: Undefined array key "label" HOT 1
- Response Headers Ignoring Header Tags [4.x]: HOT 6
- [5.x]: Pagination info on overview page not updating after bulk editing with filter applied HOT 2
- [5.x]: Setting field values in Entry::EVENT_BEFORE_SAVE doesn't work when $firstSave is true
- [5.x]: Custom field conditions in element queries are case-sensitive now HOT 1
- [5.x]: Variant Overview has table fields that doesn't exist on the variant layout HOT 3
- [5.x]: Incorrect CP console request URLs HOT 3
- [5.x]: Matrix blocks can't be reordered HOT 1
- [5.x]: web-auth/webauthn-lib 4.8.x has security advisory, conflicts with roave/security-advisories
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cms.