Skip to content

Commit

Permalink
[5.x] Extra values for entry field conditions, including depth (#11080)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga authored Nov 6, 2024
1 parent 16bb315 commit 9b4302c
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 3 deletions.
2 changes: 2 additions & 0 deletions resources/js/components/entries/BaseCreateForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
:collection-has-routes="collectionHasRoutes"
:initial-fieldset="fieldset"
:initial-values="values"
:initial-extra-values="extraValues"
:initial-meta="meta"
:initial-localizations="localizations"
:initial-has-origin="false"
Expand Down Expand Up @@ -37,6 +38,7 @@ export default {
'collectionHasRoutes',
'fieldset',
'values',
'extraValues',
'meta',
'localizations',
'revisions',
Expand Down
5 changes: 5 additions & 0 deletions resources/js/components/entries/PublishForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
:name="publishContainer"
:blueprint="fieldset"
:values="values"
:extra-values="extraValues"
:reference="initialReference"
:meta="meta"
:errors="errors"
Expand Down Expand Up @@ -335,6 +336,7 @@ export default {
initialReference: String,
initialFieldset: Object,
initialValues: Object,
initialExtraValues: Object,
initialMeta: Object,
initialTitle: String,
initialLocalizations: Array,
Expand Down Expand Up @@ -375,6 +377,7 @@ export default {
title: this.initialTitle,
values: _.clone(this.initialValues),
meta: _.clone(this.initialMeta),
extraValues: _.clone(this.initialExtraValues),
localizations: _.clone(this.initialLocalizations),
localizedFields: this.initialLocalizedFields,
hasOrigin: this.initialHasOrigin,
Expand Down Expand Up @@ -606,6 +609,7 @@ export default {
clearTimeout(this.trackDirtyStateTimeout)
this.trackDirtyState = false
this.values = this.resetValuesFromResponse(response.data.data.values);
this.extraValues = response.data.data.extraValues;
this.trackDirtyStateTimeout = setTimeout(() => (this.trackDirtyState = true), 500)
this.$nextTick(() => this.$emit('saved', response));
return;
Expand All @@ -630,6 +634,7 @@ export default {
clearTimeout(this.trackDirtyStateTimeout);
this.trackDirtyState = false;
this.values = this.resetValuesFromResponse(response.data.data.values);
this.extraValues = response.data.data.extraValues;
this.trackDirtyStateTimeout = setTimeout(() => (this.trackDirtyState = true), 500);
this.initialPublished = response.data.data.published;
this.activeLocalization.published = response.data.data.published;
Expand Down
14 changes: 14 additions & 0 deletions resources/js/components/publish/Container.vue
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ export default {
setValues(state, values) {
state.values = values;
},
setExtraValues(state, values) {
state.extraValues = values;
},
setHiddenField(state, field) {
state.hiddenFields[field.dottedKey] = {
hidden: field.hidden,
Expand Down Expand Up @@ -211,6 +214,9 @@ export default {
context.commit('setValues', payload);
vm.emitUpdatedEvent(context.state.values);
},
setExtraValues(context, payload) {
context.commit('setExtraValues', payload);
},
setMeta(context, payload) {
context.commit('setMeta', payload);
}
Expand Down Expand Up @@ -276,6 +282,14 @@ export default {
}
},
extraValues: {
deep: true,
handler(after, before) {
if (_.isEqual(before, after)) return;
this.$store.commit(`publish/${this.name}/setExtraValues`, after);
}
},
meta: {
deep: true,
handler(after, before) {
Expand Down
1 change: 1 addition & 0 deletions resources/views/entries/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
:collection-has-routes="{{ Statamic\Support\Str::bool($collectionHasRoutes) }}"
:fieldset="{{ json_encode($blueprint) }}"
:values="{{ json_encode($values) }}"
:extra-values="{{ json_encode($extraValues) }}"
:meta="{{ json_encode($meta) }}"
:published="{{ json_encode($published) }}"
:localizations="{{ json_encode($localizations) }}"
Expand Down
1 change: 1 addition & 0 deletions resources/views/entries/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
initial-reference="{{ $reference }}"
:initial-fieldset="{{ json_encode($blueprint) }}"
:initial-values="{{ json_encode($values) }}"
:initial-extra-values="{{ json_encode($extraValues) }}"
:initial-localized-fields="{{ json_encode($localizedFields) }}"
:initial-meta="{{ json_encode($meta) }}"
initial-permalink="{{ $permalink }}"
Expand Down
1 change: 1 addition & 0 deletions src/Fieldtypes/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Entries extends Relationship
'initialReference' => 'reference',
'initialFieldset' => 'blueprint',
'initialValues' => 'values',
'initialExtraValues' => 'extraValues',
'initialLocalizedFields' => 'localizedFields',
'initialMeta' => 'meta',
'initialPermalink' => 'permalink',
Expand Down
9 changes: 7 additions & 2 deletions src/Http/Controllers/CP/Collections/EntriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function edit(Request $request, $collection, $entry)
$blueprint->ensureFieldHasConfig('author', ['visibility' => 'read_only']);
}

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

if ($hasOrigin = $entry->hasOrigin()) {
[$originValues, $originMeta] = $this->extractFromFields($entry->origin(), $blueprint);
Expand All @@ -121,6 +121,7 @@ public function edit(Request $request, $collection, $entry)
'editBlueprint' => cp_route('collections.blueprints.edit', [$collection, $blueprint]),
],
'values' => array_merge($values, ['id' => $entry->id()]),
'extraValues' => $extraValues,
'meta' => $meta,
'collection' => $collection->handle(),
'collectionHasRoutes' => ! is_null($collection->route($entry->locale())),
Expand Down Expand Up @@ -270,11 +271,12 @@ public function update(Request $request, $collection, $entry)
$saved = $entry->updateLastModified(User::current())->save();
}

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

return [
'data' => array_merge((new EntryResource($entry->fresh()))->resolve()['data'], [
'values' => $values,
'extraValues' => $extraValues,
]),
'saved' => $saved,
];
Expand Down Expand Up @@ -321,6 +323,9 @@ public function create(Request $request, $collection, $site)
'save' => cp_route('collections.entries.store', [$collection->handle(), $site->handle()]),
],
'values' => $values->all(),
'extraValues' => [
'depth' => 1,
],
'meta' => $fields->meta(),
'collection' => $collection->handle(),
'collectionCreateLabel' => $collection->createLabel(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ protected function extractFromFields($entry, $blueprint)
'published' => $entry->published(),
]);

return [$values->all(), $fields->meta()];
$extraValues = [
'depth' => $entry->page()?->depth(),
];

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

0 comments on commit 9b4302c

Please sign in to comment.