Jump to content

ICT:Asset-Chain Children Button logic (with boolean field)

From Costa Sano MediaWiki

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_children to 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_asset is EMPTY (Ensures only Top Assets are listed).
  • Gin Styling: Uses button button--primary classes to match the Gin Admin Theme accent colors.
  • Field Order: The field_as_has_children field 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_asset field is hidden from the Edit form if field_as_has_children is 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.


Heritage Project: Asset Hierarchy System

Overview

This system manages a single-level recursive asset hierarchy. It uses a custom boolean field to drive the User Interface (showing buttons) and data integrity (locking the hierarchy).

Data Structure

  • Entity Types: Supports standard node and custom asset entities.
  • Field: Parent Asset (field_as_parent_asset): An Entity Reference field on child assets pointing to their Top Asset.
  • Field: Has Children (field_as_has_children): A boolean field on Top Assets used as a flag for hierarchy state.

Implementation Logic (heritage_tweaks.module)

The module automates the hierarchy through two main PHP hooks.

1. Hierarchy Automation (The Switch)

The heritage_tweaks_entity_presave() function acts as a "detector" during the save process.

  • Trigger: When any asset is saved.
  • Action: If field_as_parent_asset is not empty, the code identifies the parent entity.
  • Result: It programmatically sets the parent's field_as_has_children to 1 (TRUE).
  • Outcome: This "wakes up" the children button in the main list view automatically.

2. Hierarchy Protection (The Safety Rail)

The heritage_tweaks_form_alter() function protects data integrity during editing.

  • Trigger: When an existing asset is opened in the Edit form.
  • Logic: It checks the field_as_has_children value.
  • Constraint: If the value is 1, the field_as_parent_asset field is hidden (#access = FALSE).
  • Outcome: This prevents a Parent from being assigned to another Parent, strictly enforcing the single-level design rule.

Views Configuration

The "Children" button is rendered via a Global: Custom Text field using Twig logic:

{% if field_as_has_children %}
  <a href="/admin/asset-chain/{{ nid }}" class="button button--primary">Children</a>
{% endif %}

User Experience (UX) Guidelines

  • Single-Tab Navigation: The "Children" button opens in the current tab (no _blank target).
  • Gin Styling: Primary actions (Forward) use button--primary. Secondary actions (Back) use button--secondary.
  • Error Correction: Top Assets without children can still be edited to assign a parent if they were created as "Top" by mistake.

Maintenance & Troubleshooting

  • Syncing Existing Data: To update assets created before this module, simply Edit and Save one child record for each Top Asset.
  • Cache: After any module code changes, perform a drush cr to refresh the hook registry.