From cce7045e3f0ff418ee6e0a982a3830d604c6b64c Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Tue, 22 Oct 2024 00:10:27 +0200 Subject: [PATCH] [5.x] Extra values for field conditions. Add extra asset values. (#10588) Co-authored-by: Jason Varga --- resources/js/components/assets/Editor/Editor.vue | 5 +++++ .../components/field-conditions/ValidatorMixin.js | 2 +- resources/js/components/publish/Container.vue | 6 ++++++ resources/js/components/publish/Fields.vue | 4 ++++ .../js/tests/FieldConditionsValidator.test.js | 15 ++++++++++++++- src/Assets/Asset.php | 4 ++-- src/Http/Resources/CP/Assets/Asset.php | 14 +++++++++++++- 7 files changed, 45 insertions(+), 5 deletions(-) diff --git a/resources/js/components/assets/Editor/Editor.vue b/resources/js/components/assets/Editor/Editor.vue index e551c95308..ea80ed29bd 100644 --- a/resources/js/components/assets/Editor/Editor.vue +++ b/resources/js/components/assets/Editor/Editor.vue @@ -126,6 +126,7 @@ :name="publishContainer" :blueprint="fieldset" :values="values" + :extra-values="extraValues" :meta="meta" :errors="errors" @updated="values = { ...$event, focus: values.focus }" @@ -204,6 +205,7 @@ import FocalPointEditor from './FocalPointEditor.vue'; import PdfViewer from './PdfViewer.vue'; import PublishFields from '../../publish/Fields.vue'; import HasHiddenFields from '../../publish/HasHiddenFields'; +import pick from 'underscore/modules/pick'; export default { @@ -245,6 +247,7 @@ export default { asset: null, publishContainer: 'asset', values: {}, + extraValues: {}, meta: {}, fields: null, fieldset: null, @@ -347,6 +350,8 @@ export default { .flatten(true) .value(); + this.extraValues = pick(this.asset, ['filename', 'basename', 'extension', 'path', 'mimeType', 'width', 'height', 'duration']); + this.loading = false; }); }, diff --git a/resources/js/components/field-conditions/ValidatorMixin.js b/resources/js/components/field-conditions/ValidatorMixin.js index 770dd499b5..2e3c1c96b7 100644 --- a/resources/js/components/field-conditions/ValidatorMixin.js +++ b/resources/js/components/field-conditions/ValidatorMixin.js @@ -25,7 +25,7 @@ export default { } // Use validation to determine whether field should be shown. - let validator = new Validator(field, this.values, dottedFieldPath, this.$store, this.storeName); + let validator = new Validator(field, {...this.values, ...this.extraValues}, dottedFieldPath, this.$store, this.storeName); let passes = validator.passesConditions(); // If the field is configured to always save, never omit value. diff --git a/resources/js/components/publish/Container.vue b/resources/js/components/publish/Container.vue index 6feb8efc38..5429ac5206 100644 --- a/resources/js/components/publish/Container.vue +++ b/resources/js/components/publish/Container.vue @@ -25,6 +25,10 @@ export default { type: Object, default: () => {} }, + extraValues: { + type: Object, + default: () => {} + }, meta: { type: Object, default: () => {} @@ -78,6 +82,7 @@ export default { const initial = { blueprint: _.clone(this.blueprint), values: _.clone(this.values), + extraValues: _.clone(this.extraValues), meta: _.clone(this.meta), localizedFields: _.clone(this.localizedFields), site: this.site, @@ -97,6 +102,7 @@ export default { state: { blueprint: initial.blueprint, values: initial.values, + extraValues: initial.extraValues, hiddenFields: {}, jsonSubmittingFields: [], revealerFields: [], diff --git a/resources/js/components/publish/Fields.vue b/resources/js/components/publish/Fields.vue index e7aa4c0418..da0de21644 100644 --- a/resources/js/components/publish/Fields.vue +++ b/resources/js/components/publish/Fields.vue @@ -58,6 +58,10 @@ export default { return this.state.values; }, + extraValues() { + return this.state.extraValues || {}; + }, + meta() { return this.state.meta; }, diff --git a/resources/js/tests/FieldConditionsValidator.test.js b/resources/js/tests/FieldConditionsValidator.test.js index 08a1bc260a..af54ca38c6 100644 --- a/resources/js/tests/FieldConditionsValidator.test.js +++ b/resources/js/tests/FieldConditionsValidator.test.js @@ -64,7 +64,8 @@ const Fields = new Vue({ data() { return { storeName: 'base', - values: {} + values: {}, + extraValues: {}, } }, methods: { @@ -78,6 +79,9 @@ const Fields = new Vue({ } Store.commit('publish/base/setValues', storeValues); }, + setExtraValues(values) { + this.extraValues = values; + }, setStoreValues(values) { Store.commit('publish/base/setValues', values); }, @@ -106,6 +110,7 @@ let showFieldIf = function (conditions=null, dottedFieldPath=null) { afterEach(() => { Fields.values = {}; + Fields.extraValues = {}; Store.commit('publish/base/reset'); }); @@ -1010,3 +1015,11 @@ test('it properly omits nested revealer-hidden fields when multiple conditions a // Though this third venue is hidden by a revealer, it's also disabled by a regular toggle condition, so it should actually be omitted... expect(Store.state.publish.base.hiddenFields['nested.event_venue_three'].omitValue).toBe(true); }); + +test('it can use extra values in conditions', () => { + Fields.setValues({}); + Fields.setExtraValues({hello: 'world'}); + + expect(showFieldIf({hello: 'world'})).toBe(true); + expect(showFieldIf({hello: 'there'})).toBe(false); +}); diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 938d53de95..1f79073d8d 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -1071,12 +1071,12 @@ protected function defaultAugmentedRelations() return $this->selectedQueryRelations; } - private function hasDimensions() + public function hasDimensions() { return $this->isImage() || $this->isSvg() || $this->isVideo(); } - private function hasDuration() + public function hasDuration() { return $this->isAudio() || $this->isVideo(); } diff --git a/src/Http/Resources/CP/Assets/Asset.php b/src/Http/Resources/CP/Assets/Asset.php index 4e0ff2c437..7b7e004528 100644 --- a/src/Http/Resources/CP/Assets/Asset.php +++ b/src/Http/Resources/CP/Assets/Asset.php @@ -23,6 +23,7 @@ public function toArray($request) 'size' => Str::fileSizeForHumans($this->size()), 'lastModified' => $this->lastModified()->inPreferredFormat(), 'lastModifiedRelative' => $this->lastModified()->diffForHumans(), + 'mimeType' => $this->mimeType(), 'isImage' => $this->isImage(), 'isSvg' => $this->isSvg(), 'isAudio' => $this->isAudio(), @@ -31,10 +32,21 @@ public function toArray($request) 'isPdf' => $this->isPdf(), 'isPreviewable' => $this->isPreviewable(), - $this->mergeWhen($this->isImage() || $this->isSvg(), function () { + $this->mergeWhen($this->hasDimensions(), function () { return [ 'width' => $this->width(), 'height' => $this->height(), + ]; + }), + + $this->mergeWhen($this->hasDuration(), function () { + return [ + 'duration' => $this->duration(), + ]; + }), + + $this->mergeWhen($this->isImage() || $this->isSvg(), function () { + return [ 'preview' => $this->previewUrl(), 'thumbnail' => $this->thumbnailUrl('small'), ];