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

fix: Allow file fields to be non translatable #2529

Open
wants to merge 2 commits into
base: 3.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
5 changes: 3 additions & 2 deletions frontend/js/mixins/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export default {
open: function () {
this.opened = true
},
fieldName: function (id) {
return this.name + '[' + id + ']' // output : nameOfBlock[UniqID][name]
fieldName: function (id, extra = null) {
const fieldName = this.name + '[' + id + ']' // output : nameOfBlock[UniqID][name]
return extra ? fieldName + extra : fieldName
},
repeaterName: function (id) {
return this.name.replace('[', '-').replace(']', '') + '|' + id // nameOfBlock-UniqID|name
Expand Down
2 changes: 2 additions & 0 deletions src/Services/Forms/Fields/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace A17\Twill\Services\Forms\Fields;

use A17\Twill\Services\Forms\Fields\Traits\CanDisableTranslate;
use A17\Twill\Services\Forms\Fields\Traits\CanHaveButtonOnTop;
use A17\Twill\Services\Forms\Fields\Traits\HasFieldNote;
use A17\Twill\Services\Forms\Fields\Traits\HasMax;
Expand All @@ -14,6 +15,7 @@ class Files extends BaseFormField
use HasMax;
use HasFieldNote;
use CanHaveButtonOnTop;
use CanDisableTranslate;

protected ?string $itemLabel = null;

Expand Down
23 changes: 23 additions & 0 deletions src/Services/Forms/Fields/Traits/CanDisableTranslate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace A17\Twill\Services\Forms\Fields\Traits;

trait CanDisableTranslate
{
/**
* @var bool
*/
protected bool $disableTranslate = false;

/**
* Disables translate
*
* @return $this
*/
public function disableTranslate(): static

Check warning on line 17 in src/Services/Forms/Fields/Traits/CanDisableTranslate.php

View check run for this annotation

Codecov / codecov/patch

src/Services/Forms/Fields/Traits/CanDisableTranslate.php#L17

Added line #L17 was not covered by tests
{
$this->disableTranslate = true;

Check warning on line 19 in src/Services/Forms/Fields/Traits/CanDisableTranslate.php

View check run for this annotation

Codecov / codecov/patch

src/Services/Forms/Fields/Traits/CanDisableTranslate.php#L19

Added line #L19 was not covered by tests

return $this;

Check warning on line 21 in src/Services/Forms/Fields/Traits/CanDisableTranslate.php

View check run for this annotation

Codecov / codecov/patch

src/Services/Forms/Fields/Traits/CanDisableTranslate.php#L21

Added line #L21 was not covered by tests
}
}
8 changes: 7 additions & 1 deletion src/View/Components/Fields/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
public bool $buttonOnTop = false,
public ?string $itemLabel = null,
public ?string $fieldNote = null,
public bool $disableTranslate = false
) {
$itemLabel = $itemLabel ?? strtolower($label);
$this->itemLabel = $itemLabel;
Expand All @@ -28,12 +29,17 @@
label: $label,
note: $note ?? 'Add' . ($max > 1 ? " up to $max $itemLabel" : ' one ' . Str::singular($itemLabel)),
renderForBlocks: $renderForBlocks,
renderForModal: $renderForModal
renderForModal: $renderForModal,
);
}

public function render(): View
{
return view('twill::partials.form._files', $this->data());
}

public function getExtraName(): string

Check warning on line 41 in src/View/Components/Fields/Files.php

View check run for this annotation

Codecov / codecov/patch

src/View/Components/Fields/Files.php#L41

Added line #L41 was not covered by tests
{
return $this->disableTranslate ? '[' . config('app.locale') . ']' : '';

Check warning on line 43 in src/View/Components/Fields/Files.php

View check run for this annotation

Codecov / codecov/patch

src/View/Components/Fields/Files.php#L43

Added line #L43 was not covered by tests
}
}
10 changes: 8 additions & 2 deletions src/View/Components/Fields/TwillFormComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ public function formFieldName(bool $asAttributes = false, ?string $customName =
{
$name = $customName ?? $this->name;
if ($this->renderForBlocks) {
$extra = $this->getExtraName();
if ($asAttributes) {
return "name: fieldName('$name')";
return "name: fieldName('$name', '$extra')";
}

return ":name=\"fieldName('$name')\"";
return ":name=\"fieldName('$name', '$extra')\"";
}

if ($asAttributes) {
Expand All @@ -66,4 +67,9 @@ public function formFieldName(bool $asAttributes = false, ?string $customName =
}

abstract public function render(): View;

public function getExtraName(): string
{
return '';
}
}
66 changes: 44 additions & 22 deletions views/partials/form/_files.blade.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
<a17-locale
type="a17-filefield"
:attributes="{
label: '{{ $label }}',
itemLabel: '{{ $itemLabel }}',
note: '{{ $note }}',
fieldNote: '{{ $fieldNote }}',
max: {{ $max }},
filesizeMax: {{ $filesizeMax }},
@if ($buttonOnTop) buttonOnTop: true, @endif
{!! $formFieldName(true) !!}
}"
></a17-locale>
@if (!$disableTranslate ?? true)
<a17-locale
type="a17-filefield"
:attributes="{
label: '{{ $label }}',
itemLabel: '{{ $itemLabel }}',
note: '{{ $note }}',
fieldNote: '{{ $fieldNote }}',
max: {{ $max }},
filesizeMax: {{ $filesizeMax }},
@if ($buttonOnTop) buttonOnTop: true, @endif
{!! $formFieldName(true) !!}
}"
></a17-locale>

@unless($renderForBlocks)
@push('vuexStore')
@foreach(getLocales() as $locale)
@if (isset($form_fields['files']) && isset($form_fields['files'][$locale][$name]))
window['{{ config('twill.js_namespace') }}'].STORE.medias.selected["{{ $name }}[{{ $locale }}]"] = {!! json_encode($form_fields['files'][$locale][$name]) !!}
@endif
@endforeach
@endpush
@endunless
@else
<a17-filefield
{!! $formFieldName() !!}
:max="{{ $max }}"
label="{{ $label }}"
item-label="{{ $itemLabel }}"
note="{{ $note }}"
field-note="{{ $fieldNote }}"
:filesize-max="{{ $filesizeMax }}"
@if ($buttonOnTop) :button-on-top="true", @endif
></a17-filefield>

@unless($renderForBlocks)
@push('vuexStore')
@if (isset($form_fields['files']) && isset($form_fields['files'][config('app.locale')]) && isset($form_fields['files'][config('app.locale')][$name]))
window['{{ config('twill.js_namespace') }}'].STORE.medias.selected["{{ $name }}"] = {!! json_encode($form_fields['files'][config('app.locale')][$name]) !!}
@endif
@endpush
@endunless
@endif

@unless($renderForBlocks)
@push('vuexStore')
@foreach(getLocales() as $locale)
@if (isset($form_fields['files']) && isset($form_fields['files'][$locale][$name]))
window['{{ config('twill.js_namespace') }}'].STORE.medias.selected["{{ $name }}[{{ $locale }}]"] = {!! json_encode($form_fields['files'][$locale][$name]) !!}
@endif
@endforeach
@endpush
@endunless
Loading