-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
97 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace markhuot\keystone\actions; | ||
|
||
use craft\elements\Asset; | ||
use craft\fields\Assets; | ||
use Illuminate\Support\Collection; | ||
use markhuot\keystone\interfaces\ShouldHandleEvents; | ||
|
||
class EagerLoadComponents implements ShouldHandleEvents | ||
{ | ||
public function handle(Collection $components) | ||
{ | ||
$assetIds = []; | ||
|
||
foreach ($components as $component) { | ||
foreach ($component->getType()->getFieldDefinitions() as $field) { | ||
if ($field->className === Assets::class) { | ||
$assetIds = array_merge($assetIds, $component->data->getRaw($field->handle) ?? []); | ||
} | ||
} | ||
} | ||
|
||
$assets = Asset::find()->id($assetIds)->collect()->keyBy('id'); | ||
|
||
foreach ($components as $component) { | ||
foreach ($component->getType()->getFieldDefinitions() as $field) { | ||
if ($field->className === Assets::class) { | ||
$assets = collect($component->data->getRaw($field->handle) ?? []) | ||
->map(fn ($id) => $assets->get($id)) | ||
->filter(); | ||
$component->data->populateRelation($field->handle, $assets); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace markhuot\keystone\events; | ||
|
||
use craft\base\Event; | ||
use Illuminate\Support\Collection; | ||
|
||
class AfterPopulateTree extends Event | ||
{ | ||
public Collection $components; | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace markhuot\keystone\interfaces; | ||
|
||
interface ShouldHandleEvents | ||
{ | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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