Skip to content

Commit

Permalink
[5.x] Extra values for field conditions. Add extra asset values. (#10588
Browse files Browse the repository at this point in the history
)

Co-authored-by: Jason Varga <jason@pixelfear.com>
  • Loading branch information
daun and jasonvarga authored Oct 21, 2024
1 parent 97b36cd commit cce7045
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 5 deletions.
5 changes: 5 additions & 0 deletions resources/js/components/assets/Editor/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
:name="publishContainer"
:blueprint="fieldset"
:values="values"
:extra-values="extraValues"
:meta="meta"
:errors="errors"
@updated="values = { ...$event, focus: values.focus }"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -245,6 +247,7 @@ export default {
asset: null,
publishContainer: 'asset',
values: {},
extraValues: {},
meta: {},
fields: null,
fieldset: null,
Expand Down Expand Up @@ -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;
});
},
Expand Down
2 changes: 1 addition & 1 deletion resources/js/components/field-conditions/ValidatorMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions resources/js/components/publish/Container.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export default {
type: Object,
default: () => {}
},
extraValues: {
type: Object,
default: () => {}
},
meta: {
type: Object,
default: () => {}
Expand Down Expand Up @@ -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,
Expand All @@ -97,6 +102,7 @@ export default {
state: {
blueprint: initial.blueprint,
values: initial.values,
extraValues: initial.extraValues,
hiddenFields: {},
jsonSubmittingFields: [],
revealerFields: [],
Expand Down
4 changes: 4 additions & 0 deletions resources/js/components/publish/Fields.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export default {
return this.state.values;
},
extraValues() {
return this.state.extraValues || {};
},
meta() {
return this.state.meta;
},
Expand Down
15 changes: 14 additions & 1 deletion resources/js/tests/FieldConditionsValidator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ const Fields = new Vue({
data() {
return {
storeName: 'base',
values: {}
values: {},
extraValues: {},
}
},
methods: {
Expand All @@ -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);
},
Expand Down Expand Up @@ -106,6 +110,7 @@ let showFieldIf = function (conditions=null, dottedFieldPath=null) {

afterEach(() => {
Fields.values = {};
Fields.extraValues = {};
Store.commit('publish/base/reset');
});

Expand Down Expand Up @@ -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);
});
4 changes: 2 additions & 2 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
14 changes: 13 additions & 1 deletion src/Http/Resources/CP/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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'),
];
Expand Down

0 comments on commit cce7045

Please sign in to comment.