/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { if (isset($defaults['choices']) || isset($defaults['choice_loader'])) { throw new \LogicException('You can not provide your own choice list for this type.'); } parent::buildForm($builder, $options); }
public function getDefaultOptions(array $options) { $options = parent::getDefaultOptions($options); $roles = array(); if (count($options['choices']) == 0) { // get roles from the Admin classes foreach ($this->pool->getAdminServiceIds() as $id) { try { $admin = $this->pool->getInstance($id); } catch (\Exception $e) { continue; } $securityHandler = $admin->getSecurityHandler(); foreach ($securityHandler->buildSecurityInformation($admin) as $role => $acls) { $roles[$role] = $role; } } // get roles from the service container foreach ($this->pool->getContainer()->getParameter('security.role_hierarchy.roles') as $name => $rolesHierarchy) { $roles[$name] = $name . ': ' . implode(', ', $rolesHierarchy); foreach ($rolesHierarchy as $role) { if (!isset($roles[$role])) { $roles[$role] = $role; } } } $options['choices'] = $roles; } return $options; }
/** * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form, array $options) { parent::buildView($view, $form, $options); $view->vars['include_jquery'] = $options['include_jquery']; $view->vars['include_js'] = $options['include_js']; $view->vars['include_js_constructor'] = $options['include_js_constructor']; $view->vars['include_css'] = $options['include_css']; $view->vars['picker'] = $options['picker']; }
/** * {@inheritDoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { // translate options before building form foreach ($options['choices'] as $name => $value) { $options['choices'][$name] = $this->translator->trans($value, array(), $options['catalogue']); } // translate empty value if (!empty($options['empty_value'])) { $options['empty_value'] = $this->translator->trans($options['empty_value'], array(), $options['catalogue']); } parent::buildForm($builder, $options); }
/** * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form, array $options) { switch ($options['widget']) { case 'select2': if ($this->assetsLoader) { $this->assetsLoader->addVendor($options['widget']); } break; default: break; } parent::buildView($view, $form, $options); }
/** * @param OptionsResolver $resolver */ public function configureOptions(OptionsResolver $resolver) { $this->choices = []; /**@var Environment $choice*/ foreach ($this->service->getAllInMyCircle() as $choice) { $this->choices[$choice->getName()] = $choice; } parent::configureOptions($resolver); $resolver->setDefaults(array('choices' => [], 'attr' => ['data-live-search' => false], 'choice_attr' => function ($category, $key, $index) { /** @var \AppBundle\Form\DataField\DataFieldType $dataFieldType */ $dataFieldType = $this->choices[$index]; return ['data-content' => '<span class="text-' . $dataFieldType->getColor() . '"><i class="fa fa-square"></i> ' . $dataFieldType->getName() . '</span>']; }, 'choice_value' => function ($value) { return $value; }, 'multiple' => false, 'managedOnly' => true)); }
/** * {@inheritdoc} */ public function setDefaultOptions(OptionsResolverInterface $resolver) { parent::setDefaultOptions($resolver); //set our custom choice list $treeChoiceListCache =& $this->treeChoiceListCache; $treeChoiceList = function (Options $options) use(&$treeChoiceListCache) { // Harden against NULL values (like in EntityType and ModelType) $choices = null !== $options['choices'] ? $options['choices'] : []; // Reuse existing choice lists in order to increase performance $hash = hash('sha256', serialize([$choices, $options['preferred_choices']])); if (!isset($treeChoiceListCache[$hash])) { $treeChoiceListCache[$hash] = new TreeChoiceList($choices, $options['preferred_choices']); } return $treeChoiceListCache[$hash]; }; $resolver->setDefaults(['choice_list' => $treeChoiceList, 'choices_as_values' => true]); }
/** * @param OptionsResolver $resolver */ public function configureOptions(OptionsResolver $resolver) { $this->choices = []; $keys = []; /**@var ContentType $choice*/ foreach ($this->service->getAll() as $choice) { $keys[] = $choice->getName(); $this->choices[$choice->getName()] = $choice; } parent::configureOptions($resolver); $resolver->setDefaults(array('choices' => $keys, 'attr' => ['data-live-search' => false], 'choice_attr' => function ($category, $key, $index) { /** @var ContentType $contentType */ $contentType = $this->choices[$index]; return ['data-content' => '<span class="text-' . $contentType->getColor() . '"><i class="' . (empty($contentType->getIcon()) ? ' fa fa-book' : $contentType->getIcon()) . '"></i> ' . $contentType->getName() . '</span>']; }, 'choice_value' => function ($value) { return $value; }, 'multiple' => false)); }
private function createChoiceListView(ChoiceListInterface $choiceList, array $options) { // If no explicit grouping information is given, use the structural // information from the "choices" option for creating groups if (!$options['group_by'] && $options['choices']) { $options['group_by'] = !$options['choices_as_values'] ? ChoiceType::flipRecursive($options['choices']) : $options['choices']; } return $this->choiceListFactory->createView($choiceList, $options['preferred_choices'], $options['choice_label'], $options['choice_name'], $options['group_by'], $options['choice_attr']); }
public function getDefaultOptions(array $options) { $options = parent::getDefaultOptions($options); $options['choices'] = array(self::TYPE_YES => $this->translator->trans('label_type_yes', array(), 'SonataAdminBundle'), self::TYPE_NO => $this->translator->trans('label_type_no', array(), 'SonataAdminBundle')); return $options; }
/** * {@inheritDoc} */ public function setDefaultOptions(OptionsResolverInterface $resolver) { parent::setDefaultOptions($resolver); $resolver->setDefaults(array('choices' => array(self::TYPE_IS_EQUAL => $this->translator->trans('label_type_equals', array(), 'SonataAdminBundle'), self::TYPE_IS_NOT_EQUAL => $this->translator->trans('label_type_not_equals', array(), 'SonataAdminBundle')))); }
public function buildForm(FormBuilderInterface $builder, array $options = self::DEFAULT_OPTIONS) { parent::buildForm($builder, $options); }
/** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { $choiceLabels = (object) array('labels' => array()); $choiceListFactory = $this->choiceListFactory; $emptyData = function (Options $options) { if ($options['multiple'] || $options['expanded']) { return array(); } return ''; }; $placeholder = function (Options $options) { return $options['required'] ? null : ''; }; // BC closure, to be removed in 3.0 $choicesNormalizer = function (Options $options, $choices) use($choiceLabels) { // Unset labels from previous invocations $choiceLabels->labels = array(); // This closure is irrelevant when "choices_as_values" is set to true if ($options['choices_as_values']) { return $choices; } if (null === $choices) { return; } return ChoiceType::normalizeLegacyChoices($choices, $choiceLabels); }; // BC closure, to be removed in 3.0 $choiceLabel = function (Options $options) use($choiceLabels) { // If the choices contain duplicate labels, the normalizer of the // "choices" option stores them in the $choiceLabels variable // Trigger the normalizer $options->offsetGet('choices'); // Pick labels from $choiceLabels if available if ($choiceLabels->labels) { // Don't pass the labels by reference. We do want to create a // copy here so that every form has an own version of that // variable (contrary to the $choiceLabels object shared by all // forms) $labels = $choiceLabels->labels; // The $choiceLabels object is shared with the 'choices' closure. // Since that normalizer can be replaced, labels have to be cleared here. $choiceLabels->labels = array(); return function ($choice, $key) use($labels) { return $labels[$key]; }; } return; }; $that = $this; $choiceListNormalizer = function (Options $options, $choiceList) use($choiceListFactory, $that) { if ($choiceList) { @trigger_error(sprintf('The "choice_list" option of the "%s" form type (%s) is deprecated since version 2.7 and will be removed in 3.0. Use "choice_loader" instead.', $that->getName(), __CLASS__), E_USER_DEPRECATED); if ($choiceList instanceof LegacyChoiceListInterface) { return new LegacyChoiceListAdapter($choiceList); } return $choiceList; } if (null !== $options['choice_loader']) { return $choiceListFactory->createListFromLoader($options['choice_loader'], $options['choice_value']); } // Harden against NULL values (like in EntityType and ModelType) $choices = null !== $options['choices'] ? $options['choices'] : array(); // BC when choices are in the keys, not in the values if (!$options['choices_as_values']) { return $choiceListFactory->createListFromFlippedChoices($choices, $options['choice_value'], false); } return $choiceListFactory->createListFromChoices($choices, $options['choice_value']); }; $placeholderNormalizer = function (Options $options, $placeholder) use($that) { if (!is_object($options['empty_value']) || !$options['empty_value'] instanceof \Exception) { @trigger_error(sprintf('The form option "empty_value" of the "%s" form type (%s) is deprecated since version 2.6 and will be removed in 3.0. Use "placeholder" instead.', $that->getName(), __CLASS__), E_USER_DEPRECATED); $placeholder = $options['empty_value']; } if ($options['multiple']) { // never use an empty value for this case return; } elseif ($options['required'] && ($options['expanded'] || isset($options['attr']['size']) && $options['attr']['size'] > 1)) { // placeholder for required radio buttons or a select with size > 1 does not make sense return; } elseif (false === $placeholder) { // an empty value should be added but the user decided otherwise return; } elseif ($options['expanded'] && '' === $placeholder) { // never use an empty label for radio buttons return 'None'; } // empty value has been set explicitly return $placeholder; }; $compound = function (Options $options) { return $options['expanded']; }; $choiceTranslationDomainNormalizer = function (Options $options, $choiceTranslationDomain) { if (true === $choiceTranslationDomain) { return $options['translation_domain']; } return $choiceTranslationDomain; }; $resolver->setDefaults(array('multiple' => false, 'expanded' => false, 'choice_list' => null, 'choices' => array(), 'choices_as_values' => false, 'choice_loader' => null, 'choice_label' => $choiceLabel, 'choice_name' => null, 'choice_value' => null, 'choice_attr' => null, 'preferred_choices' => array(), 'group_by' => null, 'empty_data' => $emptyData, 'empty_value' => new \Exception(), 'placeholder' => $placeholder, 'error_bubbling' => false, 'compound' => $compound, 'data_class' => null, 'choice_translation_domain' => true)); $resolver->setNormalizer('choices', $choicesNormalizer); $resolver->setNormalizer('choice_list', $choiceListNormalizer); $resolver->setNormalizer('placeholder', $placeholderNormalizer); $resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer); $resolver->setAllowedTypes('choice_list', array('null', 'Symfony\\Component\\Form\\ChoiceList\\ChoiceListInterface', 'Symfony\\Component\\Form\\Extension\\Core\\ChoiceList\\ChoiceListInterface')); $resolver->setAllowedTypes('choices', array('null', 'array', '\\Traversable')); $resolver->setAllowedTypes('choice_translation_domain', array('null', 'bool', 'string')); $resolver->setAllowedTypes('choices_as_values', 'bool'); $resolver->setAllowedTypes('choice_loader', array('null', 'Symfony\\Component\\Form\\ChoiceList\\Loader\\ChoiceLoaderInterface')); $resolver->setAllowedTypes('choice_label', array('null', 'bool', 'callable', 'string', 'Symfony\\Component\\PropertyAccess\\PropertyPath')); $resolver->setAllowedTypes('choice_name', array('null', 'callable', 'string', 'Symfony\\Component\\PropertyAccess\\PropertyPath')); $resolver->setAllowedTypes('choice_value', array('null', 'callable', 'string', 'Symfony\\Component\\PropertyAccess\\PropertyPath')); $resolver->setAllowedTypes('choice_attr', array('null', 'array', 'callable', 'string', 'Symfony\\Component\\PropertyAccess\\PropertyPath')); $resolver->setAllowedTypes('preferred_choices', array('array', '\\Traversable', 'callable', 'string', 'Symfony\\Component\\PropertyAccess\\PropertyPath')); $resolver->setAllowedTypes('group_by', array('null', 'array', '\\Traversable', 'callable', 'string', 'Symfony\\Component\\PropertyAccess\\PropertyPath')); }
/** * {@inheritdoc} */ public function setDefaultOptions(OptionsResolverInterface $resolver) { parent::setDefaultOptions($resolver); $resolver->replaceDefaults(array('multiple' => true)); }
/** * @param FormView $view * @param FormInterface $form * @param array $options */ public function buildView(FormView $view, FormInterface $form, array $options) { parent::buildView($view, $form, $options); $view->vars = array_replace($view->vars, array('icons' => $options['icons'])); }
public function setDefaultOptions(OptionsResolverInterface $resolver) { parent::setDefaultOptions($resolver); $resolver->setRequired(array('sf_admin_class')); }
public function setDefaultOptions(OptionsResolverInterface $resolver) { parent::setDefaultOptions($resolver); $resolver->setDefaults(array('source_field' => null)); }
/** * {@inheritDoc} */ public function getDefaultOptions() { $options = parent::getDefaultOptions(); $options['choices'] = array(self::TYPE_IS_EQUAL => $this->translator->trans('label_type_equals', array(), 'SonataAdminBundle'), self::TYPE_IS_NOT_EQUAL => $this->translator->trans('label_type_not_equals', array(), 'SonataAdminBundle')); return $options; }
/** * {@inheritdoc} */ public function buildForm(FormBuilder $builder, array $options) { $options['expanded'] = true; $options['multiple'] = false; parent::buildForm($builder, $options); }