Eccube\Form\Type\ShippingMultipleItemType::buildForm PHP Method

buildForm() public method

public buildForm ( Symfony\Component\Form\FormBuilderInterface $builder, array $options )
$builder Symfony\Component\Form\FormBuilderInterface
$options array
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $app = $this->app;
        $builder->add('quantity', 'integer', array('attr' => array('min' => 1, 'maxlength' => $this->app['config']['int_len']), 'constraints' => array(new Assert\NotBlank(), new Assert\GreaterThanOrEqual(array('value' => 1)), new Assert\Regex(array('pattern' => '/^\\d+$/')))))->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use($app) {
            $form = $event->getForm();
            if ($app->isGranted('IS_AUTHENTICATED_FULLY')) {
                // 会員の場合、CustomerAddressを設定
                $Customer = $app->user();
                $form->add('customer_address', 'entity', array('class' => 'Eccube\\Entity\\CustomerAddress', 'property' => 'shippingMultipleDefaultName', 'query_builder' => function (EntityRepository $er) use($Customer) {
                    return $er->createQueryBuilder('ca')->where('ca.Customer = :Customer')->orderBy("ca.id", "ASC")->setParameter('Customer', $Customer);
                }, 'constraints' => array(new Assert\NotBlank())));
            } else {
                // 非会員の場合、セッションに設定されたCustomerAddressを設定
                if ($app['session']->has('eccube.front.shopping.nonmember.customeraddress')) {
                    $customerAddresses = $app['session']->get('eccube.front.shopping.nonmember.customeraddress');
                    $customerAddresses = unserialize($customerAddresses);
                    $addresses = array();
                    $i = 0;
                    /** @var \Eccube\Entity\CustomerAddress $CustomerAddress */
                    foreach ($customerAddresses as $CustomerAddress) {
                        $addresses[$i] = $CustomerAddress->getShippingMultipleDefaultName();
                        $i++;
                    }
                    $form->add('customer_address', 'choice', array('choices' => $addresses, 'constraints' => array(new Assert\NotBlank())));
                }
            }
        })->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
            /** @var \Eccube\Entity\Shipping $data */
            $data = $event->getData();
            /** @var \Symfony\Component\Form\Form $form */
            $form = $event->getForm();
            if (is_null($data)) {
                return;
            }
            $quantity = 0;
            $Order = $data->getOrder();
            $shippings = $Order->getShippings();
            if (count($shippings) > 1) {
                // お届け先を複数に分割していた場合、登録されている個数を設定
                foreach ($data->getShipmentItems() as $ShipmentItem) {
                    $quantity += $ShipmentItem->getQuantity();
                }
            }
            $form['quantity']->setData($quantity);
        });
    }
ShippingMultipleItemType