Skip to content

Commit

Permalink
Merge pull request Sylius#7744 from pamil/pull-request/7719
Browse files Browse the repository at this point in the history
Pass entry options to entry types in ResourceTranslationsType
  • Loading branch information
pjedrzejewski authored Mar 13, 2017
2 parents 2e6ce0c + 4f85e4a commit b0c9d92
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,19 @@ public static function getSubscribedEvents()
public function preSetData(FormEvent $event)
{
$form = $event->getForm();
$type = $form->getConfig()->getOption('entry_type');
$entryType = $form->getConfig()->getOption('entry_type');
$entryOptions = $form->getConfig()->getOption('entry_options');

foreach ($this->definedLocalesCodes as $localeCode) {
if ($form->has($localeCode)) {
continue;
}

$required = $localeCode === $this->defaultLocaleCode;
$form->add($localeCode, $type, [
$form->add($localeCode, $entryType, array_replace($entryOptions, [
'required' => $required,
]);
'block_name' => 'entry',
]));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\ResourceBundle\Tests\Form\Type;

use Prophecy\Prophecy\ObjectProphecy;
use Sylius\Bundle\ResourceBundle\Form\Type\ResourceTranslationsType;
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;

/**
* @author Kamil Kokot <[email protected]>
*/
final class ResourceTranslationsTypeTest extends TypeTestCase
{

/**
* {@inheritdoc}
*/
protected function getExtensions()
{
/** @var TranslationLocaleProviderInterface|ObjectProphecy $translationLocaleProvider */
$translationLocaleProvider = $this->prophesize(TranslationLocaleProviderInterface::class);
$translationLocaleProvider->getDefaultLocaleCode()->willReturn('en_US');
$translationLocaleProvider->getDefinedLocalesCodes()->willReturn(['en_US', 'pl_PL']);

$resourceTranslationsType = new ResourceTranslationsType($translationLocaleProvider->reveal());

return [
new PreloadedExtension([$resourceTranslationsType], []),
];
}

/**
* @test
*/
public function it_respects_entry_options()
{
$form = $this->factory->create(
ResourceTranslationsType::class,
null,
['entry_type' => TextType::class, 'entry_options' => ['empty_data' => 'Default']]
);

// Testing child type because of `$event->getForm()->getParent()->getData()` inside ResourceTranslationsType::submit()
$englishTranslationForm = $form->get('en_US');
$englishTranslationForm->submit(null);

$this->assertEquals('Default', $englishTranslationForm->getData());
}
}

0 comments on commit b0c9d92

Please sign in to comment.