From d09e220bc93e75561aa53a6a53774ea63ae71acf Mon Sep 17 00:00:00 2001 From: Jan Willem Henckel Date: Tue, 30 Oct 2018 15:52:42 +0100 Subject: [PATCH] add fields needed to work with craft as headless cms --- src/Types/EntryInterface.php | 49 ++++++++++++++++++++++++++- src/Types/EntryInterfaceAlternate.php | 18 ++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/Types/EntryInterfaceAlternate.php diff --git a/src/Types/EntryInterface.php b/src/Types/EntryInterface.php index 09b25787..6e98522f 100644 --- a/src/Types/EntryInterface.php +++ b/src/Types/EntryInterface.php @@ -5,6 +5,7 @@ use markhuot\CraftQL\Builders\InterfaceBuilder; use markhuot\CraftQL\FieldBehaviors\EntryQueryArguments; use markhuot\CraftQL\Helpers\StringHelper; +use Craft; class EntryInterface extends InterfaceBuilder { @@ -26,13 +27,59 @@ function boot() { $this->addStringField('uri'); $this->addStringField('url'); + $this->addStringField('fullUri') + ->resolve(function($root, $args) { + $site = Craft::$app->sites->getSiteById($root->siteId); + return $root->uri ? rtrim( + str_replace( + '__home__', + '', + str_replace( + '@web', + '', + $site->baseUrl . $root->uri + ) + ), + '/' + ) : null; + }); + + $this->addField('site')->type(Site::class); + + $this->addField('supportedSites')->lists()->type(Site::class) + ->resolve(function ($root, $args) { + return array_map(function ($site) { + return Craft::$app->sites->getSiteById($site['siteId']); + }, $root['supportedSites']); + }); + + $this->addField('alternateEntries')->lists()->type(EntryInterfaceAlternate::class) + ->resolve(function ($root, $args) { + return array_map(function ($site) use ($root) { + $entry = Craft::$app->entries->getEntryById($root->id, $site['siteId']); + return (object) [ + 'entry' => $entry, + 'isSelf' => $root->siteId == $entry->siteId + ]; + }, $root['supportedSites']); + // return array_filter($allEntries, function($entry) { + // return $entry->enabled; + // }); + }); + + $this->addStringField('language') + ->resolve(function($root, $args) { + $site = Craft::$app->sites->getSiteById($root->siteId); + return $site->language; + }); + if ($this->request->token()->can('query:sections')) { $this->addField('section')->type(Section::class); $this->addField('type')->type(EntryType::class); } $this->addField('ancestors')->lists()->type(EntryInterface::class); - + $this->addField('children') ->lists() ->type(EntryInterface::class) diff --git a/src/Types/EntryInterfaceAlternate.php b/src/Types/EntryInterfaceAlternate.php new file mode 100644 index 00000000..4861f2f5 --- /dev/null +++ b/src/Types/EntryInterfaceAlternate.php @@ -0,0 +1,18 @@ +addField('entry')->type(EntryInterface::class); + $this->addBooleanField('isSelf')->nonNull(); + } + +} \ No newline at end of file