From 0a49e75f1ef75f3df5d2bb9bf020337d36c24244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Tr=C3=A4nklein?= Date: Tue, 7 Mar 2017 16:30:46 +0100 Subject: [PATCH] fix(resolveConfig): add prefix to field name if filter param specified --- lib/ACFComposer/ResolveConfig.php | 11 ++++++++- tests/test-resolveConfigForField.php | 36 ++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/lib/ACFComposer/ResolveConfig.php b/lib/ACFComposer/ResolveConfig.php index 27e0ab8..e4aa938 100644 --- a/lib/ACFComposer/ResolveConfig.php +++ b/lib/ACFComposer/ResolveConfig.php @@ -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); } diff --git a/tests/test-resolveConfigForField.php b/tests/test-resolveConfigForField.php index 8a4fc40..03e703c 100644 --- a/tests/test-resolveConfigForField.php +++ b/tests/test-resolveConfigForField.php @@ -72,8 +72,8 @@ 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', @@ -81,11 +81,39 @@ function testForFieldGetConfigFromFilterWithArguments() { '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); }