I need to programmatically generate the entity form for my custom entity - basically get the same output that happens when you call
\Drupal::formBuilder()->getForm('form namespace')
When I try to do that with my entity form I get the error:
Call to a member function getEntityTypeId() on null in Drupal\Core\Entity\EntityForm->getBaseFormId() (line 77 of core/lib/Drupal/Core/Entity/EntityForm.php).
Really stumped on how to proceed.
Here's an obfuscated version of code I'm using
Entity Definition:
/**
* Defines the My Entity entity.
*
* @ingroup my_module
*
* @ContentEntityType(
* id = "my_entity",
* label = @Translation("My Entity"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\my_module\MyEntityListBuilder",
* "views_data" = "Drupal\my_module\Entity\MyEntityViewsData",
* "translation" = "Drupal\my_module\MyEntityTranslationHandler",
*
* "form" = {
* "default" = "Drupal\my_module\Form\MyEntityForm",
* "add" = "Drupal\my_module\Form\MyEntityForm",
* "edit" = "Drupal\my_module\Form\MyEntityForm",
* "delete" = "Drupal\my_module\Form\MyEntityDeleteForm",
* },
* "access" = "Drupal\my_module\MyEntityAccessControlHandler",
* "route_provider" = {
* "html" = "Drupal\my_module\MyEntityHtmlRouteProvider",
* },
* },
* base_table = "my_entity",
* data_table = "my_entity_field_data",
* translatable = TRUE,
* admin_permission = "administer as my_entity entities",
* entity_keys = {
* "id" = "id",
* "label" = "name",
* "uuid" = "uuid",
* "uid" = "user_id",
* "langcode" = "langcode",
* "status" = "status",
* },
* links = {
* "canonical" = "/admin/structure/my_entity/{my_entity}",
* "add-form" = "/admin/structure/my_entity/add",
* "edit-form" = "/admin/structure/my_entity/{my_entity}/edit",
* "delete-form" = "/admin/structure/my_entity/{my_entity}/delete",
* "collection" = "/admin/structure/my_entity",
* },
* field_ui_base_route = "my_entity.settings",
* constraints = {
* "CustomContraint" = {}
* }
* )
*/
class MyEntity extends ContentEntityBase implements MyEntityInterface {
Form namespace
namespace Drupal\my_module\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
/**
* Form controller for My Entity edit forms.
*
* @ingroup my_module
*/
class MyEntityForm extends ContentEntityForm {
Okay, so this is what I've found so far. User oknate's answer partially works; I also had to add a dummy function to my container form class to get around a callback error, which looked like this:
public function processForm($element, FormStateInterface $form_state, $form)
{
return $element;
}
This is probably not the right way to do this, but it does result in a form object that will render in another form.