public function newChildAction(Request $request)
    {
        $this->isGranted('CREATE');

        $context = $this->loadContext($request);

        $resourceName = $this->config->getResourceName();
        $resource = $context->getResource($resourceName);

        $child = $this->createNewFromParent($context, $resource);

        if (0 < strlen($referer = $request->headers->get('referer'))) {
            $cancelPath = $referer;
        } else {
            $cancelPath = $this->generateResourcePath($request);
        }

        $form = $this
            ->createForm($this->config->getFormType(), $child, [
                'action' => $this->generateUrl(
                    $this->config->getRoute('new_child'),
                    $context->getIdentifiers(true)
                ),
                'method' => 'POST',
                'attr' => [
                    'class' => 'form-horizontal form-with-tabs',
                ],
                'admin_mode' => true,
                '_redirect_enabled' => true,
            ])
            ->add('actions', 'form_actions', [
                'buttons' => [
                    'saveAndList' => [
                        'type' => 'submit', 'options' => [
                            'button_class' => 'primary',
                            'label' => 'ekyna_core.button.save_and_list',
                            'attr' => [
                                'icon' => 'list',
                            ],
                        ],
                    ],
                    'save' => [
                        'type' => 'submit', 'options' => [
                            'button_class' => 'primary',
                            'label' => 'ekyna_core.button.save',
                            'attr' => [
                                'icon' => 'ok',
                            ],
                        ],
                    ],
                    'cancel' => [
                        'type' => 'button', 'options' => [
                            'label' => 'ekyna_core.button.cancel',
                            'button_class' => 'default',
                            'as_link' => true,
                            'attr' => [
                                'class' => 'form-cancel-btn',
                                'icon' => 'remove',
                                'href' => $cancelPath,
                            ],
                        ],
                    ],
                ],
            ])
        ;

        $form->handleRequest($this->getRequest());
        if ($form->isValid()) {

            $this->getRepository()->persistAsLastChildOf($child, $resource);

            // TODO use ResourceManager
            $event = $this->getOperator()->create($child);

            /* if ($request->isXmlHttpRequest()) {
                if ($event->hasErrors()) {
                    $errorMessages = $event->getErrors();
                    $errors = [];
                    foreach ($errorMessages as $message) {
                        $errors[] = $message->getMessage();
                    }
                    return new JsonResponse(array('error' => implode(', ', $errors)));
                }

                return new JsonResponse(array(
                    'id' => $resource->getId(),
                    'name' => (string)$resource,
                ));
            }*/

            $event->toFlashes($this->getFlashBag());

            if (!$event->hasErrors()) {
                /** @noinspection PhpUndefinedMethodInspection */
                if ($form->get('actions')->get('saveAndList')->isClicked()) {
                    $redirectPath = $this->generateResourcePath($resource, 'list');
                } elseif (null === $redirectPath = $form->get('_redirect')->getData()) {
                    $redirectPath = $this->generateResourcePath($child);
                }
                return $this->redirect($redirectPath);
            }
        }

        return $this->render(
            $this->config->getTemplate('new_child.html'),
            $context->getTemplateVars([
                'child' => $child,
                'form' => $form->createView()
            ])
        );
    }