I'd like to create different fields configuration for create and edit actions in Sonata Admin Bundle.
Is there any way to determine it except checking $this->getSubject()->getId()
in Sonata\AdminBundle\Admin\Admin::configureFormFields()
?
Picoss
You can also do this:
protected function configureFormFields(FormMapper $formMapper) {
if ($this->isCurrentRoute('create')) {
// CREATE
}
else {
// EDIT
}
}
Roberto
with:
if($this->getRequest()->get($this->getIdParameter()) == null){
// create
} else {
// edit
}
I use this :
$creationMode = ($this->id($this->getSubject()))?(false):(true);
if ($creationMode){
//Ok
}
In sonata admin from version 3.x
if ($this->isCurrentRoute('create')) {
// CREATE
}
else {
// EDIT
}
In sonata admin before version 3.x use:
$subject = $this->getSubject();
if ($subject->isNew()) {
// CREATE
}
else {
// EDIT
}
You can also do this:
protected function configureFormFields(FormMapper $formMapper) {
if ($this->isCurrentRoute('create')) {
// CREATE
}
else {
// EDIT
}
}
public function getAction(): ?string
{
if (! $this->getRequest()) {
return null;
}
$pathArray = \explode('/', $this->request->getPathInfo());
return \end($pathArray);
}
来源:https://stackoverflow.com/questions/17834765/is-there-any-way-to-determine-current-action-create-or-edit-in-sonata-adminbun