diff --git a/src/ConditionalLogic.php b/src/ConditionalLogic.php index 306e7bd8..0632843c 100644 --- a/src/ConditionalLogic.php +++ b/src/ConditionalLogic.php @@ -23,16 +23,23 @@ public function __construct( string $name, string $operator, mixed $value = null, - string $group = null + string $group = null, + string $key = null, ) { - $this->rules[] = $this->rule($name, $operator, $value, $group); + $this->rules[] = [ + 'name' => $name, + 'operator' => $operator, + 'value' => $value, + 'group' => $group, + 'key' => $key, + ]; } /** * @param string $operator `==` is equal to, `!=` is not equal to, `>` is greater than, `<` is less than, `==pattern` matches pattern, `==contains` contains value, `==empty` has no value, `!=empty` has any value * @throws \InvalidArgumentException */ - public static function where(string $name, string $operator, mixed $value = null, string $group = null): static + public static function where(string $name, string $operator, mixed $value = null, string $group = null, string $key = null): static { $allowedOperators = [ '>', @@ -49,24 +56,20 @@ public static function where(string $name, string $operator, mixed $value = null throw new InvalidArgumentException("Invalid conditional logic operator [$operator]."); } - return new self($name, $operator, $value, $group); - } - - public function and(string|array $name, string $operator, mixed $value = null, string $group = null): static - { - $this->rules[] = $this->rule($name, $operator, $value, $group); - - return $this; + return new self($name, $operator, $value, $group, $key); } - private function rule(string|array $name, string $operator, mixed $value = null, string $group = null): array + public function and(string|array $name, string $operator, mixed $value = null, string $group = null, string $key = null): static { - return [ + $this->rules[] = [ 'name' => $name, 'operator' => $operator, 'value' => $value, 'group' => $group, + 'field' => $key, ]; + + return $this; } /** @internal */ @@ -79,7 +82,7 @@ public function get(string|null $parentKey = null): array $key = $resolvedParentKey . '_' . Key::sanitize($rule['name']); $newRule = [ - 'field' => 'field_' . Key::hash($key), + 'field' => $rule['key'] ?? 'field_' . Key::hash($key), 'operator' => $rule['operator'], ]; diff --git a/src/Fields/Field.php b/src/Fields/Field.php index c4d676c4..f792555d 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -110,21 +110,21 @@ public function get(string|null $parentKey = null): array if (isset($this->settings['conditional_logic'])) { $this->settings['conditional_logic'] = array_map( - fn ($rules) => $rules->get($parentKey), + fn($rules) => $rules->get($parentKey), $this->settings['conditional_logic'] ); } if (isset($this->settings['layouts'])) { $this->settings['layouts'] = array_map( - fn ($layout) => $layout->get($key), + fn($layout) => $layout->get($key), $this->settings['layouts'] ); } if (isset($this->settings['sub_fields'])) { $this->settings['sub_fields'] = array_map( - fn ($field) => $field->get($key), + fn($field) => $field->get($key), $this->settings['sub_fields'] ); } diff --git a/src/Fields/Settings/Choices.php b/src/Fields/Settings/Choices.php index d02f9a7c..5eb74956 100644 --- a/src/Fields/Settings/Choices.php +++ b/src/Fields/Settings/Choices.php @@ -23,7 +23,7 @@ trait Choices public function choices(array $choices): static { if (array_is_list($choices)) { - $choices = array_combine(array_map(fn ($key) => Key::sanitize($key), $choices), $choices); + $choices = array_combine(array_map(fn($key) => Key::sanitize($key), $choices), $choices); } $this->settings['choices'] = $choices; diff --git a/src/helpers.php b/src/helpers.php index d02b7169..20569411 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -32,9 +32,9 @@ function register_extended_field_group(array $settings): array $settings['style'] ??= 'seamless'; - $settings['fields'] = array_map(fn ($field) => $field->get($key), $settings['fields']); + $settings['fields'] = array_map(fn($field) => $field->get($key), $settings['fields']); - $settings['location'] = array_map(fn ($location) => $location->get(), $settings['location']); + $settings['location'] = array_map(fn($location) => $location->get(), $settings['location']); $settings['key'] = Key::generate($key, 'group'); diff --git a/tests/ConditionalLogicTest.php b/tests/ConditionalLogicTest.php index 656eca6f..872e3af1 100644 --- a/tests/ConditionalLogicTest.php +++ b/tests/ConditionalLogicTest.php @@ -69,4 +69,31 @@ public function testResolvedParentKey() $settings['fields'][1]['sub_fields'][0]['conditional_logic'][0][0]['field'] ); } + + public function testFieldKey() + { + $settings = register_extended_field_group([ + 'title' => 'Field Key', + 'fields' => [ + Select::make('Select') + ->key('field_123abc') + ->choices(['Red']), + Text::make('Text') + ->conditionalLogic([ + ConditionalLogic::where( + name: 'select', + operator: '==', + value: 'red', + key: 'field_123abc' + ), + ]), + ], + 'location' => [] + ]); + + $this->assertSame( + 'field_123abc', + $settings['fields'][1]['conditional_logic'][0][0]['field'] + ); + } }