Add generic field visibility for StreamField StructBlocks#92
Merged
Conversation
Collaborator
Author
|
@tituomin The fingerprinting approach to associate Wagtail generated blocks with client-side JS feels a bit flaky, there might be another way to handle that |
f2ad8fe to
ecc01e7
Compare
12 tasks
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This time using Wagtail's built-in RulesController and telepath. https://docs.wagtail.org/en/v7.2.3/reference/ui/client/classes/controllers_RulesController.RulesController.html
c73b0bd to
356c60a
Compare
tituomin
approved these changes
Jun 4, 2026
7 tasks
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.
Add a generic pattern for showing/ hiding fields within a StructBlock in Wagtail streamfields based on the value of a sibling ChoiceBlock field.
Purpose
When a StructBlock has fields that are only relevant for certain choices (e.g. layout-specific settings), the admin shows all fields unconditionally. This change aims to improve the user experience by only showing necessary and relevant fields.
Solution (summary by Claude)
blocks/conditional_struct_block.py— A ConditionalStructBlock base class. Subclasses declare conditional_rules at class level:Subclasses with non-empty rules are auto-registered via init_subclass. get_conditional_rules_config() serialises all registered rules as a JSON object keyed by block fingerprint — the sorted set of the block's declared field names, which uniquely identifies the block type in the DOM without requiring Wagtail internals changes.
static/kausal_common/js/conditional_struct_block.js — A single generic MutationObserver script that reads window.KAUSAL_CONDITIONAL_RULES and sets up show/hide listeners on any matching .struct-block using data-contentpath selectors. The fingerprint is computed from direct children only to avoid false matches from nested struct-blocks.
wagtail_hooks.py — An insert_editor_js hook that injects the rules config as an inline <script> followed by the JS file. Because kausal_common is in INSTALLED_APPS, the hook is discovered automatically; no registration is needed in consuming apps.
Usage
Extend ConditionalStructBlock instead of blocks.StructBlock and declare conditional_rules.
✅ Pre-Merge Checklist
Type of Change
Testing
Manual testing instructions
If feature requires manual testing by reviewer, you can provide instructions here.Internationalization & Accessibility
Dependencies