Skip to content

Commit 77acada

Browse files
committed
Add the localizable field toggler. Strip out localizable: false since it's the default. Closes #2045
1 parent a601ce0 commit 77acada

File tree

11 files changed

+176
-3
lines changed

11 files changed

+176
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
### What's new
6+
- Localizable field toggle. [#2045](https://github.com/statamic/cms/issues/2045)
67
- The `form` tags get a `submission_created` boolean. [#2285](https://github.com/statamic/cms/issues/2285)
78
- The `template` fieldtype will ignore views in the `partials` directory when `hide_partials` is enabled. [#2249](https://github.com/statamic/cms/issues/2249)
89
- The "first child" option is only in `link` fieldtypes if the entry is in a structured collection. [#2209](https://github.com/statamic/cms/issues/2209)

resources/js/components/blueprints/Fields.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
:is-editing="editingField === field._id"
1616
:is-section-expanded="isSectionExpanded"
1717
:suggestable-condition-fields="suggestableConditionFields"
18+
:can-define-localizable="canDefineLocalizable"
1819
@edit="$emit('field-editing', field._id)"
1920
@updated="$emit('field-updated', i, $event)"
2021
@deleted="$emit('field-deleted', i)"
@@ -70,9 +71,12 @@ import ImportField from './ImportField.vue';
7071
import LinkFields from './LinkFields.vue';
7172
import FieldtypeSelector from '../fields/FieldtypeSelector.vue';
7273
import FieldSettings from '../fields/Settings.vue';
74+
import CanDefineLocalizable from '../fields/CanDefineLocalizable';
7375
7476
export default {
7577
78+
mixins: [CanDefineLocalizable],
79+
7680
components: {
7781
RegularField,
7882
ImportField,

resources/js/components/blueprints/RegularField.vue

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
</div>
1212
<div class="pr-1 flex">
1313
<width-selector v-model="width" class="mr-1" />
14+
<button v-if="canDefineLocalizable"
15+
class="hover:text-grey-100 mr-1"
16+
:class="{ 'text-grey-100': localizable, 'text-grey-60': !localizable }"
17+
v-tooltip="__('Localizable')"
18+
@click="localizable = !localizable"
19+
>
20+
<svg-icon name="earth" />
21+
</button>
1422
<button @click.prevent="$emit('deleted')" class="text-grey-60 hover:text-grey-100"><svg-icon name="trash" /></button>
1523
<stack name="field-settings" v-if="isEditing" @closed="editorClosed">
1624
<field-settings
@@ -35,10 +43,11 @@
3543
import Field from './Field.vue';
3644
import FieldSettings from '../fields/Settings.vue';
3745
import WidthSelector from '../fields/WidthSelector.vue';
46+
import CanDefineLocalizable from '../fields/CanDefineLocalizable';
3847
3948
export default {
4049
41-
mixins: [Field],
50+
mixins: [Field, CanDefineLocalizable],
4251
4352
components: {
4453
FieldSettings,
@@ -51,7 +60,7 @@ export default {
5160
5261
data() {
5362
return {
54-
showHandle: false
63+
showHandle: false,
5564
}
5665
},
5766
@@ -96,7 +105,19 @@ export default {
96105
if (! this.isSectionExpanded) return 'blueprint-section-field-w-full';
97106
98107
return `blueprint-section-field-${tailwind_width_class(this.width)}`;
99-
}
108+
},
109+
110+
localizable: {
111+
get() {
112+
return this.field.config.localizable || false;
113+
},
114+
set(localizable) {
115+
let field = this.field;
116+
field.config.localizable = localizable;
117+
if (field.type === 'reference') field.config_overrides.push('localizable');
118+
this.$emit('updated', field);
119+
}
120+
},
100121
},
101122
102123
methods: {

resources/js/components/blueprints/Section.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
:editing-field="editingField"
3333
:is-section-expanded="isEditing || isSingle"
3434
:suggestable-condition-fields="suggestableConditionFields"
35+
:can-define-localizable="canDefineLocalizable"
3536
@field-created="fieldCreated"
3637
@field-updated="fieldUpdated"
3738
@field-deleted="deleteField"
@@ -54,9 +55,12 @@
5455

5556
<script>
5657
import Fields from './Fields.vue';
58+
import CanDefineLocalizable from '../fields/CanDefineLocalizable';
5759
5860
export default {
5961
62+
mixins: [CanDefineLocalizable],
63+
6064
components: {
6165
Fields,
6266
},

resources/js/components/blueprints/Sections.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
:key="section._id"
1111
:section="section"
1212
:is-single="singleSection"
13+
:can-define-localizable="canDefineLocalizable"
1314
:deletable="isSectionDeletable(i)"
1415
@updated="updateSection(i, $event)"
1516
@deleted="deleteSection(i)"
@@ -36,11 +37,14 @@
3637
import uniqid from 'uniqid';
3738
import BlueprintSection from './Section.vue';
3839
import {Sortable, Plugins} from '@shopify/draggable';
40+
import CanDefineLocalizable from '../fields/CanDefineLocalizable';
3941
4042
let sortableSections, sortableFields;
4143
4244
export default {
4345
46+
mixins: [CanDefineLocalizable],
47+
4448
components: {
4549
BlueprintSection
4650
},
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default {
2+
3+
props: {
4+
canDefineLocalizable: {
5+
type: Boolean,
6+
default: () => {
7+
return Statamic.$config.get('sites').length > 1;
8+
}
9+
}
10+
}
11+
12+
}

resources/js/components/fieldtypes/grid/FieldsFieldtype.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<fields
44
:fields="fields"
55
:editing-field="editingField"
6+
:can-define-localizable="false"
67
@field-created="fieldCreated"
78
@field-updated="fieldUpdated"
89
@field-linked="fieldLinked"

resources/js/components/fieldtypes/replicator/SetsFieldtype.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<sections
66
:initial-sections="sections"
77
:require-section="config.require_section"
8+
:can-define-localizable="false"
89
:add-section-text="__('Add Set')"
910
:new-section-text="__('New Set')"
1011
@updated="sectionsUpdated"

src/Fields/FieldTransformer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ private static function inlineSectionField(array $submitted)
3131
unset($field['width']);
3232
}
3333

34+
if (Arr::get($field, 'localizable', false) === false) {
35+
unset($field['localizable']);
36+
}
37+
3438
return array_filter([
3539
'handle' => $submitted['handle'],
3640
'field' => $field,
@@ -69,6 +73,7 @@ private static function referenceFieldToVue($field): array
6973
);
7074

7175
$mergedConfig['width'] = $mergedConfig['width'] ?? 100;
76+
$mergedConfig['localizable'] = $mergedConfig['localizable'] ?? false;
7277

7378
return [
7479
'handle' => $field['handle'],
@@ -85,6 +90,7 @@ private static function inlineFieldToVue($field): array
8590
{
8691
$config = $field['field'];
8792
$config['width'] = $config['width'] ?? 100;
93+
$config['localizable'] = $config['localizable'] ?? false;
8894

8995
return [
9096
'handle' => $field['handle'],

tests/Feature/Collections/Blueprints/UpdateBlueprintTest.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,123 @@ public function width_of_100_gets_stripped_out_for_inline_fields_but_left_in_for
321321
], Facades\Blueprint::find('collections.test.test')->contents());
322322
}
323323

324+
/** @test */
325+
public function localizable_of_false_gets_stripped_out_for_inline_fields_but_left_in_for_reference_fields_with_config_overrides()
326+
{
327+
$this->setTestRoles(['test' => ['access cp', 'configure fields']]);
328+
$user = tap(Facades\User::make()->assignRole('test'))->save();
329+
$collection = tap(Collection::make('test'))->save();
330+
$blueprint = (new Blueprint)->setNamespace('collections.test')->setHandle('test')->setContents(['title' => 'Test'])->save();
331+
332+
$fieldset = (new Fieldset)->setContents([
333+
'fields' => [
334+
[
335+
'handle' => 'somefield',
336+
'field' => [],
337+
],
338+
],
339+
]);
340+
341+
Facades\Fieldset::shouldReceive('find')
342+
->with('somefieldset')
343+
->andReturn($fieldset);
344+
345+
$this
346+
->actingAs($user)
347+
->submit($collection, $blueprint, [
348+
'title' => 'Updated title',
349+
'sections' => [
350+
[
351+
'_id' => 'id-one',
352+
'handle' => 'one',
353+
'display' => 'Section One',
354+
'fields' => [
355+
[
356+
'_id' => 'id-s1-f1',
357+
'handle' => 'one-one',
358+
'type' => 'reference',
359+
'field_reference' => 'somefieldset.somefield',
360+
'config' => [
361+
'foo' => 'bar',
362+
'localizable' => false,
363+
],
364+
'config_overrides' => ['localizable'],
365+
],
366+
[
367+
'_id' => 'id-s1-f2',
368+
'handle' => 'one-two',
369+
'type' => 'inline',
370+
'config' => [
371+
'type' => 'text',
372+
'localizable' => false,
373+
],
374+
],
375+
[
376+
'_id' => 'id-s1-f3',
377+
'handle' => 'one-three',
378+
'type' => 'inline',
379+
'config' => [
380+
'type' => 'text',
381+
'localizable' => true,
382+
],
383+
],
384+
],
385+
],
386+
],
387+
])
388+
->assertOk();
389+
390+
$this->assertEquals([
391+
'title' => 'Updated title',
392+
'sections' => [
393+
'one' => [
394+
'display' => 'Section One',
395+
'fields' => [
396+
[
397+
'handle' => 'title',
398+
'field' => [
399+
'type' => 'text',
400+
'required' => true,
401+
],
402+
],
403+
[
404+
'handle' => 'one-one',
405+
'field' => 'somefieldset.somefield',
406+
'config' => [
407+
'localizable' => false,
408+
],
409+
],
410+
[
411+
'handle' => 'one-two',
412+
'field' => [
413+
'type' => 'text',
414+
],
415+
],
416+
[
417+
'handle' => 'one-three',
418+
'field' => [
419+
'type' => 'text',
420+
'localizable' => true,
421+
],
422+
],
423+
],
424+
],
425+
'sidebar' => [
426+
'fields' => [
427+
[
428+
'handle' => 'slug',
429+
'field' => [
430+
'type' => 'slug',
431+
'localizable' => true,
432+
'required' => true,
433+
],
434+
],
435+
],
436+
],
437+
],
438+
], Facades\Blueprint::find('collections.test.test')->contents());
439+
}
440+
324441
private function submit($collection, $blueprint, $params = [])
325442
{
326443
return $this->patch(

0 commit comments

Comments
 (0)