|
5 | 5 |
|
6 | 6 | use Illuminate\Support\Arr;
|
7 | 7 | use Illuminate\Support\Collection;
|
| 8 | +use Illuminate\Support\Facades\Cache; |
8 | 9 | use Illuminate\Support\Str;
|
9 | 10 | use Illuminate\View\View;
|
10 | 11 | use Riclep\Storyblok\Exceptions\UnableToRenderException;
|
@@ -450,41 +451,65 @@ public function getRelation(RequestStory $requestStory, $relation, $className =
|
450 | 451 | * @param array|null $options
|
451 | 452 | * @return array
|
452 | 453 | */
|
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 | + } |
488 | 513 |
|
489 | 514 | /**
|
490 | 515 | * Returns the casts on this Block
|
|
0 commit comments