Skip to content

Post Date: Extendable Suggested Formats#62345

Draft
TylerB24890 wants to merge 5 commits intoWordPress:trunkfrom
TylerB24890:feature/extendable-date-formats-55638
Draft

Post Date: Extendable Suggested Formats#62345
TylerB24890 wants to merge 5 commits intoWordPress:trunkfrom
TylerB24890:feature/extendable-date-formats-55638

Conversation

@TylerB24890
Copy link
Copy Markdown
Contributor

What?

This PR adds a date.formatOptions property to the __experimentalFeatures object in the editor settings. The formatOptions value is then used as the suggested date formats in the core/post-date block.

NOTE: If __experimentalFeatures is not the correct key this kind of setting should live under please let me know. This might be better off living in the date settings from @wordpress/date

Why?

Related Issue: #55638

This PR makes the hard-coded suggested date formats extendable so extenders can choose which date formats are allowed for selecting. If a unique date format is required, extenders can add it to this setting and have it selectable instead of requiring editors to use the Custom Format option every time.

How?

By applying the date.formatOptions to the Block Editor Settings, extenders can use the block_editor_settings_all filter to change the suggested date formats available to editors.

Testing Instructions

  1. Create a new Post with the block editor.
  2. Add the core/post-date block
  3. Note the available formats in the block sidebar.
  4. In functions.php (or similar) add the following:
add_filter(
    'block_editor_settings_all',
    function ( $settings ) {
        $settings['__experimentalFeatures']['date']['formatOptions'] = [
            'M j',
            'F j, Y'
        ];

        return $settings;
    }
);
  1. Refresh the block editor & confirm the core/post-date block Format Options are reduced to only the two defined (plus the "Custom" option).
    • If you used the same Date formats defined above, you should see the following format options available:
      • Jan 25
      • January 25, 2022

_x( 'M j', 'short date format without the year' ),
] ),
];
const editorSettings = select( blockEditorStore ).getSettings();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TylerB24890 can you refactor this to use useSelect instead? There are some architectural differences and the usage of select itself is discouraged

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All set.

@fabiankaegy fabiankaegy added [Type] Enhancement A suggestion for improvement. [Feature] Extensibility The ability to extend blocks or the editing experience [Block] Post Date Affects the Post Date Block labels Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Post Date Affects the Post Date Block [Feature] Extensibility The ability to extend blocks or the editing experience [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants