From 2b44e40f554246b7cd23a23ed2f6bde1373e76b6 Mon Sep 17 00:00:00 2001 From: Daniel Weaver Date: Tue, 2 Apr 2024 13:47:04 -0400 Subject: [PATCH 1/3] Adding replicator set label option --- .../fieldtypes/replicator/AddSetButton.vue | 16 +++++++++++--- .../fieldtypes/replicator/Replicator.vue | 1 + resources/lang/en/fieldtypes.php | 1 + src/Fieldtypes/Replicator.php | 22 ++++++++++++------- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/resources/js/components/fieldtypes/replicator/AddSetButton.vue b/resources/js/components/fieldtypes/replicator/AddSetButton.vue index d3258f60ea..8b43f1eaab 100644 --- a/resources/js/components/fieldtypes/replicator/AddSetButton.vue +++ b/resources/js/components/fieldtypes/replicator/AddSetButton.vue @@ -3,14 +3,23 @@
@@ -33,6 +42,7 @@ export default { index: Number, last: Boolean, enabled: { type: Boolean, default: true }, + label: String, }, methods: { diff --git a/resources/js/components/fieldtypes/replicator/Replicator.vue b/resources/js/components/fieldtypes/replicator/Replicator.vue index 3f7d4cb216..a45051457a 100644 --- a/resources/js/components/fieldtypes/replicator/Replicator.vue +++ b/resources/js/components/fieldtypes/replicator/Replicator.vue @@ -98,6 +98,7 @@ :groups="groupConfigs" :sets="setConfigs" :index="value.length" + :label="config.button_label" @added="addSet" /> diff --git a/resources/lang/en/fieldtypes.php b/resources/lang/en/fieldtypes.php index 9c2d9a928c..d934f16149 100644 --- a/resources/lang/en/fieldtypes.php +++ b/resources/lang/en/fieldtypes.php @@ -129,6 +129,7 @@ 'range.config.step' => 'The minimum size between values.', 'range.title' => 'Range', 'relationship.config.mode' => 'Choose your preferred UI style.', + 'replicator.config.button_label' => 'Add a label to the add set button.', 'replicator.config.collapse' => 'The set collapsing behavior.', 'replicator.config.collapse.accordion' => 'Only allow one set to be expanded at a time', 'replicator.config.collapse.disabled' => 'All sets expanded by default', diff --git a/src/Fieldtypes/Replicator.php b/src/Fieldtypes/Replicator.php index a437022e07..19f78b4812 100644 --- a/src/Fieldtypes/Replicator.php +++ b/src/Fieldtypes/Replicator.php @@ -54,6 +54,12 @@ protected function configFieldItems(): array 'type' => 'toggle', 'default' => true, ], + 'button_label' => [ + 'display' => __('Add Set Label'), + 'instructions' => __('statamic::fieldtypes.replicator.config.button_label'), + 'type' => 'text', + 'default' => '', + ], ], ], [ @@ -137,12 +143,12 @@ protected function setRules($handle, $data, $index) ->addValues($data) ->validator() ->withContext([ - 'prefix' => $this->field->validationContext('prefix').$this->setRuleFieldPrefix($index).'.', + 'prefix' => $this->field->validationContext('prefix') . $this->setRuleFieldPrefix($index) . '.', ]) ->rules(); return collect($rules)->mapWithKeys(function ($rules, $handle) use ($index) { - return [$this->setRuleFieldPrefix($index).'.'.$handle => $rules]; + return [$this->setRuleFieldPrefix($index) . '.' . $handle => $rules]; })->all(); } @@ -165,7 +171,7 @@ protected function setValidationAttributes($handle, $data, $index) $attributes = $this->fields($handle, $index)->addValues($data)->validator()->attributes(); return collect($attributes)->mapWithKeys(function ($attribute, $handle) use ($index) { - return [$this->setRuleFieldPrefix($index).'.'.$handle => $attribute]; + return [$this->setRuleFieldPrefix($index) . '.' . $handle => $attribute]; })->all(); } @@ -184,7 +190,7 @@ protected function performAugmentation($values, $shallow) return collect($values)->reject(function ($set, $key) { return array_get($set, 'enabled', true) === false; })->map(function ($set, $index) use ($shallow) { - if (! Arr::get($this->flattenedSetsConfig(), "{$set['type']}.fields")) { + if (!Arr::get($this->flattenedSetsConfig(), "{$set['type']}.fields")) { return $set; } @@ -234,7 +240,7 @@ public function flattenedSetsConfig() // If the first set doesn't have a nested "set" key, it would be the legacy format. // We'll put it in a "main" group so it's compatible with the new format. // This also happens in the "sets" fieldtype. - if (! Arr::has($sets->first(), 'sets')) { + if (!Arr::has($sets->first(), 'sets')) { $sets = collect([ 'main' => [ 'sets' => $sets->all(), @@ -279,14 +285,14 @@ public function addGqlTypes() protected function gqlSetTypeName($set) { - return 'Set_'.collect($this->field->handlePath())->map(function ($part) { + return 'Set_' . collect($this->field->handlePath())->map(function ($part) { return Str::studly($part); - })->join('_').'_'.Str::studly($set); + })->join('_') . '_' . Str::studly($set); } protected function gqlSetsTypeName() { - return 'Sets_'.collect($this->field->handlePath())->map(function ($part) { + return 'Sets_' . collect($this->field->handlePath())->map(function ($part) { return Str::studly($part); })->join('_'); } From 87d0abbff6c13137e9a2b0eccece4eccc1a8585f Mon Sep 17 00:00:00 2001 From: Daniel Weaver Date: Tue, 2 Apr 2024 13:57:02 -0400 Subject: [PATCH 2/3] Reverting autoformatting --- src/Fieldtypes/Replicator.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Fieldtypes/Replicator.php b/src/Fieldtypes/Replicator.php index 19f78b4812..e1bf392cf4 100644 --- a/src/Fieldtypes/Replicator.php +++ b/src/Fieldtypes/Replicator.php @@ -143,12 +143,12 @@ protected function setRules($handle, $data, $index) ->addValues($data) ->validator() ->withContext([ - 'prefix' => $this->field->validationContext('prefix') . $this->setRuleFieldPrefix($index) . '.', + 'prefix' => $this->field->validationContext('prefix').$this->setRuleFieldPrefix($index).'.', ]) ->rules(); return collect($rules)->mapWithKeys(function ($rules, $handle) use ($index) { - return [$this->setRuleFieldPrefix($index) . '.' . $handle => $rules]; + return [$this->setRuleFieldPrefix($index).'.'.$handle => $rules]; })->all(); } @@ -171,7 +171,7 @@ protected function setValidationAttributes($handle, $data, $index) $attributes = $this->fields($handle, $index)->addValues($data)->validator()->attributes(); return collect($attributes)->mapWithKeys(function ($attribute, $handle) use ($index) { - return [$this->setRuleFieldPrefix($index) . '.' . $handle => $attribute]; + return [$this->setRuleFieldPrefix($index).'.'.$handle => $attribute]; })->all(); } @@ -190,7 +190,7 @@ protected function performAugmentation($values, $shallow) return collect($values)->reject(function ($set, $key) { return array_get($set, 'enabled', true) === false; })->map(function ($set, $index) use ($shallow) { - if (!Arr::get($this->flattenedSetsConfig(), "{$set['type']}.fields")) { + if (! Arr::get($this->flattenedSetsConfig(), "{$set['type']}.fields")) { return $set; } @@ -240,7 +240,7 @@ public function flattenedSetsConfig() // If the first set doesn't have a nested "set" key, it would be the legacy format. // We'll put it in a "main" group so it's compatible with the new format. // This also happens in the "sets" fieldtype. - if (!Arr::has($sets->first(), 'sets')) { + if (! Arr::has($sets->first(), 'sets')) { $sets = collect([ 'main' => [ 'sets' => $sets->all(), @@ -285,14 +285,14 @@ public function addGqlTypes() protected function gqlSetTypeName($set) { - return 'Set_' . collect($this->field->handlePath())->map(function ($part) { + return 'Set_'.collect($this->field->handlePath())->map(function ($part) { return Str::studly($part); - })->join('_') . '_' . Str::studly($set); + })->join('_').'_'.Str::studly($set); } protected function gqlSetsTypeName() { - return 'Sets_' . collect($this->field->handlePath())->map(function ($part) { + return 'Sets_'.collect($this->field->handlePath())->map(function ($part) { return Str::studly($part); })->join('_'); } @@ -314,4 +314,4 @@ public function toQueryableValue($value) { return empty($value) ? null : $value; } -} +} \ No newline at end of file From 0335cfc1d094dcb41bcdd95fd10d364e0a38d003 Mon Sep 17 00:00:00 2001 From: Daniel Weaver Date: Tue, 2 Apr 2024 14:29:39 -0400 Subject: [PATCH 3/3] Code style fix --- src/Fieldtypes/Replicator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fieldtypes/Replicator.php b/src/Fieldtypes/Replicator.php index e1bf392cf4..0517c66867 100644 --- a/src/Fieldtypes/Replicator.php +++ b/src/Fieldtypes/Replicator.php @@ -314,4 +314,4 @@ public function toQueryableValue($value) { return empty($value) ? null : $value; } -} \ No newline at end of file +}