I have a form defined in mymodule/src/Form/myForm.php
namespace DrupalmymoduleForm; use DrupalCoreFormFormBase; use DrupalCoreFormFormStateInterface; class myForm extends FormBase { /** * Specify the form ID. */ public function getFormId() { return 'mymodule_myform'; } /** * Build the actual form. */ public function buildForm(array $form, FormStateInterface $form_state) { // Form API code here...
Originally, this routing worked fine and the form displayed at the correct path:
mymodule.single_tab: path: '/node/{node}/edit/performance' defaults: _form: 'DrupalmymoduleFormmyForm' _title: 'Add performance'
Then I needed to add a custom access check, restricting to a specific content type:
mymodule.single_tab: path: '/node/{node}/edit/performance' defaults: _title: 'Add performance' _controller: 'DrupalmymoduleControllermymoduleController::content' requirements: _custom_access: 'DrupalmymoduleControllermymoduleController::checkAccess'
The access check works fine. Now I think I have to display my form in the controller:
namespace DrupalmymoduleController; use DrupalCoreControllerControllerBase; use DrupalCoreFormFormBuilderInterface; use DrupalCoreAccessAccessResult; use DrupalnodeEntityNode; /** * Controller for Add Performance single tab. */ class mymoduleController extends ControllerBase { /** * Returns the add single performance form. */ public function content() { $form = $this->formBuilder->getForm('DrupalmymoduleFormmyForm'); return [ '#type' => 'markup', '#markup' => $form ]; }
This fails to display the form, and I get:
Call to a member function getForm() on null
I tried defining my form builder class inside the controller instead, but same result. What am I doing wrong?