diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 1d692cec..e7382210 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\Event; +use Statamic\Facades\Collection; use Statamic\Facades\CP\Nav; use Statamic\Facades\GraphQL; use Statamic\Facades\Permission; @@ -55,6 +56,7 @@ public function bootAddon() ->bootAddonSubscriber() ->bootAddonGlidePresets() ->bootAddonCommands() + ->bootAddonComputedSeoFields() ->bootAddonGraphQL(); } @@ -159,6 +161,17 @@ protected function bootAddonCommands() return $this; } + protected function bootAddonComputedSeoFields() + { + Collection::all()->each(function ($collection) { + Collection::computed($collection->handle(), 'seo', function ($entry) { + return $this->getItemSeoCascade($entry); + }); + }); + + return $this; + } + protected function bootAddonGraphQL() { GraphQL::addType(\Statamic\SeoPro\GraphQL\SeoProType::class); @@ -168,12 +181,7 @@ protected function bootAddonGraphQL() return [ 'type' => GraphQL::type('SeoPro'), 'resolve' => function ($item) { - return (new Cascade) - ->with(SiteDefaults::load()->augmented()) - ->with($this->getAugmentedSectionDefaults($item)) - ->with($item->seo) - ->withCurrent($item) - ->get(); + return $this->getItemSeoCascade($item); }, ]; }; @@ -192,4 +200,14 @@ private function userHasSeoPermissions() || $user->can('edit seo site defaults') || $user->can('edit seo section defaults'); } + + private function getItemSeoCascade($item) + { + return (new Cascade) + ->with(SiteDefaults::load()->augmented()) + ->with($this->getAugmentedSectionDefaults($item)) + ->with($item->seo) + ->withCurrent($item) + ->get(); + } }