Skip to content

Commit 49e35c2

Browse files
Adding parameters for updating a story (#31)
1 parent 66fc342 commit 49e35c2

File tree

4 files changed

+153
-5
lines changed

4 files changed

+153
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 1.0.11 - 2026-01-05
4+
- Adding parameters for updating a story
5+
36
## 1.0.10 - 2025-12-12
47
- Introduce `shouldRetry` flag for automatic 429 retry handling
58

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,89 @@ For publishing a story by the story identifier you can use the `publish` method:
509509
$storyApi->publish($storyId);
510510
```
511511

512+
### Update a Story
513+
514+
Updates an existing story using the Storyblok Management API.
515+
516+
#### Method Signature
517+
518+
```php
519+
$storyApi->update(
520+
string $storyId,
521+
Story $storyData,
522+
string $groupId = "",
523+
string $forceUpdate = "",
524+
int $releaseId = 0,
525+
bool $publish = false,
526+
string $lang = ""
527+
): StoryResponse
528+
```
529+
530+
#### Parameters
531+
532+
- **`storyId`** (`string`)
533+
The ID of the story to update.
534+
535+
- **`storyData`** (`Story`)
536+
The Story object containing updated content and metadata.
537+
538+
- **`groupId`** (`string`, optional)
539+
Group ID (UUID string) shared between stories defined as alternates.
540+
541+
- **`forceUpdate`** (`string`, optional)
542+
Set to `"1"` to force an update of a locked story.
543+
A story is locked when another user edits it. Forcing an update may cause content conflicts and has no effect if the story is locked due to a workflow stage.
544+
545+
- **`releaseId`** (`int`, optional)
546+
Numeric ID of the release in which the story should be updated.
547+
548+
- **`publish`** (`bool`, optional)
549+
Set to `true` to publish the story immediately after updating it.
550+
551+
- **`lang`** (`string`, optional)
552+
Language code to update or publish the story individually.
553+
The language must be enabled in **Settings → Internationalization**. |
554+
555+
#### Examples
556+
557+
Update a story:
558+
559+
```php
560+
$storyApi->update(
561+
storyId: '123456',
562+
storyData: $story
563+
);
564+
```
565+
566+
Update and publish immediately:
567+
568+
```php
569+
$storyApi->update(
570+
storyId: '123456',
571+
storyData: $story,
572+
publish: true
573+
);
574+
```
575+
576+
Update a specific language:
577+
578+
```php
579+
$storyApi->update(
580+
storyId: '123456',
581+
storyData: $story,
582+
lang: 'de'
583+
);
584+
```
585+
586+
Force update a locked story:
587+
588+
```php
589+
$storyApi->update(
590+
storyId: '123456',
591+
storyData: $story,
592+
forceUpdate: '1'
593+
);
594+
```
512595

513596
### Creating stories from large CSV files (streamed, memory-efficient)
514597
When importing stories from a CSV file, especially very large ones, one of the approaches is to stream the CSV file using generators and create stories one by one using the Management API client. This keeps memory usage low and allows the client’s built-in retry logic to gracefully handle potential 429 Too Many Requests responses, which can occur multiple times when processing large CSV files.

src/Data/BaseData.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ public function set(
157157
$array = &$array[$key];
158158
}
159159

160-
$array[array_shift($keys)] = $value;
160+
$key = array_shift($keys);
161+
162+
if (!is_null($key)) {
163+
$array[$key] = $value;
164+
}
165+
161166
return $this;
162167
}
163168

src/Endpoints/StoryApi.php

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,77 @@ public function create(
133133
}
134134

135135
/**
136-
* Updates an existing story
136+
* Updates an existing story.
137+
*
138+
* @param string $storyId
139+
* The ID of the story to update.
140+
*
141+
* @param Story $storyData
142+
* The Story object containing the updated content and metadata.
143+
*
144+
* @param string $groupId
145+
* Optional. Group ID (UUID string) shared between stories defined as alternates.
146+
*
147+
* @param string $forceUpdate
148+
* Optional. Set to "1" to force an update of a locked story.
149+
* A story is locked when another user edits it. Forcing an update may cause
150+
* a content conflict. This parameter has no effect if the story is locked
151+
* due to a workflow stage.
152+
*
153+
* @param int $releaseId
154+
* Optional. Numeric ID of the release in which the story should be updated.
155+
*
156+
* @param bool $publish
157+
* Optional. Set to true to publish the story immediately after updating it.
158+
*
159+
* @param string $lang
160+
* Optional. Language code to update or publish the story individually.
161+
* The language must be enabled in Settings → Internationalization.
162+
*
163+
* @return StoryResponse
164+
* The response containing the updated story data.
137165
*
138166
* @throws InvalidStoryDataException
167+
* Thrown when the provided story data is invalid.
139168
*/
140-
public function update(string $storyId, Story $storyData): StoryResponse
141-
{
169+
public function update(
170+
string $storyId,
171+
Story $storyData,
172+
string $groupId = "",
173+
string $forceUpdate = "",
174+
int $releaseId = 0,
175+
bool $publish = false,
176+
string $lang = "",
177+
): StoryResponse {
142178
$this->validateStoryId($storyId);
143179
//$this->validateStoryData($storyData);
180+
$payload = ["story" => $storyData->toArray()];
181+
182+
if ($groupId !== "") {
183+
$payload["group_id"] = $groupId;
184+
}
185+
186+
if ($forceUpdate !== "") {
187+
$payload["force_update"] = $forceUpdate;
188+
}
189+
190+
if ($releaseId > 0) {
191+
$payload["release_id"] = $releaseId;
192+
}
193+
194+
if ($publish) {
195+
$payload["publish"] = 1;
196+
}
197+
198+
if ($lang !== "") {
199+
$payload["lang"] = $lang;
200+
}
144201

145202
$httpResponse = $this->makeHttpRequest(
146203
"PUT",
147204
$this->buildStoryEndpoint($storyId),
148205
[
149-
"body" => json_encode(["story" => $storyData->toArray()]),
206+
"body" => json_encode($payload),
150207
],
151208
);
152209
return new StoryResponse($httpResponse);

0 commit comments

Comments
 (0)