I'm looking to add a form inside a custom template. The form, if I open the path registered inside the file cmodule.routing.yml, works but if i try to use it inside a custom template it doesn't work: with a dump(form) or dump(form.anyfield) i get NULL.
cmodule.routing.yml:
cmodule.manageform:
path: '/cmodule/manageform'
defaults:
_title: 'Documents'
_form: '\Drupal\cmodule\Forms\RequestForm'
requirements:
_permission: 'access content'
cmodule.manage:
path: '/cmodule/manage'
defaults:
_controller: '\Drupal\cmodule\Controller\CmoduleController::manageAction'
_title: 'Custom Documents'
requirements:
_permission: 'access content'
RequestForm.php:
/**
* {@inheritdoc}
*
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['type'] = array(
'#type' => 'radios',
'#options' => array(
'0' =>t('DOC'),
'1' =>t('PDF')
),
);
$form['pid'] = array(
'#type' => 'textfield',
'#title' => t('ID:'),
'#required' => FALSE
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Submit'),
'#attributes'=> ['class'=>['glyphicon', 'glyphicon-search']],
);
return $form;
}
cmodule.module:
/**
* Implements hook_theme().
*/
function cmodule_theme(){
$templates = array(
'manage_cmodule_page' => array(
'variables' =>
[
'id' => NULL,
'form' => NULL
],
'template' =>'manage_cmodule',
'render element' => 'form'
)
);
return $templates;
}
the controller contains:
public function manageAction() {
$id = 1;
$form = \Drupal::formBuilder()->getForm('Drupal\cmodule\Forms\RequestForm');
$form['type']['#title_display'] = 'invisible';
return [ '#theme' => 'manage_cmodule_page',
'#form' => $form,
'#id' => $id,
];
}
and then the template/view (contains):
<div>
{{ form.form_token }}
{{ form.form_build_id }}
{{ form.form_id }}
{{form.type}}
{{form.submit}}
</div>
etc..
But as I said above the template doesn't show the form, but show (if i print it) the #id value passed through the controller. Do you have any suggestions?!
UPDATE #1 I tried also to follow the suggestion of Beebee, and then this and this so the code is:
controller:
controller contains:
public function manageAction() {
$form = \Drupal::formBuilder()->getForm('Drupal\cmodule\Forms\RequestForm');
$render['#form'] = $form;
$render['theme'] = 'manage_cmodule_page';
return $render;;
}
and the implementation of hook_theme in .module file is:
return [
'manage_cmodule_page' => [
'template' =>'manage_cmodule',
'render element' => 'form',
]
];
The result is an empty page with an error inside the apache log:
[php7:error] [pid 12660:tid 1592] [client ::1:59521] PHP Fatal error: Out of memory (allocated 270532608) (tried to allocate 266354688 bytes) in C:\drupal\vendor\twig\twig\lib\Twig\Extension\Debug.php on line 60, referer: http://localhost/drupal/
if I remove the following row from the controller
$render['theme'] = 'manage_atps_page';
the page loads, but it is empty (without any form).
UPDATE #2
I fixed my issue following also the idea of Alex Kuzava. Thus I used the code of UPDATE 1, with the new directory of the form, and then I added these 3 lines inside the part of my template (otherwise the submit button doesn't work).
{{ form.form.form_build_id }}
{{ form.form.form_id }}
{{ form.form.form_token }}
id
is one of those things that'll always be set in a variable array. It's not a good indicator of whether something is working or not.