0
votes

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.

1
looks like you need all four arguments in the service definition. you only have one.craigh
I'm not sure where the other 3 services come from, I tried looking for those and came up empty-handed. But I am sure they are being injected okay from somewhere else by default, I did not add those. You'll notice the error only has a problem with the fourth argument, and that's the only one I added.carbide20

1 Answers

1
votes

There are a few issues in your code.

  1. You're using a 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.
  2. If you want to get the channel by code, you should always use repository for that (sylius.repository.channel service).
  3. If you just want to set a channel on newly created product, why just not use the custom factory for that? It's more clean way and should not result in any unexpected errors in the future. Again, check out proper documentation chapter to find out how to do it properly.

Hope it will help you ;)