From eaa228d0d47fba34fdf52085dfcbf2bff14bf964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Tr=C3=A4nklein?= Date: Thu, 10 Nov 2016 18:13:26 +0100 Subject: [PATCH] feat(ResolveConfig): for conditional logic with field on same level --- lib/ACFComposer/ResolveConfig.php | 17 +++++++++++ phpcs.ruleset.xml | 1 - tests/test-resolveConfigForField.php | 44 ++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/lib/ACFComposer/ResolveConfig.php b/lib/ACFComposer/ResolveConfig.php index a0974a5..08daef6 100644 --- a/lib/ACFComposer/ResolveConfig.php +++ b/lib/ACFComposer/ResolveConfig.php @@ -40,6 +40,8 @@ protected static function forEntity($config, $requiredAttributes, $parentKeySuff : "{$parentKeySuffix}_{$output['name']}"; $output['key'] = "field_{$keySuffix}"; + $output = self::forConditionalLogic($output, $parentKeySuffix); + $output = apply_filters('ACFComposer/resolveEntity', $output); $output = apply_filters("ACFComposer/resolveEntity?name={$output['name']}", $output); $output = apply_filters("ACFComposer/resolveEntity?key={$output['key']}", $output); @@ -76,4 +78,19 @@ protected static function validateConfig($config, $requiredAttributes = []) { protected static function mapLocation($locationArray) { return array_map('self::forLocation', $locationArray); } + + protected static function forConditionalLogic($config, $keySuffix) { + if (array_key_exists('conditional_logic', $config)) { + $config['conditional_logic'] = array_map(function ($conditionGroup) use ($keySuffix) { + return array_map(function ($condition) use ($keySuffix) { + if (array_key_exists('fieldPath', $condition)) { + $condition['field'] = "field_{$keySuffix}_{$condition['fieldPath']}"; + unset($condition['fieldPath']); + } + return $condition; + }, $conditionGroup); + }, $config['conditional_logic']); + } + return $config; + } } diff --git a/phpcs.ruleset.xml b/phpcs.ruleset.xml index d00a921..7170532 100644 --- a/phpcs.ruleset.xml +++ b/phpcs.ruleset.xml @@ -40,5 +40,4 @@ - diff --git a/tests/test-resolveConfigForField.php b/tests/test-resolveConfigForField.php index ab20e6a..c860629 100644 --- a/tests/test-resolveConfigForField.php +++ b/tests/test-resolveConfigForField.php @@ -172,4 +172,48 @@ function testAppliesResolveFilterByKey() { $resolvedConfig['foo'] = 'bar'; $this->assertEquals($resolvedConfig, $output); } + + function testResolveConditionalLogicOnSameLevel() { + $subFieldOne = [ + 'name' => 'subField1', + 'label' => 'Sub Field 1', + 'type' => 'someType', + ]; + $subFieldWithConditional = [ + 'name' => 'subField2', + 'label' => 'Sub Field 2', + 'type' => 'someType', + 'conditional_logic' => [ + [ + [ + 'fieldPath' => 'subField1', + 'operator' => 'someOp', + 'value' => 'someValue' + ] + ] + ] + ]; + $config = [ + 'name' => 'someField', + 'label' => 'Some Field', + 'type' => 'someType', + 'sub_fields' => [ + $subFieldOne, + $subFieldWithConditional + ] + ]; + + $output = ResolveConfig::forField($config); + + $config['key'] = 'field_someField'; + $subFieldOne['key'] = 'field_someField_subField1'; + $subFieldWithConditional['key'] = 'field_someField_subField2'; + $subFieldWithConditional['conditional_logic'][0][0]['field'] = 'field_someField_subField1'; + unset($subFieldWithConditional['conditional_logic'][0][0]['fieldPath']); + $config['sub_fields'] = [ + $subFieldOne, + $subFieldWithConditional + ]; + $this->assertEquals($config, $output); + } }