Is there any way to determine current action (create or edit) in Sonata\\AdminBundle\\Admin\\Admin::configureFormFields()?

陌路散爱 提交于 2019-12-03 10:03:21
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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!