fix deprecations: use fqcn for submit and hidden types.

This commit is contained in:
nobohan 2018-04-04 11:54:23 +02:00
parent 5515d94aae
commit ac9325a7f2

View File

@ -4,7 +4,10 @@ namespace Chill\CustomFieldsBundle\Controller;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Doctrine\ORM\Query; use Doctrine\ORM\Query;
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup; use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
use Chill\CustomFieldsBundle\Entity\CustomField; use Chill\CustomFieldsBundle\Entity\CustomField;
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldsGroupToIdTransformer; use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldsGroupToIdTransformer;
@ -27,7 +30,7 @@ class CustomFieldsGroupController extends Controller
$cfGroups = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->findAll(); $cfGroups = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->findAll();
$defaultGroups = $this->getDefaultGroupsId(); $defaultGroups = $this->getDefaultGroupsId();
$makeDefaultFormViews = array(); $makeDefaultFormViews = array();
foreach ($cfGroups as $group) { foreach ($cfGroups as $group) {
if (!in_array($group->getId(), $defaultGroups)){ if (!in_array($group->getId(), $defaultGroups)){
@ -41,33 +44,33 @@ class CustomFieldsGroupController extends Controller
'make_default_forms' => $makeDefaultFormViews 'make_default_forms' => $makeDefaultFormViews
)); ));
} }
/** /**
* Get an array of CustomFieldsGroupId which are marked as default * Get an array of CustomFieldsGroupId which are marked as default
* for their entity * for their entity
* *
* @return int[] * @return int[]
*/ */
private function getDefaultGroupsId() private function getDefaultGroupsId()
{ {
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$customFieldsGroupIds = $em->createQuery('SELECT g.id FROM ' $customFieldsGroupIds = $em->createQuery('SELECT g.id FROM '
. 'ChillCustomFieldsBundle:CustomFieldsDefaultGroup d ' . 'ChillCustomFieldsBundle:CustomFieldsDefaultGroup d '
. 'JOIN d.customFieldsGroup g') . 'JOIN d.customFieldsGroup g')
->getResult(Query::HYDRATE_SCALAR); ->getResult(Query::HYDRATE_SCALAR);
$result = array(); $result = array();
foreach ($customFieldsGroupIds as $row) { foreach ($customFieldsGroupIds as $row) {
$result[] = $row['id']; $result[] = $row['id'];
} }
return $result; return $result;
} }
/** /**
* create a form to make the group default * create a form to make the group default
* *
* @param CustomFieldsGroup $group * @param CustomFieldsGroup $group
* @return \Symfony\Component\Form\Form * @return \Symfony\Component\Form\Form
*/ */
@ -77,12 +80,12 @@ class CustomFieldsGroupController extends Controller
'method' => 'POST', 'method' => 'POST',
'action' => $this->generateUrl('customfieldsgroup_makedefault') 'action' => $this->generateUrl('customfieldsgroup_makedefault')
)) ))
->add('id', 'hidden') ->add('id', HiddenType::class)
->add('submit', 'submit', array('label' => 'Make default')) ->add('submit', SubmitType::class, array('label' => 'Make default'))
->getForm(); ->getForm();
} }
/** /**
* Creates a new CustomFieldsGroup entity. * Creates a new CustomFieldsGroup entity.
* *
@ -97,13 +100,13 @@ class CustomFieldsGroupController extends Controller
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$em->persist($entity); $em->persist($entity);
$em->flush(); $em->flush();
$this->addFlash('success', $this->get('translator') $this->addFlash('success', $this->get('translator')
->trans("The custom fields group has been created")); ->trans("The custom fields group has been created"));
return $this->redirect($this->generateUrl('customfieldsgroup_show', array('id' => $entity->getId()))); return $this->redirect($this->generateUrl('customfieldsgroup_show', array('id' => $entity->getId())));
} }
$this->addFlash('error', $this->get('translator') $this->addFlash('error', $this->get('translator')
->trans("The custom fields group form contains errors")); ->trans("The custom fields group form contains errors"));
@ -127,7 +130,7 @@ class CustomFieldsGroupController extends Controller
'method' => 'POST', 'method' => 'POST',
)); ));
$form->add('submit', 'submit', array('label' => 'Create')); $form->add('submit', SubmitType::class, array('label' => 'Create'));
return $form; return $form;
} }
@ -160,7 +163,7 @@ class CustomFieldsGroupController extends Controller
if (!$entity) { if (!$entity) {
throw $this->createNotFoundException('Unable to find CustomFieldsGroup entity.'); throw $this->createNotFoundException('Unable to find CustomFieldsGroup entity.');
} }
$options = $this->getOptionsAvailable($entity->getEntity()); $options = $this->getOptionsAvailable($entity->getEntity());
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:show.html.twig', array( return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:show.html.twig', array(
@ -169,18 +172,18 @@ class CustomFieldsGroupController extends Controller
'options' => $options 'options' => $options
)); ));
} }
/** /**
* Return an array of available key option for custom fields group * Return an array of available key option for custom fields group
* on the given entity * on the given entity
* *
* @param string $entity the entity to filter * @param string $entity the entity to filter
*/ */
private function getOptionsAvailable($entity) private function getOptionsAvailable($entity)
{ {
$options = $this->getParameter('chill_custom_fields.' $options = $this->getParameter('chill_custom_fields.'
. 'customizables_entities'); . 'customizables_entities');
foreach($options as $key => $definition) { foreach($options as $key => $definition) {
if ($definition['class'] == $entity) { if ($definition['class'] == $entity) {
foreach ($definition['options'] as $key => $value) { foreach ($definition['options'] as $key => $value) {
@ -226,23 +229,23 @@ class CustomFieldsGroupController extends Controller
'action' => $this->generateUrl('customfieldsgroup_update', array('id' => $entity->getId())), 'action' => $this->generateUrl('customfieldsgroup_update', array('id' => $entity->getId())),
'method' => 'PUT', 'method' => 'PUT',
)); ));
$form->add('submit', 'submit', array('label' => 'Update')); $form->add('submit', SubmitType::class, array('label' => 'Update'));
return $form; return $form;
} }
private function createCreateFieldForm(CustomFieldsGroup $customFieldsGroup) private function createCreateFieldForm(CustomFieldsGroup $customFieldsGroup)
{ {
$fieldChoices = array(); $fieldChoices = array();
foreach ($this->get('chill.custom_field.provider')->getAllFields() foreach ($this->get('chill.custom_field.provider')->getAllFields()
as $key => $customType) { as $key => $customType) {
$fieldChoices[$key] = $customType->getName(); $fieldChoices[$key] = $customType->getName();
} }
$customfield = (new CustomField()) $customfield = (new CustomField())
->setCustomFieldsGroup($customFieldsGroup); ->setCustomFieldsGroup($customFieldsGroup);
$builder = $this->get('form.factory') $builder = $this->get('form.factory')
->createNamedBuilder(null, 'form', $customfield, array( ->createNamedBuilder(null, 'form', $customfield, array(
'method' => 'GET', 'method' => 'GET',
@ -252,15 +255,15 @@ class CustomFieldsGroupController extends Controller
->add('type', 'choice', array( ->add('type', 'choice', array(
'choices' => $fieldChoices 'choices' => $fieldChoices
)) ))
->add('customFieldsGroup', 'hidden') ->add('customFieldsGroup', HiddenType::class)
->add('submit', 'submit'); ->add('submit', SubmitType::class);
$builder->get('customFieldsGroup') $builder->get('customFieldsGroup')
->addViewTransformer(new CustomFieldsGroupToIdTransformer( ->addViewTransformer(new CustomFieldsGroupToIdTransformer(
$this->getDoctrine()->getManager())); $this->getDoctrine()->getManager()));
return $builder->getForm(); return $builder->getForm();
} }
/** /**
* Edits an existing CustomFieldsGroup entity. * Edits an existing CustomFieldsGroup entity.
* *
@ -280,29 +283,29 @@ class CustomFieldsGroupController extends Controller
if ($editForm->isValid()) { if ($editForm->isValid()) {
$em->flush(); $em->flush();
$this->addFlash('success', $this->get('translator') $this->addFlash('success', $this->get('translator')
->trans("The custom fields group has been updated")); ->trans("The custom fields group has been updated"));
return $this->redirect($this->generateUrl('customfieldsgroup_edit', array('id' => $id))); return $this->redirect($this->generateUrl('customfieldsgroup_edit', array('id' => $id)));
} }
$this->addFlash('error', $this->get('translator') $this->addFlash('error', $this->get('translator')
->trans("The custom fields group form contains errors")); ->trans("The custom fields group form contains errors"));
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:edit.html.twig', array( return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:edit.html.twig', array(
'entity' => $entity, 'entity' => $entity,
'edit_form' => $editForm->createView(), 'edit_form' => $editForm->createView(),
)); ));
} }
/** /**
* Set the CustomField Group with id $cFGroupId as default * Set the CustomField Group with id $cFGroupId as default
*/ */
public function makeDefaultAction(Request $request) public function makeDefaultAction(Request $request)
{ {
$form = $this->createMakeDefaultForm(null); $form = $this->createMakeDefaultForm(null);
$form->handleRequest($request); $form->handleRequest($request);
@ -324,25 +327,25 @@ class CustomFieldsGroupController extends Controller
if($cFDefaultGroup) { if($cFDefaultGroup) {
$em->remove($cFDefaultGroup); $em->remove($cFDefaultGroup);
$em->flush(); /*this is necessary, if not doctrine $em->flush(); /*this is necessary, if not doctrine
* will not remove old entity before adding a new one, * will not remove old entity before adding a new one,
* and this leads to violation constraint of unique entity * and this leads to violation constraint of unique entity
* in postgresql * in postgresql
*/ */
} }
$newCFDefaultGroup = new CustomFieldsDefaultGroup(); $newCFDefaultGroup = new CustomFieldsDefaultGroup();
$newCFDefaultGroup->setCustomFieldsGroup($cFGroup); $newCFDefaultGroup->setCustomFieldsGroup($cFGroup);
$newCFDefaultGroup->setEntity($cFGroup->getEntity()); $newCFDefaultGroup->setEntity($cFGroup->getEntity());
$em->persist($newCFDefaultGroup); $em->persist($newCFDefaultGroup);
$em->flush(); $em->flush();
$this->addFlash('success', $this->get('translator') $this->addFlash('success', $this->get('translator')
->trans("The default custom fields group has been changed")); ->trans("The default custom fields group has been changed"));
return $this->redirect($this->generateUrl('customfieldsgroup')); return $this->redirect($this->generateUrl('customfieldsgroup'));
} }
/** /**
* This function render the customFieldsGroup as a form. * This function render the customFieldsGroup as a form.
* *
@ -358,20 +361,20 @@ class CustomFieldsGroupController extends Controller
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->find($id); $entity = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->find($id);
if (!$entity) { if (!$entity) {
throw $this->createNotFoundException('Unable to find CustomFieldsGroups entity.'); throw $this->createNotFoundException('Unable to find CustomFieldsGroups entity.');
} }
$form = $this->createForm('custom_field', null, array('group' => $entity)); $form = $this->createForm('custom_field', null, array('group' => $entity));
$form->add('submit_dump', 'submit', array('label' => 'POST AND DUMP')); $form->add('submit_dump', 'submit', array('label' => 'POST AND DUMP'));
$form->add('submit_render','submit', array('label' => 'POST AND RENDER')); $form->add('submit_render','submit', array('label' => 'POST AND RENDER'));
$form->handleRequest($request); $form->handleRequest($request);
$this->get('twig.loader') $this->get('twig.loader')
->addPath(__DIR__.'/../Tests/Fixtures/App/app/Resources/views/', ->addPath(__DIR__.'/../Tests/Fixtures/App/app/Resources/views/',
$namespace = 'test'); $namespace = 'test');
if ($form->isSubmitted()) { if ($form->isSubmitted()) {
if ($form->get('submit_render')->isClicked()) { if ($form->get('submit_render')->isClicked()) {
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:render_for_test.html.twig', array( return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:render_for_test.html.twig', array(
@ -379,17 +382,17 @@ class CustomFieldsGroupController extends Controller
'customFieldsGroup' => $entity 'customFieldsGroup' => $entity
)); ));
} }
var_dump($form->getData()); var_dump($form->getData());
var_dump(json_enccode($form->getData())); var_dump(json_enccode($form->getData()));
} }
return $this return $this
->render('@test/CustomField/simple_form_render.html.twig', array( ->render('@test/CustomField/simple_form_render.html.twig', array(
'form' => $form->createView() 'form' => $form->createView()
)); ));
} }
} }