Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Conditional fields in asset blueprints #10588

Open
wants to merge 2 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions resources/js/components/assets/Editor/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,6 @@ export default {
this.$axios.get(url).then(response => {
const data = response.data.data;
this.asset = data;

// If there are no fields, it will be an empty array when PHP encodes
// it into JSON on the server. We'll ensure it's always an object.
this.values = _.isArray(data.values) ? {} : data.values;

this.meta = data.meta;
this.actionUrl = data.actionUrl;
this.actions = data.actions;
Expand All @@ -347,10 +342,41 @@ export default {
.flatten(true)
.value();

// If there are no fields, it will be an empty array when PHP encodes
// it into JSON on the server. We'll ensure it's always an object.
const blueprintValues = _.isArray(data.values) ? {} : data.values;
const assetValues = _.chain(this.asset)
.pick(['filename', 'basename', 'extension', 'path', 'mimeType', 'width', 'height', 'duration'])
.omit(_.pluck(this.fields, 'handle'))
.value();

// Merge asset file data with asset blueprint data
this.values = { ...assetValues, ...blueprintValues };

// Append hidden fields to blueprint to allow field conditions
const hiddenFields = Object.keys(assetValues).map(handle => this.createHiddenField(handle));
this.fieldset = this.extendBlueprintFields(this.fieldset, hiddenFields);

this.loading = false;
});
},

createHiddenField(handle) {
return {
handle,
display: handle,
type: 'text',
if: { 'internal_should_not_exist_in_blueprint': 'equals true' }
};
},

extendBlueprintFields(blueprint, fields) {
const extended = clone(blueprint);
const existingFields = data_get(extended, 'tabs.0.sections.0.fields');
existingFields?.push(...fields);
return extended;
},

openFocalPointEditor() {
this.showFocalPointEditor = true;
},
Expand Down
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
Loading