Skip to content

Commit

Permalink
Fix GraphQL exception caused by canonical entry (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni authored Jul 10, 2024
1 parent f56e09d commit 581542a
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/Actions/GetSiteDefaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Statamic\Facades\Blink;
use Statamic\Facades\Site;
use Statamic\Fields\Value;
use Statamic\Tags\Context;

class GetSiteDefaults
{
Expand All @@ -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(
Expand All @@ -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);
}
}

0 comments on commit 581542a

Please sign in to comment.