Conversation
|
Size Change: -19 B (0%) Total Size: 7.87 MB 📦 View Changed
ℹ️ View Unchanged
|
defaultRenderingMode value not respected when changed using block_editor_settings_all
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
| const settingsDefaultMode = | ||
| getEditorSettings( state ).defaultRenderingMode; | ||
| const defaultMode = | ||
| defaultModePreference || postTypeDefaultMode || settingsDefaultMode; |
There was a problem hiding this comment.
Maybe I'm reading it wrong (and it's somewhat an existing problem), but could this selector be optimized a little better?
For example, it returns defaultModePreference when set, but only after doing all the work of getting the postTypeDefaultMode and settingsDefaultMode.
Could it have an early return like this right at the top of the function?
const defaultModePreference = select( preferencesStore ).get(
'core',
'renderingModes'
)?.[ theme ]?.[ postType ];
if ( defaultModePreference ) {
return defaultModePreference;
}There was a problem hiding this comment.
Updated to return early after each value that's checked to avoid unnecessary processing.
|
Thanks for the ping. I think I just missed this 😓 At that point, we were already avoiding passing similar settings via filter, in favor of post type configs. Which still should be preferred. The filter still doesn't work at this point, and it requires one more change. Hardcoded value here should be removed (introduced via #62339): Removal should be safe, since it's already defined as store default value. add_filter( 'block_editor_settings_all', function( $settings, $context ) {
$settings['defaultRenderingMode'] = 'template-locked';
return $settings;
}, 20, 2 ); |
Mamaduka
left a comment
There was a problem hiding this comment.
Thanks, @desrosj!
Tested this manually, and the logic works as expected:
- A new default can be provided via block editor settings.
- A post type config can override it.
- User preference always takes precedence.
P.S. You might want to rebase the branch, which should resolve some failing e2e tests - #77857.
|
Flaky tests detected in eaeccd1. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/25203840673
|
The `defaultRenderingMode` editor setting (filterable via `block_editor_settings_all`) is never consulted by `getDefaultRenderingMode`, making it impossible to control the initial rendering mode from PHP. The selector only checks user preferences and post type supports, then fell back to the hardcoded `'post-only'`. This adds `editorSettings.defaultRenderingMode` as a third tier in the fallback chain: user preference, post type supports, settings, `'post-only'`. User-saved preferences (set when clicking the "Show template" toggle) are still respected and take priority.
This adds tests to confirm that `default-mode` is respected when the desired value is set using. `add_post_type_support( 'post_type', 'editor', [ 'default-mode' => 'template-locked' ] )`.
This is defined as a store default value in https://github.com/WordPress/gutenberg/blob/d53f8203eb26716d1b1ee58db429c44a107b9021/packages/editor/src/store/defaults.js#L32. Co-Authored-By: George Mamadashvili <240569+Mamaduka@users.noreply.github.com>
4697dd1 to
eaeccd1
Compare
What?
The
defaultRenderingModeeditor setting (filterable viablock_editor_settings_all) is never consulted bygetDefaultRenderingMode, making it impossible to control the initial rendering mode from PHP.The selector only checks user preferences and post type supports, then fell back to the hardcoded
'post-only'.Why?
There's no way for a plugin, theme, or custom code to control the default behavior. In my case, I was trying to specify
template-lockedas the default for all users on a site as it's easier for them to edit the content when it's shown in the context of the template with the site's full layout.defaultRenderinModeimplies that the value specified will be used as the default, but it's actually ignored (with the exception of the "Go back" snackbar.How?
This adds
editorSettings.defaultRenderingModeas a third tier in the fallback chain: user preference, post type supports, settings,'post-only'.User-saved preferences (set when clicking the "Show template" toggle) are still respected and take priority.
Additional tests are included to cover the scenarios where
add_post_type_support( 'post_type', 'editor', [ 'default-mode' => 'template-locked' ] )was used to register a default mode for a specific post type.Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Sonnet 4.6
Used for: Analyzing the problem and creating the initial draft of the PR.