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.