Skip to content

Commit

Permalink
feat(ResolveConfig): for conditional logic with field on same level
Browse files Browse the repository at this point in the history
  • Loading branch information
domtra committed Nov 10, 2016
1 parent 158b1eb commit eaa228d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
17 changes: 17 additions & 0 deletions lib/ACFComposer/ResolveConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
1 change: 0 additions & 1 deletion phpcs.ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,4 @@
</properties>
</rule>


</ruleset>
44 changes: 44 additions & 0 deletions tests/test-resolveConfigForField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit eaa228d

Please sign in to comment.