I'm newby in Symfony, so excuse my ignorance.
I have a parent Entity "Article" and sub-entites like "Page", "News" etc. They have common fields like title, date etc.
I created initial form for Article (in ArticleAdmin class) where's possible to choose title and type of sub-entity, and tried to call sub-entity's Admin class and pass there POST data. But this didn't work, because createAction in Controller is responsible for both the form rendering and for its processing, and when i try to override it in my createAction() i get an error "Error: Call to private method Sonata\AdminBundle\Controller\CRUDController::setFormTheme()"
Here's my code:
ArticleAdmin - parent entity
<?php
namespace A26\CMS\ContentBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
class ArticleAdmin extends AbstractAdmin
{
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title', 'text', array(
'label' => 'Title'
))
->add('slug', 'text', array(
'label' => 'Slug'
))
->add('type', 'choice', array(
'choices' => array(
'Page' => 'Text Page',
'News' => 'News'
),
));
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('title');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('title')
->add('is_publish');
}
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('title');
}
}
?>
PageAdmin - sub-entity
<?php
namespace A26\CMS\PagesBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Ivory\CKEditorBundle\Form\Type\CKEditorType;
class PageAdmin extends AbstractAdmin
{
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->tab('Content')
->with('Content')
->add('title', 'text', array(
'label' => 'Title'
))
->add('content', CKEditorType::class, array(
'label' => 'Content'
))
->end()
->end();
}
// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('title');
}
// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('title');
}
// Fields to be shown on show action
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('title');
}
}
?>
ArticleAdmin Controller - i copied CRUDController and replaced block
if ($form->isSubmitted()) {...}
<?php
namespace A26\CMS\ContentBundle\Controller;
use Sonata\AdminBundle\Controller\CRUDController as Controller;
class ArticleAdminController extends Controller
{
public function createAction()
{
$request = $this->getRequest();
// the key used to lookup the template
$templateKey = 'edit';
$this->admin->checkAccess('create');
$class = new \ReflectionClass($this->admin->hasActiveSubClass() ? $this->admin->getActiveSubClass() : $this->admin->getClass());
if ($class->isAbstract()) {
return $this->render(
'SonataAdminBundle:CRUD:select_subclass.html.twig',
array(
'base_template' => $this->getBaseTemplate(),
'admin' => $this->admin,
'action' => 'create',
),
null,
$request
);
}
$object = $this->admin->getNewInstance();
$preResponse = $this->preCreate($request, $object);
if ($preResponse !== null) {
return $preResponse;
}
$this->admin->setSubject($object);
/** @var $form \Symfony\Component\Form\Form */
$form = $this->admin->getForm();
$form->setData($object);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$response = $this->forward("A26CMSPagesBundle:PageAdmin:create", array('_sonata_admin' => $this->container->get('request')->get('_sonata_admin')));
dump($response);
die();
}
$formView = $form->createView();
// set the theme for the current Admin Form
$this->setFormTheme($formView, $this->admin->getFormTheme());
return $this->render($this->admin->getTemplate($templateKey), array(
'action' => 'create',
'form' => $formView,
'object' => $object,
), null);
}
}
Help, please! Maybe it can be implemented another way?
Thanks in advance!!