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 <[email protected]>
- 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 <[email protected]>
- 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 <[email protected]>
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 <[email protected]>
…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 <[email protected]>
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 <[email protected]> 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 <[email protected]> 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 <[email protected]> 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 <[email protected]> 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 comment was marked as resolved.
This comment was marked as resolved.
…t/ids - Reorder built-in format registration to match expected order: table, json, csv, yaml, count, ids - Skip field validation for count/ids formats as they don't use fields - Prevents "Field not found" warnings when using count format - Fixes formatter.feature test expecting specific format order - Add phpcs:ignore comments for unused $fields parameter in count/ids handlers Agent-Logs-Url: https://github.com/wp-cli/wp-cli/sessions/f411852e-6f8a-4957-bb85-3bc9fbdc2a7f Co-authored-by: swissspidy <[email protected]>
Contributor
Author
... Fixed both issues:
The tests should now pass. All PHPUnit tests for Formatter pass locally, and lint/PHPStan checks are clean. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
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.
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.