Completed
Push — master ( e4a46d...3346e3 )
by Adam
06:33
created

SimilarProductSubscriber::onProductInit()   A

Complexity

Conditions 2
Paths 2

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 7
Code Lines 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\SimilarProductBundle\EventListener;
14
15
use Doctrine\Common\Collections\ArrayCollection;
16
use WellCommerce\Bundle\CatalogBundle\Entity\Product;
17
use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
18
use WellCommerce\Bundle\CoreBundle\EventListener\AbstractEventSubscriber;
19
use WellCommerce\Component\Form\Event\FormEvent;
20
21
/**
22
 * Class SimilarProductSubscriber
23
 *
24
 * @author  Adam Piotrowski <[email protected]>
25
 */
26
class SimilarProductSubscriber extends AbstractEventSubscriber
27
{
28
    public static function getSubscribedEvents()
29
    {
30
        return [
31
            'admin.product.pre_form_init' => ['onProductFormAdminInit'],
32
            'product.post_init'           => ['onProductInit'],
33
        ];
34
    }
35
    
36
    public function onProductInit(EntityEvent $event)
37
    {
38
        $entity = $event->getEntity();
39
        if ($entity instanceof Product) {
40
            $entity->setSimilarProducts(new ArrayCollection());
41
        }
42
    }
43
    
44
    public function onProductFormAdminInit(FormEvent $event)
45
    {
46
        $resource = $event->getResource();
47
        if ($resource instanceof Product) {
48
            $form    = $event->getForm();
49
            $builder = $event->getFormBuilder();
50
            
51
            $similarProductData = $form->addChild($builder->getElement('nested_fieldset', [
52
                'name'  => 'similar_products',
53
                'label' => 'similar_product.fieldset.products',
54
            ]));
55
            
56
            $similarProductData->addChild($builder->getElement('product_select', [
57
                'name'        => 'similarProducts',
58
                'label'       => 'similar_product.fieldset.products',
59
                'load_route'  => 'admin.product.grid',
60
                'repeat_min'  => 0,
61
                'repeat_max'  => 50,
62
                'transformer' => $builder->getRepositoryTransformer('collection', $this->container->get('product.repository')),
63
            ]));
64
        }
65
    }
66
}
67