Skip to content

Commit

Permalink
Form block name fixes remake (sonata-project#5825)
Browse files Browse the repository at this point in the history
  • Loading branch information
klavig authored Feb 12, 2020
1 parent 04f8151 commit b35b794
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 8 deletions.
5 changes: 1 addition & 4 deletions src/Action/AppendFormFieldElementAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ public function __invoke(Request $request): Response

$subject = $admin->getObject($objectId);
if ($objectId && !$subject) {
throw new NotFoundHttpException(sprintf(
'Could not find subject for id "%s"',
$objectId
));
throw new NotFoundHttpException(sprintf('Could not find subject for id "%s"', $objectId));
}

if (!$subject) {
Expand Down
8 changes: 4 additions & 4 deletions src/Form/Extension/Field/Type/FormTypeFieldExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$baseName = str_replace('.', '_', $view->parent->vars['sonata_admin_code']);

$baseType = $blockPrefixes[\count($blockPrefixes) - 2];
$blockSuffix = preg_replace('#^_([a-z0-9]{14})_(.++)$#', '$2', array_pop($blockPrefixes));
$blockSuffix = preg_replace('#^_([a-z0-9]{14})_(.++)$#', '$2', end($blockPrefixes));

$blockPrefixes[] = sprintf('%s_%s', $baseName, $baseType);
$blockPrefixes[] = sprintf('%s_%s_%s_%s', $baseName, $baseType, $view->parent->vars['name'], $view->vars['name']);
$blockPrefixes[] = sprintf('%s_%s_%s_%s', $baseName, $baseType, $view->parent->vars['name'], $blockSuffix);

$view->vars['block_prefixes'] = $blockPrefixes;
$view->vars['block_prefixes'] = array_unique($blockPrefixes);
$view->vars['sonata_admin_enabled'] = true;
$view->vars['sonata_admin'] = [
'admin' => false,
Expand All @@ -127,7 +127,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$blockPrefixes = $view->vars['block_prefixes'];
$baseName = str_replace('.', '_', $sonataAdmin['admin']->getCode());
$baseType = $blockPrefixes[\count($blockPrefixes) - 2];
$blockSuffix = preg_replace('#^_([a-z0-9]{14})_(.++)$#', '$2', array_pop($blockPrefixes));
$blockSuffix = preg_replace('#^_([a-z0-9]{14})_(.++)$#', '$2', end($blockPrefixes));

$blockPrefixes[] = sprintf('%s_%s', $baseName, $baseType);
$blockPrefixes[] = sprintf('%s_%s_%s', $baseName, $sonataAdmin['name'], $baseType);
Expand All @@ -137,7 +137,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$blockPrefixes[] = $sonataAdmin['block_name'];
}

$view->vars['block_prefixes'] = $blockPrefixes;
$view->vars['block_prefixes'] = array_unique($blockPrefixes);
$view->vars['sonata_admin_enabled'] = true;
$view->vars['sonata_admin'] = $sonataAdmin;
$view->vars['sonata_admin_code'] = $sonataAdmin['admin']->getCode();
Expand Down
81 changes: 81 additions & 0 deletions tests/Form/Extension/Field/Type/FormTypeFieldExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public function testbuildViewWithWithSonataAdmin(): void
'form',
'field',
'text',
'_s50b26aa76cb96_username',
'my_admin_reference_text',
'my_admin_reference_name_text',
'my_admin_reference_name_text_username',
Expand Down Expand Up @@ -163,6 +164,7 @@ public function testbuildViewWithNestedForm(): void
'form',
'field',
'text',
'_s50b26aa76cb96_settings_format',
'parent_code_text',
'parent_code_text_settings_format',
'parent_code_text_settings_settings_format',
Expand Down Expand Up @@ -203,6 +205,85 @@ public function testbuildViewWithNestedFormWithNoParent(): void
$this->assertArrayHasKey('sonata_admin', $formView->vars);
}


public function testbuildViewCollectionField(): void
{
$eventDispatcher = $this->getMockForAbstractClass(EventDispatcherInterface::class);

$formView = new FormView();
$formView->vars['name'] = 'field';
$formView->vars['attr'] = ['hidden' => true];
$formView->vars['block_prefixes'] = [
'form',
'field',
'checkbox',
'_parent_collection_entry_field',
];
$formView->vars['unique_block_prefix'] = '_parent_collection_entry_field';
$formView->vars['sonata_admin_enabled'] = true;
$formView->vars['sonata_admin_code'] = 'admin.parent';

$formView->parent = new FormView();
$formView->parent->vars['name'] = '0';
$formView->parent->vars['block_prefixes'] = [
'form',
'parent_specification',
'_parent_collection_entry',
'admin_parent_parent_field',
'admin_parent_parent_field_collection_0',
'admin_parent_parent_field_collection__parent_collection_entry',
];
$formView->parent->vars['unique_block_prefix'] = '_parent_collection_entry';
$formView->parent->vars['sonata_admin_enabled'] = true;
$formView->parent->vars['sonata_admin_code'] = 'admin.parent';

$formView->parent->parent = new FormView();
$formView->parent->parent->vars['name'] = 'collection';
$formView->parent->parent->vars['block_prefixes'] = [
'form',
'collection',
'sonata_type_native_collection',
'_parent_collection',
'admin_parent_sonata_type_native_collection',
'admin_parent_collection_sonata_type_native_collection',
'admin_parent_collection_sonata_type_native_collection__parent_collection',
'field_collection',
];
$formView->parent->parent->vars['unique_block_prefix'] = '_parent_collection';
$formView->parent->parent->vars['sonata_admin_enabled'] = false;
$formView->parent->parent->vars['sonata_admin_code'] = 'admin.parent';

$formView->parent->parent->parent = new FormView();
$formView->parent->parent->parent->vars['name'] = 'parent';
$formView->parent->parent->parent->vars['block_prefixes'] = [
'form',
'_parent',
];
$formView->parent->parent->parent->vars['unique_block_prefix'] = '_parent';
$formView->parent->parent->parent->vars['sonata_admin_enabled'] = false;

$options = [];
$config = new FormConfigBuilder('test', 'stdClass', $eventDispatcher, $options);
$config->setAttribute('sonata_admin', ['admin' => false]);

$form = new Form($config);

$extension = new FormTypeFieldExtension([], []);
$extension->buildView($formView, $form, []);

$expected = [
'form',
'field',
'checkbox',
'_parent_collection_entry_field',
'admin_parent_checkbox',
'admin_parent_checkbox_0_field',
'admin_parent_checkbox_0__parent_collection_entry_field',
];

$this->assertSame($expected, $formView->vars['block_prefixes']);
}

/**
* @group legacy
*/
Expand Down

0 comments on commit b35b794

Please sign in to comment.