Problem/Motivation
Auto (and raw) formatters do not display bundle value for paragraphs.
Steps to reproduce
Add type to paragraph's custom elements display and select any formatter. Check api response of node where this formatter is used. type attribute is empty for anonymous user.
Proposed resolution
Create a custom formatter.
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | custom-elements-3556241-13.patch | 10.15 KB | arthur_lorenz |
Issue fork custom_elements-3556241
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
fagoComment #4
fagosee MR comments
Comment #10
arthur_lorenz commentedReworked the MR based on @fago's review feedback:
1. Moved to main module — formatter is now in custom_elements, not custom_elements_extra_formatters.
2. More generic, but scoped — renamed to BundleTypeCeFieldFormatter (plugin ID entity_bundle_type). Instead of being
restricted to fields named "type", it's now applicable to any entity reference field whose target type is a bundle
config entity (node_type, paragraphs_type, etc.) via isApplicable() + getBundleOf().
3. No entity loading when not needed — reads target_id directly when skip_access_check is enabled.
4. Configurable access check — added skip_access_check boolean setting (default FALSE). When enabled, outputs the
bundle ID without loading the entity or checking access.
Kernel test covers: attribute output, slot output, access check enforcement, and applicability checks.
Comment #11
arthur_lorenz commentedAttached patch file
Comment #12
fagothx! this looks pretty good already. I added some rather minor remarks, but let's make sure we re-use existing helpers. see MR!
Comment #13
arthur_lorenz commentedThx, addressed review feedback in the latest push:
1. Use trait helpers for output: Added CustomElementsFieldFormatterUtilsTrait and replaced manual slot/attribute/flatten logic with $this->setMultipleValue().
2. Defensive isApplicable: Added early return for empty $target_type, and simplified to use $field_definition->getSetting('target_type') instead of going through getFieldStorageDefinition() (since FieldConfigBase::getSetting() already falls back to storage settings).
3. Consolidated tests: Merged 5 test methods into 3 to reduce PHPUnit setup overhead — slot/attribute checks are now combined in testOutputWithSkipAccessCheck, and applicability checks in testIsApplicable.
Comment #14
fagothx, this seems all good now! let's do some more testing before we merge though!
Comment #16
fagoworks well! Merged.