Reputation: 1745
I've been looking all day for a way to access a channel entity within the ProductType (which extends AbstractResourceType) in Sylius. Specifically, I need to get the 'US_Web_Store' channel entity within the buildForm() function of the ProductType class (src/Sylius/Bundle/ProductBundle/Form/Type/ProductType.php)
Here's my code:
src/Sylius/Bundle/ProductBundle/Form/Type/ProductType.php
namespace Sylius\Bundle\ProductBundle\Form\Type;
use Doctrine\ORM\EntityManagerInterface;
class ProductType extends AbstractResourceType
{
private $variantResolver;
private $em;
public function __construct($dataClass, $validationGroups, ProductVariantResolverInterface $variantResolver, EntityManagerInterface $em) {
parent::__construct($dataClass, $validationGroups);
$this->variantResolver = $variantResolver;
$this->em = $em;
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$product = $builder->getData();
$product->addChannel($this->container->get('sylius.repository.channel')->findOneBy(['code' => 'US_Web_Store']));
[...]
}
}
Then in src/Sylius/Bundle/ProductBundle/Resources/config/services.xml:
<service id="product.form.type.product" class="Sylius\Bundle\ProductBundle\Form\Type\ProductType">
<argument type="service" id="doctrine.orm.entity_manager"/>
</service>
And the error I'm getting is:
Type error: Argument 4 passed to Sylius\Bundle\ProductBundle\Form\Type\ProductType::__construct() must implement interface Doctrine\ORM\EntityManagerInterface, none given
I've also tried passing the channel repository via:
But had the same issue. Nothing is ever passed to the controller, so I'm sure I'm missing something fundamental here.
Upvotes: 0
Views: 571
Reputation: 161
There are a few issues in your code.
ProductType
from Sylius, which means it will be used instead of the default one. However, your service id is different than this in Sylius (you have product.form.type.product
, in Sylius it's sylius.form.type.product
). In a result, you'll have two services using the same Sylius\Bundle\ProductBundle\Form\Type\ProductType
. Exception that you have is caused by the sylius.form.type.product
service which now uses your class that requires fourth parameter, but it does not get it. Event if you'd name the service with the same key, it would still throw an exception, as you need to pass all 4 parameters into constructor in service definition, as it already have been notices by craigh. If you don't know what they are, you should look at the definition of sylius.form.type.product
service. To get known about the most proper way of overriding Sylius forms, checkout the Customizing Forms chapter from docs.sylius.repository.channel
service).Hope it will help you ;)
Upvotes: 1