/**
  * @Route("/newPagePart", name="KunstmaanPagePartBundle_admin_newpagepart")
  * @Template("KunstmaanPagePartBundle:PagePartAdminTwigExtension:pagepart.html.twig")
  *
  * @param \Symfony\Component\HttpFoundation\Request $request
  *
  * @return array
  */
 public function newPagePartAction(Request $request)
 {
     $em = $this->getDoctrine()->getManager();
     $pageId = $request->get('pageid');
     $pageClassName = $request->get('pageclassname');
     $context = $request->get('context');
     $pagePartClass = $request->get('type');
     $page = $em->getRepository($pageClassName)->findOneById($pageId);
     $pagePartConfigurationReader = new PagePartConfigurationReader($this->container->get('kernel'));
     $pagePartAdminConfigurators = $pagePartConfigurationReader->getPagePartAdminConfigurators($page);
     $pagePartAdminConfigurator = null;
     foreach ($pagePartAdminConfigurators as $ppac) {
         if ($context == $ppac->getContext()) {
             $pagePartAdminConfigurator = $ppac;
         }
     }
     if (is_null($pagePartAdminConfigurator)) {
         throw new \RuntimeException(sprintf('No page part admin configurator found for context "%s".', $context));
     }
     $pagePartAdmin = new PagePartAdmin($pagePartAdminConfigurator, $em, $page, $context, $this->container);
     $pagePart = new $pagePartClass();
     $formFactory = $this->container->get('form.factory');
     $formBuilder = $formFactory->createBuilder(FormType::class);
     $pagePartAdmin->adaptForm($formBuilder);
     $id = 'newpp_' . time();
     $data = $formBuilder->getData();
     $data['pagepartadmin_' . $id] = $pagePart;
     $adminType = $pagePart->getDefaultAdminType();
     $adminTypeFqn = ClassUtils::getClass($adminType);
     $formBuilder->add('pagepartadmin_' . $id, $adminTypeFqn);
     $formBuilder->setData($data);
     $form = $formBuilder->getForm();
     $formview = $form->createView();
     return array('id' => $id, 'form' => $formview, 'pagepart' => $pagePart, 'pagepartadmin' => $pagePartAdmin, 'editmode' => true);
 }
 /**
  * @Route("/newPagePart", name="KunstmaanPagePartBundle_admin_newpagepart")
  * @Template("KunstmaanPagePartBundle:PagePartAdminTwigExtension:pagepart.html.twig")
  *
  * @param \Symfony\Component\HttpFoundation\Request $request
  *
  * @return array
  */
 public function newPagePartAction(Request $request)
 {
     $em = $this->get('doctrine.orm.entity_manager');
     $pageId = $request->get('pageid');
     $pageClassName = $request->get('pageclassname');
     $context = $request->get('context');
     $pagePartClass = $request->get('type');
     /** @var HasPagePartsInterface $page */
     $page = $em->getRepository($pageClassName)->find($pageId);
     if (false === $page instanceof HasPagePartsInterface) {
         throw new \RuntimeException(sprintf('Given page (%s:%d) has no pageparts', $pageClassName, $pageId));
     }
     $pagePartConfigurationReader = $this->container->get('kunstmaan_page_part.page_part_configuration_reader');
     $pagePartAdminConfigurators = $pagePartConfigurationReader->getPagePartAdminConfigurators($page);
     $pagePartAdminConfigurator = null;
     foreach ($pagePartAdminConfigurators as $ppac) {
         if ($context == $ppac->getContext()) {
             $pagePartAdminConfigurator = $ppac;
         }
     }
     if (is_null($pagePartAdminConfigurator)) {
         throw new \RuntimeException(sprintf('No page part admin configurator found for context "%s".', $context));
     }
     $pagePartAdmin = new PagePartAdmin($pagePartAdminConfigurator, $em, $page, $context, $this->container);
     /** @var PagePartInterface $pagePart */
     $pagePart = new $pagePartClass();
     if (false === $pagePart instanceof PagePartInterface) {
         throw new \RuntimeException(sprintf('Given pagepart expected to implement PagePartInterface, %s given', $pagePartClass));
     }
     $formFactory = $this->container->get('form.factory');
     $formBuilder = $formFactory->createBuilder(FormType::class);
     $pagePartAdmin->adaptForm($formBuilder);
     $id = 'newpp_' . time();
     $data = $formBuilder->getData();
     $data['pagepartadmin_' . $id] = $pagePart;
     $adminType = $pagePart->getDefaultAdminType();
     if (is_string($adminType)) {
         $adminType = $this->container->get($adminType);
     }
     $adminTypeFqn = ClassUtils::getClass($adminType);
     $formBuilder->add('pagepartadmin_' . $id, $adminTypeFqn);
     $formBuilder->setData($data);
     $form = $formBuilder->getForm();
     $formview = $form->createView();
     return ['id' => $id, 'form' => $formview, 'pagepart' => $pagePart, 'pagepartadmin' => $pagePartAdmin, 'editmode' => true];
 }
 /**
  * @param FormView $formView
  *
  * @return array
  */
 public function getFormErrors(FormView $formView)
 {
     $errors = parent::getFormErrors($formView);
     $formHelper = $this->getFormHelper();
     if (isset($formView['pagepartadmin_' . $this->pagePartAdmin->getContext()])) {
         $errors = array_merge($errors, $formHelper->getRecursiveErrorMessages($formView['pagepartadmin_' . $this->pagePartAdmin->getContext()]));
     }
     return $errors;
 }