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

Image migration logic causes editor to break when editing image attributes dynamically #56055

Closed
artemiomorales opened this issue Nov 11, 2023 · 1 comment · Fixed by #56057
Closed
Labels
[Block] Image Affects the Image Block [Type] Bug An existing feature does not function as intended

Comments

@artemiomorales
Copy link
Contributor

artemiomorales commented Nov 11, 2023

Description

When editing image attributes dynamically via PHP, the image's block may become invalid, causing the migration logic to run in the image block's deprecated.js.

We should add a check to make sure the required behaviors and behaviors.lightbox values exist in the attributes object during the migration to prevent this breakage.

Alternatively, we can review and revise the logic for determining whether a migration should run.

Please see this forum post for more details.

Step-by-step reproduction instructions

  1. Add the following code to your theme's functions.php to modify one of the image block's attributes:
add_filter( 'block_type_metadata', 'set_image_auto_centre' );

function set_image_auto_centre( $metadata ) {
  if ( 'core/image' !== $metadata['name'] ) {
    return $metadata;
  }
  $metadata['attributes']['align']['default'] = 'center';

  return $metadata;
}
  1. Open the WordPress admin and try adding a new post.
  2. See that the editor breaks.

Screenshots, screen recording, code snippet

Screenshot 2023-11-11 at 5 22 52 PM

Environment info

  • WordPress 6.4 with Gutenberg

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@artemiomorales artemiomorales added [Type] Bug An existing feature does not function as intended [Block] Image Affects the Image Block labels Nov 11, 2023
@artemiomorales artemiomorales changed the title Image lightbox: Migration logic causes editor to break when editing image attributes dynamically Image migration logic causes editor to break when editing image attributes dynamically Nov 11, 2023
@artemiomorales
Copy link
Contributor Author

artemiomorales commented Nov 11, 2023

I've created a potential fix for this.

I just add a check in the migration logic to make sure the values we expect exist. That seems to be most straightforward fix for now. We could also consider whether a deeper fix is necessary.

As of now, if one edits the attributes of any blocks, causing a change in the block’s markup, the migration logic will run — see this function where non-matching class names as a result of modifying the image alignment were causing the block validation to fail. Note: The reason this appears to break when attempting to add a new post is that the migration is run for patterns that contain the modified blocks.

On one hand, we could expect developers adding new deprecations to add a check to see if attributes exist before applying any changes. If taking this approach, it could be beneficial to add a note to the docs reminding developers to do that, as they may not expect users to modify block attributes in their functions.php and cause block invalidation.

On the other hand, we could devise additional handling for this seemingly common use case of folks modifying block attributes dynamically — see the linked forum thread for details.

Any thoughts appreciated! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Image Affects the Image Block [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant