Skip to content

[5.x]: Setting isValid to false in Structures::EVENT_BEFORE_MOVE_ELEMENT causes Server Error #15292

@rogerdawkins

Description

@rogerdawkins

What happened?

Description

I would like to control Moves and Inserts in a Structure so users cannot add new Level 1 elements but can add and move elements at Level 2. New entries will be added to Level 2 with a specific parent.

I've generated a simple module with the Craft generator which works fine as a bare module. I have then added a Structures::EVENT_BEFORE_MOVE_ELEMENT event and as a very simple test I have set $event->isValid to false. This works and stops any moves in the Structure. However, at that point it also displays 'A server error occurred.' message. I also tried adding $event->handled = true but that didn't help.

I'm running locally with DDEV and have restarted that. I've looked in the logs but no reference to the server error.

Steps to reproduce

  1. Generate empty module using the Craft Generator - craft make module
  2. The new module includes an attachEventHandlers method. Inside that I added:
Event::on(
    Structures::class,
    Structures::EVENT_BEFORE_MOVE_ELEMENT,
    function (MoveElementEvent $event) {
        // stop the move happening
        $event->isValid = false;        
    }
);
  1. Move an element in a structure

Expected behavior

Stop the Move happening without a Server Error message.

Actual behavior

Stops the move but displays the Server Error message.

Craft CMS version

5.2.5

PHP version

8.2.20

Operating system and version

Linux 6.5.0-42-generic

Database type and version

MySQL 8.0.36

Image driver and version

Imagick 3.7.0 (ImageMagick 6.9.11-60)

Installed plugins and versions

  • CKEditor 4.x-dev
  • Control Panel CSS v3.x-dev
  • Feed Me 6.x-dev
  • PDF Transform 5.0.1
  • Sprig dev-develop
  • Vite v5.x-dev

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions