1

I have created a wrapper in form of a "page" controller for my entity's form because I needed to do some tasks before it was rendered.

I ended up with this:

<?php
  class MyEntityController extends ControllerBase {
    public function mymethod(AnotherEntity $anotherEntity) {
      $myentity = $this->entityManager()->getStorageController('myentity')->create(array());
      $form = $this->entityManager()->getFormController('myentity', 'default');
      $form->setEntity($myentity);
      $form->setParentEntity($anotherEntity);
      $form->setRedirectRoute(array(
        'route_name' => 'some.route',
        'route_parameters' => array('somearugment' => $anotherEntity->id()
      ));
      return $this->formBuilder()->getForm($form);
    }
  }
?>

Is this the recommended way in regard of the entity form itself(ie. manually setting the form's entity object)?

1
  • I'm not sure, but it might be easier to subclass HtmlEntityFormController/FormController, which is the class used to display a default entity forms. Then you could just override the method there and add your own content to it. And if not, looking at that will at least tell you how core calls entity forms.
    – Berdir
    Mar 20 '14 at 21:16
1
<?php
class MyEntityController extends ControllerBase {
  public function mymethod(AnotherEntity $anotherEntity) {
    // @todo The methods don't exist in core, so it's not clear what object they
    //   should be called on.
    //$form->setParentEntity($anotherEntity);
    //$form->setRedirectRoute(array(
    //  'route_name' => 'some.route',
    //  'route_parameters' => array('somearugment' => $anotherEntity->id()
    //));

    $myentity = $this->entityManager()->getStorageController('myentity')->create(array());
    return $this->entityFormBuilder()->getForm($myentity, 'default');
  }
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy