Jump to content

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

From Costa Sano MediaWiki
Created page with "== 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:''' <code>field_as_parent_asset</code> (Entity Reference pointing to the parent). * '''Indicator Field:''' <code>field_as_h..."
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 37: Line 37:
* '''Adding Children:''' The button appears automatically the moment the first child is saved.
* '''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.
* '''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 <code>field_as_parent_asset</code> field is hidden from the Edit form if <code>field_as_has_children</code> 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 <code>node</code> and custom <code>asset</code> entities.
* '''Field: Parent Asset''' (<code>field_as_parent_asset</code>): An Entity Reference field on child assets pointing to their Top Asset.
* '''Field: Has Children''' (<code>field_as_has_children</code>): 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 <code>heritage_tweaks_entity_presave()</code> function acts as a "detector" during the save process.
* '''Trigger:''' When any asset is saved.
* '''Action:''' If <code>field_as_parent_asset</code> is not empty, the code identifies the parent entity.
* '''Result:''' It programmatically sets the parent's <code>field_as_has_children</code> to '''1 (TRUE)'''.
* '''Outcome:''' This "wakes up" the children button in the main list view automatically.
==== 2. Hierarchy Protection (The Safety Rail) ====
The <code>heritage_tweaks_form_alter()</code> function protects data integrity during editing.
* '''Trigger:''' When an existing asset is opened in the Edit form.
* '''Logic:''' It checks the <code>field_as_has_children</code> value.
* '''Constraint:''' If the value is '''1''', the <code>field_as_parent_asset</code> field is hidden (<code>#access = FALSE</code>).
* '''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:
<syntaxhighlight lang="twig">
{% if field_as_has_children %}
  <a href="/admin/asset-chain/{{ nid }}" class="button button--primary">Children</a>
{% endif %}
</syntaxhighlight>
=== User Experience (UX) Guidelines ===
* '''Single-Tab Navigation:''' The "Children" button opens in the current tab (no <code>_blank</code> target).
* '''Gin Styling:''' Primary actions (Forward) use <code>button--primary</code>. Secondary actions (Back) use <code>button--secondary</code>.
* '''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 <code>drush cr</code> to refresh the hook registry.

Latest revision as of 16:44, 30 March 2026

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.