Adapt ScopePicker to replace Entity field by Hidden field if only one choice

This commit is contained in:
Jean-Francois Monfort 2021-01-19 11:39:32 +01:00
parent 84e2cc9e02
commit 764d2cc87a

View File

@ -15,32 +15,25 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
namespace Chill\MainBundle\Form\Type; namespace Chill\MainBundle\Form\Type;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\DataMapper\ScopePickerDataMapper; use Chill\MainBundle\Form\DataMapper\ScopePickerDataMapper;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\AbstractType;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\Exception\TransformationFailedException; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Exception\UnexpectedTypeException; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\DataMapper\RadioListMapper;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView; use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\Options;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\OptionsResolver\OptionsResolver;
use Chill\MainBundle\Entity\Scope;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\MainBundle\Entity\Center;
use Symfony\Component\Security\Core\Role\Role; use Symfony\Component\Security\Core\Role\Role;
/** /**
@ -93,7 +86,7 @@ class ScopePickerType extends AbstractType
$query = $this->buildAccessibleScopeQuery($options['center'], $options['role']); $query = $this->buildAccessibleScopeQuery($options['center'], $options['role']);
$items = $query->getQuery()->execute(); $items = $query->getQuery()->execute();
if (count($items) !== 1) { if (1 !== count($items)) {
$builder->add( $builder->add(
'scope', EntityType::class, [ 'scope', EntityType::class, [
'class' => Scope::class, 'class' => Scope::class,
@ -131,14 +124,13 @@ class ScopePickerType extends AbstractType
$resolver $resolver
// create `center` option // create `center` option
->setRequired('center') ->setRequired('center')
->setAllowedTypes('center', [Center::class ]) ->setAllowedTypes('center', [Center::class])
// create ``role` option // create ``role` option
->setRequired('role') ->setRequired('role')
->setAllowedTypes('role', ['string', Role::class ]); ->setAllowedTypes('role', ['string', Role::class]);
} }
/** /**
*
* @return \Doctrine\ORM\QueryBuilder * @return \Doctrine\ORM\QueryBuilder
*/ */
protected function buildAccessibleScopeQuery(Center $center, Role $role) protected function buildAccessibleScopeQuery(Center $center, Role $role)