Make Formatter extensible with custom format handlers#6238
Open
Make Formatter extensible with custom format handlers#6238
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
- Add static $custom_formatters array to track custom format handlers - Add Formatter::add_format() method to register custom formatters - Add Formatter::get_available_formats() method with filterable format list - Update format() and show_multiple_fields() to support custom handlers - Add PHPUnit tests for custom format functionality - Add Behat feature tests for custom format scenarios Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
- Update Formatter class docblock to mention extensibility - Add detailed examples to add_format() method - Add usage examples to get_available_formats() method - Document filter hook 'formatter_available_formats' Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
- Add BUILTIN_FORMATS constant for better maintainability - Add debug message for skipped items in custom formatters - Improve test coverage for parameter passing and field filtering - Add type assertions to satisfy PHPStan Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Implement extensible format options for commands
Make Formatter extensible with custom format handlers
Feb 12, 2026
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…ix field resolution for custom formatters - Fix handler signatures in tests to accept ($items, $fields) parameters - Remove restriction preventing override of built-in formats - Add field validation and prefix resolution for custom formatters - Check custom formatters before built-in switch statement to enable overriding - Add test for prefix field resolution with custom formatters - Add test for overriding built-in formats Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
- Add static flag to track built-in format registration - Create register_builtin_formats() method to register all built-in formats using add_format() - Register count, ids, json, yaml, csv, and table formats via the API - Remove switch statements from format() and show_multiple_fields() methods - All built-in formats now use the same registration mechanism as custom formats - Built-in formats can be overridden by calling add_format() with the same name - Special handling for ids/count formats (work with raw items) and table format (needs formatter instance) Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Agent-Logs-Url: https://github.com/wp-cli/wp-cli/sessions/e94ee5b4-3181-4bf6-b2d2-3ba9a27776c6
This comment was marked as resolved.
This comment was marked as resolved.
swissspidy
reviewed
Mar 23, 2026
- Remove BUILTIN_FORMATS constant, use array_keys($custom_formatters) instead - Remove defensive type check for hook return value (trust the hook) - Create InitializeFormatter bootstrap step to register built-in formats - Move format registration from lazy initialization to bootstrap - Remove special handling for json/yaml in show_multiple_fields - Make register_builtin_formats() public and remove flag check - Add setUpBeforeClass to FormatterTest for test initialization Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Agent-Logs-Url: https://github.com/wp-cli/wp-cli/sessions/44e4d3bc-205c-4f5f-b5f4-892faf288cf9
This comment was marked as resolved.
This comment was marked as resolved.
Fixes test failures where single-item display was incorrectly wrapping items in an array. WP_CLI::print_value() properly handles single items by outputting them without array wrappers, which is required for json/yaml formats in single-item mode (display_item). Multi-item display continues to use registered format handlers directly. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Agent-Logs-Url: https://github.com/wp-cli/wp-cli/sessions/747ee921-fbd9-4706-97a9-551bbfc7849b
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
- Add optional context parameter to json/yaml format handlers - Handlers check for 'single_item' flag to output without array wrapper - Remove WP_CLI::print_value() usage in show_multiple_fields - Pass context flag when calling formatters for single-item display - Maintains backward compatibility for custom format handlers Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Agent-Logs-Url: https://github.com/wp-cli/wp-cli/sessions/2e99076d-2ca2-4d77-a5a8-eb3b535c9c25
This comment was marked as resolved.
This comment was marked as resolved.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Commands providing
--formatoutputs lacked a standardized extension mechanism. Extensions couldn't add custom formats (e.g., XML, Nagios-compatible output) without modifying core.Changes
Format Registration API
Formatter::add_format($name, $handler)for registering custom format handlers($items, $fields, $context)and output directly$custom_formattersarrayBuilt-in Formats Refactored
add_format()APIInitializeFormatterbootstrap step during startupFormat Discovery
Formatter::get_available_formats()returning all registered formatsformatter_available_formatshook for filtering the format listSingle-Item Display Support
$contextfor contextual information$context['single_item']flag to output without array wrappers{...}instead of[{...}]) through unified APIFormat Resolution
format()andshow_multiple_fields()to use registered format handlers exclusivelyvalidate_fields()Example
All built-in formats (table, json, csv, yaml, count, ids) work as before and can be overridden by registering a handler with the same name.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.