Skip to content

Commit

Permalink
Add the localizable field toggler. Strip out localizable: false sin…
Browse files Browse the repository at this point in the history
…ce it's the default. Closes #2045
  • Loading branch information
jasonvarga committed Aug 26, 2020
1 parent a601ce0 commit 77acada
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

### What's new
- Localizable field toggle. [#2045](https://github.com/statamic/cms/issues/2045)
- The `form` tags get a `submission_created` boolean. [#2285](https://github.com/statamic/cms/issues/2285)
- The `template` fieldtype will ignore views in the `partials` directory when `hide_partials` is enabled. [#2249](https://github.com/statamic/cms/issues/2249)
- 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)
Expand Down
4 changes: 4 additions & 0 deletions resources/js/components/blueprints/Fields.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
:is-editing="editingField === field._id"
:is-section-expanded="isSectionExpanded"
:suggestable-condition-fields="suggestableConditionFields"
:can-define-localizable="canDefineLocalizable"
@edit="$emit('field-editing', field._id)"
@updated="$emit('field-updated', i, $event)"
@deleted="$emit('field-deleted', i)"
Expand Down Expand Up @@ -70,9 +71,12 @@ import ImportField from './ImportField.vue';
import LinkFields from './LinkFields.vue';
import FieldtypeSelector from '../fields/FieldtypeSelector.vue';
import FieldSettings from '../fields/Settings.vue';
import CanDefineLocalizable from '../fields/CanDefineLocalizable';
export default {
mixins: [CanDefineLocalizable],
components: {
RegularField,
ImportField,
Expand Down
27 changes: 24 additions & 3 deletions resources/js/components/blueprints/RegularField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
</div>
<div class="pr-1 flex">
<width-selector v-model="width" class="mr-1" />
<button v-if="canDefineLocalizable"
class="hover:text-grey-100 mr-1"
:class="{ 'text-grey-100': localizable, 'text-grey-60': !localizable }"
v-tooltip="__('Localizable')"
@click="localizable = !localizable"
>
<svg-icon name="earth" />
</button>
<button @click.prevent="$emit('deleted')" class="text-grey-60 hover:text-grey-100"><svg-icon name="trash" /></button>
<stack name="field-settings" v-if="isEditing" @closed="editorClosed">
<field-settings
Expand All @@ -35,10 +43,11 @@
import Field from './Field.vue';
import FieldSettings from '../fields/Settings.vue';
import WidthSelector from '../fields/WidthSelector.vue';
import CanDefineLocalizable from '../fields/CanDefineLocalizable';
export default {
mixins: [Field],
mixins: [Field, CanDefineLocalizable],
components: {
FieldSettings,
Expand All @@ -51,7 +60,7 @@ export default {
data() {
return {
showHandle: false
showHandle: false,
}
},
Expand Down Expand Up @@ -96,7 +105,19 @@ export default {
if (! this.isSectionExpanded) return 'blueprint-section-field-w-full';
return `blueprint-section-field-${tailwind_width_class(this.width)}`;
}
},
localizable: {
get() {
return this.field.config.localizable || false;
},
set(localizable) {
let field = this.field;
field.config.localizable = localizable;
if (field.type === 'reference') field.config_overrides.push('localizable');
this.$emit('updated', field);
}
},
},
methods: {
Expand Down
4 changes: 4 additions & 0 deletions resources/js/components/blueprints/Section.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
:editing-field="editingField"
:is-section-expanded="isEditing || isSingle"
:suggestable-condition-fields="suggestableConditionFields"
:can-define-localizable="canDefineLocalizable"
@field-created="fieldCreated"
@field-updated="fieldUpdated"
@field-deleted="deleteField"
Expand All @@ -54,9 +55,12 @@

<script>
import Fields from './Fields.vue';
import CanDefineLocalizable from '../fields/CanDefineLocalizable';
export default {
mixins: [CanDefineLocalizable],
components: {
Fields,
},
Expand Down
4 changes: 4 additions & 0 deletions resources/js/components/blueprints/Sections.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
:key="section._id"
:section="section"
:is-single="singleSection"
:can-define-localizable="canDefineLocalizable"
:deletable="isSectionDeletable(i)"
@updated="updateSection(i, $event)"
@deleted="deleteSection(i)"
Expand All @@ -36,11 +37,14 @@
import uniqid from 'uniqid';
import BlueprintSection from './Section.vue';
import {Sortable, Plugins} from '@shopify/draggable';
import CanDefineLocalizable from '../fields/CanDefineLocalizable';
let sortableSections, sortableFields;
export default {
mixins: [CanDefineLocalizable],
components: {
BlueprintSection
},
Expand Down
12 changes: 12 additions & 0 deletions resources/js/components/fields/CanDefineLocalizable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {

props: {
canDefineLocalizable: {
type: Boolean,
default: () => {
return Statamic.$config.get('sites').length > 1;
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<fields
:fields="fields"
:editing-field="editingField"
:can-define-localizable="false"
@field-created="fieldCreated"
@field-updated="fieldUpdated"
@field-linked="fieldLinked"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<sections
:initial-sections="sections"
:require-section="config.require_section"
:can-define-localizable="false"
:add-section-text="__('Add Set')"
:new-section-text="__('New Set')"
@updated="sectionsUpdated"
Expand Down
6 changes: 6 additions & 0 deletions src/Fields/FieldTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ private static function inlineSectionField(array $submitted)
unset($field['width']);
}

if (Arr::get($field, 'localizable', false) === false) {
unset($field['localizable']);
}

return array_filter([
'handle' => $submitted['handle'],
'field' => $field,
Expand Down Expand Up @@ -69,6 +73,7 @@ private static function referenceFieldToVue($field): array
);

$mergedConfig['width'] = $mergedConfig['width'] ?? 100;
$mergedConfig['localizable'] = $mergedConfig['localizable'] ?? false;

return [
'handle' => $field['handle'],
Expand All @@ -85,6 +90,7 @@ private static function inlineFieldToVue($field): array
{
$config = $field['field'];
$config['width'] = $config['width'] ?? 100;
$config['localizable'] = $config['localizable'] ?? false;

return [
'handle' => $field['handle'],
Expand Down
117 changes: 117 additions & 0 deletions tests/Feature/Collections/Blueprints/UpdateBlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,123 @@ public function width_of_100_gets_stripped_out_for_inline_fields_but_left_in_for
], Facades\Blueprint::find('collections.test.test')->contents());
}

/** @test */
public function localizable_of_false_gets_stripped_out_for_inline_fields_but_left_in_for_reference_fields_with_config_overrides()
{
$this->setTestRoles(['test' => ['access cp', 'configure fields']]);
$user = tap(Facades\User::make()->assignRole('test'))->save();
$collection = tap(Collection::make('test'))->save();
$blueprint = (new Blueprint)->setNamespace('collections.test')->setHandle('test')->setContents(['title' => 'Test'])->save();

$fieldset = (new Fieldset)->setContents([
'fields' => [
[
'handle' => 'somefield',
'field' => [],
],
],
]);

Facades\Fieldset::shouldReceive('find')
->with('somefieldset')
->andReturn($fieldset);

$this
->actingAs($user)
->submit($collection, $blueprint, [
'title' => 'Updated title',
'sections' => [
[
'_id' => 'id-one',
'handle' => 'one',
'display' => 'Section One',
'fields' => [
[
'_id' => 'id-s1-f1',
'handle' => 'one-one',
'type' => 'reference',
'field_reference' => 'somefieldset.somefield',
'config' => [
'foo' => 'bar',
'localizable' => false,
],
'config_overrides' => ['localizable'],
],
[
'_id' => 'id-s1-f2',
'handle' => 'one-two',
'type' => 'inline',
'config' => [
'type' => 'text',
'localizable' => false,
],
],
[
'_id' => 'id-s1-f3',
'handle' => 'one-three',
'type' => 'inline',
'config' => [
'type' => 'text',
'localizable' => true,
],
],
],
],
],
])
->assertOk();

$this->assertEquals([
'title' => 'Updated title',
'sections' => [
'one' => [
'display' => 'Section One',
'fields' => [
[
'handle' => 'title',
'field' => [
'type' => 'text',
'required' => true,
],
],
[
'handle' => 'one-one',
'field' => 'somefieldset.somefield',
'config' => [
'localizable' => false,
],
],
[
'handle' => 'one-two',
'field' => [
'type' => 'text',
],
],
[
'handle' => 'one-three',
'field' => [
'type' => 'text',
'localizable' => true,
],
],
],
],
'sidebar' => [
'fields' => [
[
'handle' => 'slug',
'field' => [
'type' => 'slug',
'localizable' => true,
'required' => true,
],
],
],
],
],
], Facades\Blueprint::find('collections.test.test')->contents());
}

private function submit($collection, $blueprint, $params = [])
{
return $this->patch(
Expand Down
2 changes: 2 additions & 0 deletions tests/Fieldtypes/NestedFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public function it_preprocesses_from_blueprint_format_to_vue()
'type' => 'text',
'display' => 'First Field',
'width' => 100,
'localizable' => false,
],
'fieldtype' => 'text',
'icon' => 'text',
Expand All @@ -141,6 +142,7 @@ public function it_preprocesses_from_blueprint_format_to_vue()
'component' => 'text',
'width' => 50,
'display' => 'Second Field',
'localizable' => false,
],
'config_overrides' => ['width', 'display'],
'fieldtype' => 'text',
Expand Down

0 comments on commit 77acada

Please sign in to comment.