Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Computed seo cascade on entries #283

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,6 +56,7 @@ public function bootAddon()
->bootAddonSubscriber()
->bootAddonGlidePresets()
->bootAddonCommands()
->bootAddonComputedSeoFields()
->bootAddonGraphQL();
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
},
];
};
Expand All @@ -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();
}
}