Skip to content

Commit d9a0fb4

Browse files
committed
Merge branch 'release2.19.0'
2 parents 7138a8c + 7f79991 commit d9a0fb4

File tree

5 files changed

+60
-11
lines changed

5 files changed

+60
-11
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"ivopetkov/html5-dom-document-php": "2.*",
2929
"league/commonmark": "^2.0",
3030
"spatie/schema-org": "^3.3",
31-
"storyblok/php-client": "^2.2",
31+
"storyblok/php-client": "^2.3",
3232
"storyblok/richtext-resolver": "2.0.0"
3333
},
3434
"require-dev": {

config/storyblok.php

+10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@
4747

4848
/*
4949
|--------------------------------------------------------------------------
50+
| Specify which Storyblok API region to use
51+
|--------------------------------------------------------------------------
52+
|
53+
| Defaults to null which should be the original EU region
54+
|
55+
*/
56+
'api_region' => null,
57+
58+
/*
59+
|--------------------------------------------------------------------------
5060
| Storyblok draft mode
5161
|--------------------------------------------------------------------------
5262
|

src/RequestStory.php

+35-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,23 @@
1111
class RequestStory
1212
{
1313
/**
14-
* @var array An array of relations to resolve matching: component_name.field_name
14+
* @var string|null A comma delimited string of relations to resolve matching: component_name.field_name
1515
* @see https://www.storyblok.com/tp/using-relationship-resolving-to-include-other-content-entries
1616
*/
17-
protected array $resolveRelations = [];
17+
protected ?string $resolveRelations = null;
18+
19+
20+
/**
21+
* @var string|null The language version of the Story to load
22+
* @see https://www.storyblok.com/docs/guide/in-depth/internationalization
23+
*/
24+
protected ?string $language = null;
25+
26+
/**
27+
* @var string|null The fallback language version of the Story to load
28+
* @see https://www.storyblok.com/docs/guide/in-depth/internationalization
29+
*/
30+
protected ?string $fallbackLanguage = null;
1831

1932
/**
2033
* Caches the response if needed
@@ -48,11 +61,22 @@ public function get($slugOrUuid): mixed
4861
*
4962
* @param $resolveRelations
5063
*/
51-
public function prepareRelations($resolveRelations): void
64+
public function resolveRelations($resolveRelations): void
5265
{
5366
$this->resolveRelations = implode(',', $resolveRelations);
5467
}
5568

69+
/**
70+
* Set the language and fallback language to use for this Story, will default to ‘default’
71+
*
72+
* @param string|null $language
73+
* @param string|null $fallbackLanguage
74+
*/
75+
public function language($language, $fallbackLanguage = null) {
76+
$this->language = $language;
77+
$this->fallbackLanguage = $fallbackLanguage;
78+
}
79+
5680
/**
5781
* Makes the API request
5882
*
@@ -72,6 +96,14 @@ private function makeRequest($slugOrUuid): array
7296
$storyblokClient = $storyblokClient->resolveLinks(config('storyblok.resolve_links'));
7397
}
7498

99+
if ($this->language) {
100+
$storyblokClient = $storyblokClient->language($this->language);
101+
}
102+
103+
if ($this->fallbackLanguage) {
104+
$storyblokClient = $storyblokClient->fallbackLanguage($this->fallbackLanguage);
105+
}
106+
75107
if (Str::isUuid($slugOrUuid)) {
76108
$storyblokClient = $storyblokClient->getStoryByUuid($slugOrUuid);
77109
} else {

src/Storyblok.php

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

66

77
use Riclep\Storyblok\Traits\HasChildClasses;
8+
use Storyblok\ApiException;
89

910
class Storyblok
1011
{
@@ -14,16 +15,22 @@ class Storyblok
1415
/**
1516
* Reads the requested story from the API
1617
*
17-
* @param $slug
18+
* @param string $slug
1819
* @param array|null $resolveRelations
20+
* @param null $language
21+
* @param null $fallbackLanguage
1922
* @return mixed
20-
* @throws \Storyblok\ApiException
23+
* @throws ApiException
2124
*/
22-
public function read($slug, array $resolveRelations = null) {
25+
public function read(string $slug, array $resolveRelations = null, $language = null, $fallbackLanguage = null) {
2326
$storyblokRequest = new RequestStory();
2427

2528
if ($resolveRelations) {
26-
$storyblokRequest->prepareRelations($resolveRelations);
29+
$storyblokRequest->resolveRelations($resolveRelations);
30+
}
31+
32+
if ($language) {
33+
$storyblokRequest->language($language, $fallbackLanguage);
2734
}
2835

2936
$response = $storyblokRequest->get($slug);
@@ -45,4 +52,4 @@ public function setData($data): mixed
4552

4653
return new $class($response);
4754
}
48-
}
55+
}

src/StoryblokServiceProvider.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public function register(): void
6464

6565
// register the Storyblok client, checking if we are in edit more of the dev requests draft content
6666
$client = new Client(
67-
config('storyblok.draft') ? config('storyblok.api_preview_key') : config('storyblok.api_public_key'),
68-
"api.storyblok.com", "v2", config('storyblok.use_ssl'), config('storyblok.api_region')
67+
config('storyblok.draft') ? config('storyblok.api_preview_key') : config('storyblok.api_public_key'),
68+
"api.storyblok.com", "v2", config('storyblok.use_ssl'), config('storyblok.api_region')
6969
);
7070

7171
// if we’re in Storyblok’s edit mode let’s save that in the config for easy access

0 commit comments

Comments
 (0)