Skip to content

Commit 7b215c2

Browse files
committed
Block inverse relations cached
Block inverseRelation now caches the API response from Storyblok. It now returns a collect matching the format of Folders with headers and a stories. Stories are now transformed into their correct Block type. Previously it returned the array of data from Storyblok.
1 parent 758eda0 commit 7b215c2

File tree

1 file changed

+60
-35
lines changed

1 file changed

+60
-35
lines changed

src/Block.php

+60-35
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Illuminate\Support\Arr;
77
use Illuminate\Support\Collection;
8+
use Illuminate\Support\Facades\Cache;
89
use Illuminate\Support\Str;
910
use Illuminate\View\View;
1011
use Riclep\Storyblok\Exceptions\UnableToRenderException;
@@ -450,41 +451,65 @@ public function getRelation(RequestStory $requestStory, $relation, $className =
450451
* @param array|null $options
451452
* @return array
452453
*/
453-
public function inverseRelation(string $foreignRelationshipField, string $foreignRelationshipType = 'multi', array $components = null, array $options = null): array
454-
{
455-
$storyblokClient = resolve('Storyblok\Client');
456-
457-
$type = 'any_in_array';
458-
459-
if ($foreignRelationshipType === 'single') {
460-
$type = 'in';
461-
}
462-
463-
$query = [
464-
'filter_query' => [
465-
$foreignRelationshipField => [$type => $this->meta('page_uuid') ?? $this->page()->uuid()]
466-
],
467-
];
468-
469-
if ($components) {
470-
$query = array_merge_recursive($query, [
471-
'filter_query' => [
472-
'component' => ['in' => $components],
473-
]
474-
]);
475-
}
476-
477-
if ($options) {
478-
$query = array_merge_recursive($query, $options);
479-
}
480-
481-
$storyblokClient->getStories($query);
482-
483-
return [
484-
'headers' => $storyblokClient->getHeaders(),
485-
'stories' => $storyblokClient->getBody()['stories'],
486-
];
487-
}
454+
public function inverseRelation(string $foreignRelationshipField, string $foreignRelationshipType = 'multi', array $components = null, array $options = null): array
455+
{
456+
$storyblokClient = resolve('Storyblok\Client');
457+
458+
$type = 'any_in_array';
459+
460+
if ($foreignRelationshipType === 'single') {
461+
$type = 'in';
462+
}
463+
464+
$query = [
465+
'filter_query' => [
466+
$foreignRelationshipField => [$type => $this->meta('page_uuid') ?? $this->page()->uuid()]
467+
],
468+
];
469+
470+
if ($components) {
471+
$query = array_merge_recursive($query, [
472+
'filter_query' => [
473+
'component' => ['in' => $components],
474+
]
475+
]);
476+
}
477+
478+
if ($options) {
479+
$query = array_merge_recursive($query, $options);
480+
}
481+
482+
if (request()->has('_storyblok') || !config('storyblok.cache')) {
483+
$storyblokClient->getStories($query);
484+
485+
$response = [
486+
'headers' => $storyblokClient->getHeaders(),
487+
'stories' => $storyblokClient->getBody()['stories'],
488+
];
489+
} else {
490+
$apiHash = md5(config('storyblok.api_public_key') ?? config('storyblok.api_preview_key')); // unique id for multitenancy applications
491+
492+
$uniqueTag = md5(serialize($query));
493+
494+
$response = Cache::remember($foreignRelationshipField . '-' . $foreignRelationshipType . '-' . $apiHash . '-' . $uniqueTag, config('storyblok.cache_duration') * 60, function () use ($storyblokClient, $query) {
495+
$storyblokClient->getStories($query);
496+
497+
return [
498+
'headers' => $storyblokClient->getHeaders(),
499+
'stories' => $storyblokClient->getBody()['stories'],
500+
];
501+
});
502+
}
503+
504+
return [
505+
'headers' => $response['headers'],
506+
'stories' => collect($response['stories'])->transform(function ($story) {
507+
$blockClass = $this->getChildClassName('Page', $story['content']['component']);
508+
509+
return new $blockClass($story);
510+
}),
511+
];
512+
}
488513

489514
/**
490515
* Returns the casts on this Block

0 commit comments

Comments
 (0)