Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add targetElementRecord to before/after move element in structure function and events #13409

Closed

Conversation

engram-design
Copy link
Contributor

I have a use case where my element needs validation rules when moving it within a structure. See verbb/navigation#359 where I want to enforce user-defined rules on how many nodes for a particular level there can be.

Fortunately, I can use the Element::beforeMoveInStructure() function (or the Element::EVENT_BEFORE_MOVE_IN_STRUCTURE event if I wanted) to trigger validation with element->validate(). However, one glaring issue is that I cannot get the to-be-moved element information from the function or event, because it's not passed to it.

For example, if I want validation rules to check if something can be placed at level 2 if something is currently at level 1, there's no way for me to know what the new level will be, until after the fact. I only know about the level of it currently.

This information can be grabbed from $targetElementRecord.

Happy to revise this, or let me know if I've gone about this the wrong way.

@brandonkelly
Copy link
Member

A breaking ElementInterface method signature change like this would need to wait until 5.0.

Adding additional properties to the MoveElementEvent class would be OK though (used by the EVENT_BEFORE_MOVE_ELEMENT and EVENT_AFTER_MOVE_ELEMENT events on the Structures service, where all the target element info is already known). So I’ve just added $action, $targetElementId, and getTargetElement() to that, for Craft 4.5 (#13429).

Also decided to deprecate Element::beforeMoveInStructure() / afterMoveInStructure() and the events they trigger, since they’re a little inconsistent with the service events (they get called/triggered for moves and inserts), and now the service events are more capable, so we should be steering people toward those.

@brandonkelly
Copy link
Member

Craft 4.5.0 is out with those new events.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants