Skip to content

Commit

Permalink
fix(resolveConfig): add prefix to field name if filter param specified
Browse files Browse the repository at this point in the history
  • Loading branch information
domtra committed Mar 7, 2017
1 parent 1eaf12a commit 0a49e75
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
11 changes: 10 additions & 1 deletion lib/ACFComposer/ResolveConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ protected static function forEntity($config, $requiredAttributes, $parentKeys =
$filterName = $config;
$filterParts = explode('#', $filterName);
if (isset($filterParts[1])) {
$config = apply_filters($filterParts[0], null, $filterParts[1]);
$prefix = $filterParts[1];
$config = apply_filters($filterParts[0], null, $prefix);
if (!self::isAssoc($config)) {
$config = array_map(function ($singleConfig) use ($prefix) {
$singleConfig['name'] = $prefix . '_' . $singleConfig['name'];
return $singleConfig;
}, $config);
} else {
$config['name'] = $prefix . '_' . $config['name'];
}
} else {
$config = apply_filters($filterName, null);
}
Expand Down
36 changes: 32 additions & 4 deletions tests/test-resolveConfigForField.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,48 @@ function testForFieldGetConfigFromFilter() {
$this->assertEquals($someField, $output);
}

function testForFieldGetConfigFromFilterWithArguments() {
$config = 'ACFComposer/Fields/someField#argument';
function testForFieldGetConfigFromFilterWithArgument() {
$config = 'ACFComposer/Fields/someField#prefix';
$filter = 'ACFComposer/Fields/someField';
$someField = [
'name' => 'someField',
'label' => 'Some Field',
'type' => 'someType'
];
Filters::expectApplied($filter)
->with(null, 'argument')
->with(null, 'prefix')
->once()
->andReturn($someField);
$output = ResolveConfig::forField($config);
$someField['key'] = "field_someField";
$someField['name'] = "prefix_someField";
$someField['key'] = "field_prefix_someField";
$this->assertEquals($someField, $output);
}

function testMultipleForFieldGetConfigFromFilterWithArgument() {
$config = 'ACFComposer/Fields/someField#prefix';
$filter = 'ACFComposer/Fields/someField';
$someField = [
[
'name' => 'someField',
'label' => 'Some Field',
'type' => 'someType'
],
[
'name' => 'someOtherField',
'label' => 'Some Field',
'type' => 'someType'
]
];
Filters::expectApplied($filter)
->with(null, 'prefix')
->once()
->andReturn($someField);
$output = ResolveConfig::forField($config);
$someField[0]['name'] = "prefix_someField";
$someField[0]['key'] = "field_prefix_someField";
$someField[1]['name'] = "prefix_someOtherField";
$someField[1]['key'] = "field_prefix_someOtherField";
$this->assertEquals($someField, $output);
}

Expand Down

0 comments on commit 0a49e75

Please sign in to comment.