Skip to content

[6.x] Added three new lifecylce hooks (transform query, actions and rows)#2095

Open
johannesMEGABAD wants to merge 5 commits intoPower-Components:6.xfrom
johannesMEGABAD:6.x
Open

[6.x] Added three new lifecylce hooks (transform query, actions and rows)#2095
johannesMEGABAD wants to merge 5 commits intoPower-Components:6.xfrom
johannesMEGABAD:6.x

Conversation

@johannesMEGABAD
Copy link
Copy Markdown
Contributor

@johannesMEGABAD johannesMEGABAD commented Mar 27, 2026

⚡ PowerGrid - Pull Request

  • Bug fix
  • Enhancement
  • New feature
  • Breaking change

Description

This PR introduces three new lifecycle hooks that allow users to modify dataat key stages of the PowerGrid pipeline.

New Hooks

All hooks are defined in the Hooks trait (src/Concerns/Hooks.php) with no-op defaults, making them fully opt-in.

transformQuery(mixed $query): mixed

Modify the query after filters, search, and sorting are applied but before pagination executes it. Useful for eager loading, subqueries, withCount(), or any query-level adjustments that depend on the current filter state.

Applies to ModelProcessor and ScoutBuilderProcessor.

public function transformQuery(mixed $query): mixed
{
    return $query
        ->withCount('comments')
        ->addSelect(DB::raw('(SELECT COUNT(*) FROM reviews WHERE reviews.dish_id = dishes.id) as review_count'));
}

transformRows(Collection $rows): Collection

Modify the paginated rows after the query has executed and data has been transformed, but before it is passed to the view. Runs on the current page's rows only. Ideal for enriching rows with data from external APIs or computed values not available at query time.

public function transformRows(Collection $rows): Collection
{
    $ids = $rows->pluck('id')->toArray();
    $apiData = ExternalApi::getByIds($ids);

    return $rows->map(function ($row) use ($apiData) {
        $row->external_score = $apiData[$row->id] ?? null;

        return $row;
    });
}

transformActions(array $actionsByRow, Collection $rows): array

Modify the resolved actions array before it is dispatched to the frontend via JS. Receives both the $actionsByRow dictionary (keyed by primary key) and the current $rows collection. Useful for dynamically changing button labels, visibility, or attributes based on data not available during actions() resolution.

Each action in the array has these modifiable keys: action, can, slot, tag, icon, iconAttributes, attributes, rules.

public function transformActions(array $actionsByRow, Collection $rows): array
{
    $ids = $rows->pluck('id')->toArray();
    $counts = NotificationService::getCounts($ids);

    foreach ($actionsByRow as $rowId => &$actions) {
        foreach ($actions as &$action) {
            if ($action['action'] === 'notifications') {
                $count = $counts[$rowId] ?? 0;
                $action['slot'] = "Notifications <span class=\"badge\">{$count}</span>";
            }
        }
    }

    return $actionsByRow;
}

Pipeline Position

datasource()
    │
    ▼
Filters / Search / Sorting
    │
    ▼
★ transformQuery()          <-- modify the builder before it hits the DB
    │
    ▼
Pagination
    │
    ▼
Data Transformation
    │
    ▼
★ transformActions()        <-- modify resolved actions before JS dispatch
    │
    ▼
★ transformRows()           <-- enrich/modify rows before the view
    │
    ▼
Render

Files Changed

  • src/Concerns/Hooks.php - Added transformQuery, transformRows, and transformActions hook definitions
  • src/PowerGridComponent.php - Integrated transformRows and transformActions into getRecordsFromCache() and getRecordsDataSource(); added applyAfterQuery() helper
  • src/DataSource/Processors/ModelProcessor.php - Integrated transformQuery between filter/sort pipeline and pagination
  • src/DataSource/Processors/ScoutBuilderProcessor.php - Same transformQuery integration

Tests

  • tests/Concerns/Components/DishesTransformHooksTable.php - Test component with toggleable flags for each hook
  • tests/Feature/TransformHooksTest.php - 12 test cases (4 scenarios x 3 themes: Tailwind, Bootstrap, DaisyUI)
Test Verifies
transformRows modifies row data before rendering Disabled: no custom data visible. Enabled: rows enriched with computed field
transformQuery filters the query before pagination Disabled: all dishes visible. Enabled: only in-stock dishes returned
transformActions modifies action attributes before rendering Action slots are dynamically changed per row
transformRows and transformQuery can be combined Both hooks compose correctly when used together
vendor/bin/pest tests/Feature/TransformHooksTest.php

  PASS  Tests\Feature\TransformHooksTest
  ✓ transformRows modifies row data before rendering (tailwind, bootstrap, daisyui)
  ✓ transformQuery filters the query before pagination (tailwind, bootstrap, daisyui)
  ✓ transformActions modifies action attributes before rendering (tailwind, bootstrap, daisyui)
  ✓ transformRows and transformQuery can be combined (tailwind, bootstrap, daisyui)

  Tests:    12 passed (48 assertions)

Related Issue(s):

Documentation

This PR requires Documentation update?

  • Yes
  • No
  • I have already submitted a Documentation pull request.

@johannesMEGABAD johannesMEGABAD changed the title feat: added more hooks [6.x] Added three new lifecylce hooks (transform query, actions and rows) Mar 27, 2026
Copy link
Copy Markdown
Contributor

@MrYamous MrYamous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this PR includes more files than expected, are these the right branches ?

@johannesMEGABAD
Copy link
Copy Markdown
Contributor Author

I feel like this PR includes more files than expected, are these the right branches ?

Hey! Yeah the files are from running pint. I forked the 6.x branch and had tests fail due to styling. Then I ran pint for formatting and it linted like a lot of files.

Comment thread src/PowerGridComponent.php
@luanfreitasdev
Copy link
Copy Markdown
Member

Hello @johannesMEGABAD, Thank you for this. Can you update with the last commit in the v6 branch?

@glasssshouse
Copy link
Copy Markdown
Contributor

Hello @johannesMEGABAD, Thank you for this. Can you update with the last commit in the v6 branch?

Hey! Yes I will do on tuesday when I am back

@johannesMEGABAD
Copy link
Copy Markdown
Contributor Author

Hello @johannesMEGABAD, Thank you for this. Can you update with the last commit in the v6 branch?

Hey :) I merged in the latest commits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants