Sylius\Bundle\ShippingBundle\Form\Type\ShippingMethodChoiceType::configureOptions PHP Метод

configureOptions() публичный Метод

public configureOptions ( Symfony\Component\OptionsResolver\OptionsResolver $resolver )
$resolver Symfony\Component\OptionsResolver\OptionsResolver
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(['choices' => function (Options $options) {
            if (isset($options['subject']) && $this->shippingMethodsResolver->supports($options['subject'])) {
                return $this->shippingMethodsResolver->getSupportedMethods($options['subject']);
            }
            return $this->repository->findAll();
        }, 'choice_value' => 'code', 'choice_label' => 'name', 'choice_translation_domain' => false])->setDefined(['subject'])->setAllowedTypes('subject', ShippingSubjectInterface::class);
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     parent::configureOptions($resolver);
     $choiceList = function (Options $options) {
         if (isset($options['subject'])) {
             $methods = $this->resolver->getSupportedMethods($options['subject'], $options['criteria']);
         } else {
             $methods = $this->repository->findBy($options['criteria']);
         }
         if ($options['channel']) {
             $filteredMethods = [];
             foreach ($methods as $method) {
                 if ($options['channel']->hasShippingMethod($method)) {
                     $filteredMethods[] = $method;
                 }
             }
             $methods = $filteredMethods;
         }
         return new ObjectChoiceList($methods, null, [], null, 'id');
     };
     $resolver->setDefaults(['choice_list' => $choiceList, 'criteria' => [], 'channel' => null])->setAllowedTypes('channel', [ChannelInterface::class, 'null']);
 }