/** * @see Symfony\Bridge\Doctrine\Form\Type\DoctrineType::getDefaultOptions() */ public function getDefaultOptions() { $defaultOptions = parent::getDefaultOptions(); // alias "em" as "document_manager" $defaultOptions['document_manager'] = null; $defaultOptions['em'] = function (Options $options) { if (isset($options['document_manager'])) { if (isset($options['em'])) { throw new \InvalidArgumentException('You cannot set both an "em" and "document_manager" option.'); } return $options['document_manager']; } return null; }; return $defaultOptions; }
public function configureOptions(OptionsResolver $resolver) { parent::configureOptions($resolver); // Invoke the query builder closure so that we can cache choice lists // for equal query builders $queryBuilderNormalizer = function (Options $options, $queryBuilder) { if (is_callable($queryBuilder)) { $queryBuilder = call_user_func($queryBuilder, $options['em']->getRepository($options['class'])); if (null !== $queryBuilder && !$queryBuilder instanceof QueryBuilder) { throw new UnexpectedTypeException($queryBuilder, 'Doctrine\\ORM\\QueryBuilder'); } } return $queryBuilder; }; $resolver->setNormalizer('query_builder', $queryBuilderNormalizer); $resolver->setAllowedTypes('query_builder', array('null', 'callable', 'Doctrine\\ORM\\QueryBuilder')); }
/** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { parent::setDefaultOptions($resolver); $resolver->setDefaults(array('document_manager' => null)); $registry = $this->registry; $normalizer = function (Options $options, $manager) use($registry) { if (isset($options['document_manager']) && $manager) { throw new \InvalidArgumentException('You cannot set both an "em" and "document_manager" option.'); } $manager = $options['document_manager'] ?: $manager; if (null === $manager) { return $registry->getManagerForClass($options['class']); } return $registry->getManager($manager); }; $resolver->setNormalizers(array('em' => $normalizer)); }
public function configureOptions(OptionsResolver $resolver) { parent::configureOptions($resolver); /** * Здесь требуется внедрить опцию 'menu' в Loader. * Код скопирован из DoctrineType::configureOptions() * * @param Options $options * * @return DoctrineChoiceLoader */ $choiceLoader = function (Options $options) { // Unless the choices are given explicitly, load them on demand if (null === $options['choices']) { $hash = null; $qbParts = null; // If there is no QueryBuilder we can safely cache DoctrineChoiceLoader, // also if concrete Type can return important QueryBuilder parts to generate // hash key we go for it as well if (!$options['query_builder'] || false !== ($qbParts = $this->getQueryBuilderPartsForCachingHash($options['query_builder']))) { $hash = CachingFactoryDecorator::generateHash(array($options['em'], $options['class'], $qbParts)); if (isset($this->choiceLoaders[$hash])) { return $this->choiceLoaders[$hash]; } } if (null !== $options['query_builder']) { $entityLoader = $this->getLoader($options['em'], $options['query_builder'], $options['class']); } else { $queryBuilder = $options['em']->getRepository($options['class'])->createQueryBuilder('e'); $entityLoader = $this->getLoader($options['em'], $queryBuilder, $options['class']); } // !!! Вот здесь инжектится опция. $entityLoader->setMenu($options['menu']); $doctrineChoiceLoader = new DoctrineChoiceLoader($options['em'], $options['class'], $options['id_reader'], $entityLoader); if ($hash !== null) { $this->choiceLoaders[$hash] = $doctrineChoiceLoader; } return $doctrineChoiceLoader; } }; $resolver->setDefaults(['choice_label' => 'form_title', 'class' => 'MenuModule:Item', 'choice_loader' => $choiceLoader, 'menu' => null, 'required' => false]); }
/** * @param OptionsResolver $resolver */ public function configureOptions(OptionsResolver $resolver) { parent::configureOptions($resolver); $resolver->setDefaults(array('document_manager' => null)); $registry = $this->registry; $normalizer = function (Options $options, $manager) use($registry) { if (isset($options['document_manager']) && $manager) { throw new \InvalidArgumentException('You cannot set both an "em" and "document_manager" option.'); } $manager = $options['document_manager'] ?: $manager; if (null === $manager) { return $registry->getManagerForClass($options['class']); } if ($manager instanceof ObjectManager) { return $manager; } return $registry->getManager($manager); }; $resolver->setNormalizer('em', $normalizer); $resolver->setAllowedTypes('document_manager', array('null', 'string', 'Doctrine\\ODM\\MongoDB\\DocumentManager')); }
public function configureOptions(OptionsResolver $resolver) { parent::configureOptions($resolver); $resolver->setDefaults(['advanced_select' => true, 'root_node' => function (Options $options) { return $this->domainConfiguration->getRootNode(); }, 'lang' => function (Options $options) { return $this->currentLocale->getCurrentLocale(); }, 'class' => function (Options $options) { return $this->contentTypeService->getContentTypeClass($options['page_name']); }, 'query_builder' => function (Options $options) { /** @var QueryBuilder $qb */ $qb = $options['em']->getRepository($options['class'])->createQueryBuilder('e'); $node = $options['root_node']; if ($node instanceof Node) { $qb->setParameters(['left' => $node->getLeft(), 'right' => $node->getRight()]); } $qb->setParameter('lang', $options['lang']); return $qb; }, 'empty_value' => ' ', 'required' => false]); $resolver->setAllowedTypes('root_node', ['null', Node::class]); $resolver->setRequired('page_name'); }
public function configureOptions(OptionsResolver $resolver) { parent::configureOptions($resolver); $choiceLoader = function (Options $options) { // Unless the choices are given explicitly, load them on demand if (null === $options['choices']) { $hash = null; $qbParts = null; // If there is no QueryBuilder we can safely cache DoctrineChoiceLoader, // also if concrete Type can return important QueryBuilder parts to generate // hash key we go for it as well if (!$options['query_builder'] || false !== ($qbParts = $this->getQueryBuilderPartsForCachingHash($options['query_builder']))) { $hash = CachingFactoryDecorator::generateHash(array($options['em'], $options['class'], $qbParts)); if (isset($this->choiceLoaders[$hash])) { return $this->choiceLoaders[$hash]; } } if (null !== $options['query_builder']) { $entityLoader = $this->getLoader($options['em'], $options['query_builder'], $options['class']); } else { $queryBuilder = $options['em']->getRepository($options['class'])->createQueryBuilder('e'); $entityLoader = $this->getLoader($options['em'], $queryBuilder, $options['class']); } // Inject option 'unicat_structure' $entityLoader->setStructure($options['unicat_structure']); $doctrineChoiceLoader = new DoctrineChoiceLoader($options['em'], $options['class'], $options['id_reader'], $entityLoader); if ($hash !== null) { $this->choiceLoaders[$hash] = $doctrineChoiceLoader; } return $doctrineChoiceLoader; } }; $resolver->setDefaults(['choice_label' => 'form_title', 'class' => function (Options $options) { return $options['unicat_structure']->getConfiguration()->getTaxonClass(); }, 'choice_loader' => $choiceLoader, 'required' => false, 'unicat_structure' => null]); }