18

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()?

Max Romanovsky
  • 2,824
  • 5
  • 30
  • 37

6 Answers6

31

You can also do this:

protected function configureFormFields(FormMapper $formMapper) {
  if ($this->isCurrentRoute('create')) {
    // CREATE
  }
  else {
    // EDIT
  }
}
Picoss
  • 2,047
  • 13
  • 14
2

with:

if($this->getRequest()->get($this->getIdParameter()) == null){
   // create
} else {
   // edit
}
Onema
  • 7,331
  • 12
  • 66
  • 102
Roberto
  • 532
  • 7
  • 20
1

I use this :

$creationMode = ($this->id($this->getSubject()))?(false):(true);
if ($creationMode){
 //Ok
}
djoo
  • 685
  • 1
  • 7
  • 24
1

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
  }
0

You can also do this:

protected function configureFormFields(FormMapper $formMapper) {
  if ($this->isCurrentRoute('create')) {
    // CREATE
  }
  else {
    // EDIT
  }
}
0
public function getAction(): ?string
{
    if (! $this->getRequest()) {
        return null;
    }
    $pathArray = \explode('/', $this->request->getPathInfo());

    return \end($pathArray);
}
guest
  • 1