Skip to content

Commit

Permalink
[5.x] Sync entry form values after revision publish (#10095)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
jacksleight and jasonvarga authored May 16, 2024
1 parent 5a6567e commit fc313ef
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 43 deletions.
1 change: 1 addition & 0 deletions resources/js/components/entries/PublishForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ export default {
this.isWorkingCopy = isWorkingCopy;
this.confirmingPublish = false;
this.title = response.data.data.title;
this.values = this.resetValuesFromResponse(response.data.data.values);
this.activeLocalization.title = response.data.data.title;
this.activeLocalization.published = response.data.data.published;
this.activeLocalization.status = response.data.data.status;
Expand Down
43 changes: 2 additions & 41 deletions src/Http/Controllers/CP/Collections/EntriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

class EntriesController extends CpController
{
use QueriesFilters;
use ExtractsFromEntryFields,
QueriesFilters;

public function index(FilteredRequest $request, $collection)
{
Expand Down Expand Up @@ -431,46 +432,6 @@ private function resolveSlug($request)
};
}

protected function extractFromFields($entry, $blueprint)
{
// The values should only be data merged with the origin data.
// We don't want injected collection values, which $entry->values() would have given us.
$values = collect();
$target = $entry;
while ($target) {
$values = $target->data()->merge($target->computedData())->merge($values);
$target = $target->origin();
}
$values = $values->all();

if ($entry->hasStructure()) {
$values['parent'] = array_filter([optional($entry->parent())->id()]);

if ($entry->revisionsEnabled() && $entry->has('parent')) {
$values['parent'] = [$entry->get('parent')];
}
}

if ($entry->collection()->dated()) {
$datetime = substr($entry->date()->toDateTimeString(), 0, 19);
$datetime = ($entry->hasTime()) ? $datetime : substr($datetime, 0, 10);
$values['date'] = $datetime;
}

$fields = $blueprint
->fields()
->addValues($values)
->preProcess();

$values = $fields->values()->merge([
'title' => $entry->value('title'),
'slug' => $entry->slug(),
'published' => $entry->published(),
]);

return [$values->all(), $fields->meta()];
}

protected function extractAssetsFromValues($values)
{
return collect($values)
Expand Down
46 changes: 46 additions & 0 deletions src/Http/Controllers/CP/Collections/ExtractsFromEntryFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Statamic\Http\Controllers\CP\Collections;

trait ExtractsFromEntryFields
{
protected function extractFromFields($entry, $blueprint)
{
// The values should only be data merged with the origin data.
// We don't want injected collection values, which $entry->values() would have given us.
$values = collect();
$target = $entry;
while ($target) {
$values = $target->data()->merge($target->computedData())->merge($values);
$target = $target->origin();
}
$values = $values->all();

if ($entry->hasStructure()) {
$values['parent'] = array_filter([optional($entry->parent())->id()]);

if ($entry->revisionsEnabled() && $entry->has('parent')) {
$values['parent'] = [$entry->get('parent')];
}
}

if ($entry->collection()->dated()) {
$datetime = substr($entry->date()->toDateTimeString(), 0, 19);
$datetime = ($entry->hasTime()) ? $datetime : substr($datetime, 0, 10);
$values['date'] = $datetime;
}

$fields = $blueprint
->fields()
->addValues($values)
->preProcess();

$values = $fields->values()->merge([
'title' => $entry->value('title'),
'slug' => $entry->slug(),
'published' => $entry->published(),
]);

return [$values->all(), $fields->meta()];
}
}
22 changes: 20 additions & 2 deletions src/Http/Controllers/CP/Collections/PublishedEntriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

class PublishedEntriesController extends CpController
{
use ExtractsFromEntryFields;

public function store(Request $request, $collection, $entry)
{
$this->authorize('publish', $entry);
Expand All @@ -18,7 +20,15 @@ public function store(Request $request, $collection, $entry)
'user' => User::fromUser($request->user()),
]);

return new EntryResource($entry);
$blueprint = $entry->blueprint();

[$values] = $this->extractFromFields($entry, $blueprint);

return [
'data' => array_merge((new EntryResource($entry->fresh()))->resolve()['data'], [
'values' => $values,
]),
];
}

public function destroy(Request $request, $collection, $entry)
Expand All @@ -30,6 +40,14 @@ public function destroy(Request $request, $collection, $entry)
'user' => User::fromUser($request->user()),
]);

return new EntryResource($entry);
$blueprint = $entry->blueprint();

[$values] = $this->extractFromFields($entry, $blueprint);

return [
'data' => array_merge((new EntryResource($entry->fresh()))->resolve()['data'], [
'values' => $values,
]),
];
}
}

0 comments on commit fc313ef

Please sign in to comment.