ICT:Asset-Chain Children Button logic (with boolean field)
Appearance
Asset Hierarchy: "Children" Button Logic
Overview
This implementation displays a clickable button in the Top Assets table only when an asset has at least one child. It avoids complex and performance-heavy Views relationships by using a data-driven boolean flag.
Data Model
- Entity Type: Asset (or Node)
- Parent Field:
field_as_parent_asset(Entity Reference pointing to the parent). - Indicator Field:
field_as_has_children(Boolean).- Default: Off (0)
- Logic: Marks if a Top Asset is a parent.
Automation (The Tweak Module)
The logic is handled automatically by the Heritage Tweaks module via hook_entity_presave.
- Trigger: When a child asset is saved.
- Action: The code identifies the parent referenced in
field_as_parent_asset. - Result: It programmatically flips the parent's
field_as_has_childrento True (1).
Views Configuration
The button is rendered using a Global: Custom Text field in the Assets View.
Logic
The button uses Twig to check the boolean state before rendering:
{% if field_as_has_children %}
<a href="/admin/asset-chain/{{ nid }}" class="button button--primary">Children</a>
{% endif %}
Key Settings
- Filters:
field_as_parent_assetis EMPTY (Ensures only Top Assets are listed). - Gin Styling: Uses
button button--primaryclasses to match the Gin Admin Theme accent colors. - Field Order: The
field_as_has_childrenfield must be positioned above the Custom Text field and set to "Exclude from display."
Maintenance
- Adding Children: The button appears automatically the moment the first child is saved.
- Existing Data: To "wake up" a button for a Top Asset that already has children, simply Edit and Save one of its child records.
Hierarchy Integrity Rule
To enforce the "Single Level" hierarchy design:
- Logic: The
field_as_parent_assetfield is hidden from the Edit form iffield_as_has_childrenis TRUE. - Result: Prevents a Parent from being assigned a Parent (Nested recursion).
- Exception: Top Assets without children can still be edited to assign a parent, allowing for data entry error correction.