From 581542ac663f15a4ffee8e2bd3518e946a07c14e Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Wed, 10 Jul 2024 14:46:32 -0400 Subject: [PATCH] Fix GraphQL exception caused by canonical entry (#163) --- src/Actions/GetSiteDefaults.php | 41 +++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/Actions/GetSiteDefaults.php b/src/Actions/GetSiteDefaults.php index c304476d..312daa76 100644 --- a/src/Actions/GetSiteDefaults.php +++ b/src/Actions/GetSiteDefaults.php @@ -8,6 +8,7 @@ use Statamic\Facades\Blink; use Statamic\Facades\Site; use Statamic\Fields\Value; +use Statamic\Tags\Context; class GetSiteDefaults { @@ -17,10 +18,6 @@ public static function handle(mixed $data): Collection return collect(); } - /** - * TODO: Instead of using the overriding code at the bottom, we might be able to refactor this to something similar to the GetPageData action. - * Instead of augmenting all the default values upfront, we could get the blueprint of each site default and then add the values to each blueprint field. - */ return Blink::once("advanced-seo::site::{$locale}", function () use ($locale, $data) { $siteDefaults = Defaults::enabledInType('site') ->flatMap(fn ($model) => GetAugmentedDefaults::handle( @@ -33,19 +30,33 @@ public static function handle(mixed $data): Collection )); /** - * Allow overriding site defaults by matching key in the data. - * This is useful if you want to override the site default when working with custom views. - * We need to create a new value object because we can't simply change the `value` in the existing object. + * TODO: Instead of merging the overrides, we might be able to refactor this to something similar to the GetPageData action. + * Instead of augmenting all the default values upfront, we could get the blueprint of each site default and then add the values to each blueprint field. */ - $overrides = $siteDefaults->intersectByKeys($data) - ->map(fn ($originalValue, $key) => new Value( - value: ($value = $data->get($key)) instanceof Value ? $value->raw() : $value, - handle: $originalValue->handle(), - fieldtype: $originalValue->fieldtype(), - augmentable: $originalValue->augmentable() - )); + if ($data instanceof Context) { + return self::mergeViewOverrides($siteDefaults, $data); + } - return $siteDefaults->merge($overrides); + return $siteDefaults; }); } + + /** + * Allow overriding site defaults by matching key in the data. + * This is useful if you want to override the site default when working with custom views. + * We need to create a new value object because we can't simply change the `value` in the existing object. + */ + protected static function mergeViewOverrides(Collection $siteDefaults, Collection $overrides): Collection + { + $overrides = $siteDefaults + ->intersectByKeys($overrides) + ->map(fn ($originalValue, $key) => new Value( + value: ($value = $overrides->get($key)) instanceof Value ? $value->raw() : $value, + handle: $originalValue->handle(), + fieldtype: $originalValue->fieldtype(), + augmentable: $originalValue->augmentable() + )); + + return $siteDefaults->merge($overrides); + } }