From 028e3d640b2c6bcd6df6a13db57cceafcabf03e3 Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Tue, 5 Nov 2024 22:26:53 +0100 Subject: [PATCH 1/5] Disable new term creation on more than one taxonomy --- .../js/components/inputs/relationship/RelationshipInput.vue | 1 + resources/js/components/inputs/relationship/SelectField.vue | 2 ++ 2 files changed, 3 insertions(+) diff --git a/resources/js/components/inputs/relationship/RelationshipInput.vue b/resources/js/components/inputs/relationship/RelationshipInput.vue index 1616fd90de..39d1f0ece1 100644 --- a/resources/js/components/inputs/relationship/RelationshipInput.vue +++ b/resources/js/components/inputs/relationship/RelationshipInput.vue @@ -9,6 +9,7 @@ :multiple="maxItems > 1" :typeahead="mode === 'typeahead'" :taggable="taggable" + :creatables="creatables" :read-only="readOnly" :url="selectionsUrl" :site="site" diff --git a/resources/js/components/inputs/relationship/SelectField.vue b/resources/js/components/inputs/relationship/SelectField.vue index 4d31c01b7c..4701099550 100644 --- a/resources/js/components/inputs/relationship/SelectField.vue +++ b/resources/js/components/inputs/relationship/SelectField.vue @@ -74,6 +74,7 @@ export default { typeahead: Boolean, multiple: Boolean, taggable: Boolean, + creatables: Array, config: Object, readOnly: Boolean, site: String, @@ -88,6 +89,7 @@ export default { computed: { isTaggable() { if (data_get(this.config, 'create') === false) return false; + if (this.creatables?.length >= 2) return false; return this.taggable; }, From 1ffcfc553fa386deb40b2038bfca1dedae3102bc Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Tue, 5 Nov 2024 22:33:34 +0100 Subject: [PATCH 2/5] Prefix newly created relation items --- .../components/inputs/relationship/SelectField.vue | 12 +++++++++++- src/Fieldtypes/Terms.php | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/resources/js/components/inputs/relationship/SelectField.vue b/resources/js/components/inputs/relationship/SelectField.vue index 4701099550..35a8a06323 100644 --- a/resources/js/components/inputs/relationship/SelectField.vue +++ b/resources/js/components/inputs/relationship/SelectField.vue @@ -12,7 +12,7 @@ :options="options" :get-option-key="(option) => option.id" :get-option-label="(option) => __(option.title)" - :create-option="(value) => ({ title: value, id: value })" + :create-option="(value) => createOption(value)" :placeholder="__(config.placeholder) || __('Choose...')" :searchable="true" :taggable="isTaggable" @@ -142,6 +142,16 @@ export default { this.$emit('input', items); }, + createOption(title) { + const prefix = this.creatables?.length === 1 + ? this.creatables[0].prefix + : ''; + + const id = prefix + str_slug(title); + + return { id, title }; + }, + } } diff --git a/src/Fieldtypes/Terms.php b/src/Fieldtypes/Terms.php index 3b4770999e..7e5bf5e335 100644 --- a/src/Fieldtypes/Terms.php +++ b/src/Fieldtypes/Terms.php @@ -323,6 +323,7 @@ protected function getCreatables() return [ 'title' => $this->getCreatableTitle($taxonomy, $blueprint, count($taxonomies), $blueprints->count()), 'url' => cp_route('taxonomies.terms.create', [$taxonomy->handle(), Site::selected()->handle()]).'?blueprint='.$blueprint->handle(), + 'prefix' => $taxonomy->handle().'::', ]; }); })->all(); From 604d41ba05e6106f2725e5187bb91501d294f602 Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Tue, 5 Nov 2024 22:47:46 +0100 Subject: [PATCH 3/5] Re-use existing options --- .../js/components/inputs/relationship/SelectField.vue | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/resources/js/components/inputs/relationship/SelectField.vue b/resources/js/components/inputs/relationship/SelectField.vue index 35a8a06323..8119cd9858 100644 --- a/resources/js/components/inputs/relationship/SelectField.vue +++ b/resources/js/components/inputs/relationship/SelectField.vue @@ -142,14 +142,9 @@ export default { this.$emit('input', items); }, - createOption(title) { - const prefix = this.creatables?.length === 1 - ? this.creatables[0].prefix - : ''; - - const id = prefix + str_slug(title); - - return { id, title }; + createOption(value) { + const existing = this.options.find((option) => option.title === value); + return existing || { id: value, title: value }; }, } From db816dd0325b683113e877ba1de33c28c393de58 Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Tue, 5 Nov 2024 22:50:56 +0100 Subject: [PATCH 4/5] Remove prefix --- src/Fieldtypes/Terms.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Fieldtypes/Terms.php b/src/Fieldtypes/Terms.php index 7e5bf5e335..3b4770999e 100644 --- a/src/Fieldtypes/Terms.php +++ b/src/Fieldtypes/Terms.php @@ -323,7 +323,6 @@ protected function getCreatables() return [ 'title' => $this->getCreatableTitle($taxonomy, $blueprint, count($taxonomies), $blueprints->count()), 'url' => cp_route('taxonomies.terms.create', [$taxonomy->handle(), Site::selected()->handle()]).'?blueprint='.$blueprint->handle(), - 'prefix' => $taxonomy->handle().'::', ]; }); })->all(); From 784dcd57a040222f9d6cbec6b013a3603b6f2705 Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Tue, 5 Nov 2024 23:01:48 +0100 Subject: [PATCH 5/5] Remove creatables --- .../js/components/inputs/relationship/RelationshipInput.vue | 1 - resources/js/components/inputs/relationship/SelectField.vue | 2 -- 2 files changed, 3 deletions(-) diff --git a/resources/js/components/inputs/relationship/RelationshipInput.vue b/resources/js/components/inputs/relationship/RelationshipInput.vue index 39d1f0ece1..1616fd90de 100644 --- a/resources/js/components/inputs/relationship/RelationshipInput.vue +++ b/resources/js/components/inputs/relationship/RelationshipInput.vue @@ -9,7 +9,6 @@ :multiple="maxItems > 1" :typeahead="mode === 'typeahead'" :taggable="taggable" - :creatables="creatables" :read-only="readOnly" :url="selectionsUrl" :site="site" diff --git a/resources/js/components/inputs/relationship/SelectField.vue b/resources/js/components/inputs/relationship/SelectField.vue index 8119cd9858..b4a23931b3 100644 --- a/resources/js/components/inputs/relationship/SelectField.vue +++ b/resources/js/components/inputs/relationship/SelectField.vue @@ -74,7 +74,6 @@ export default { typeahead: Boolean, multiple: Boolean, taggable: Boolean, - creatables: Array, config: Object, readOnly: Boolean, site: String, @@ -89,7 +88,6 @@ export default { computed: { isTaggable() { if (data_get(this.config, 'create') === false) return false; - if (this.creatables?.length >= 2) return false; return this.taggable; },