cmodule.routing.yml: cmodule.routing.yml:
RequestForm.php: RequestForm.php:
/** * {@inheritdoc} * */ public function buildForm(array $form, FormStateInterface $form_state) { $form['type'] = [array( '#type' '#type' => 'radios', '#options' => [array( '0' => t '0' =>t('DOC'), '1' => t '1' =>t('PDF'), ] ), ]; ); $form['pid'] = [array( '#type' '#type' => 'textfield', '#title' '#title' => t('ID:'), '#required' => FALSE, ]; ); $form['submit'] = [array( '#type' '#type' => 'submit', '#value' '#value' => $this->t('Submit'), '#attributes' => ['class' => ['glyphicon''#attributes'=> ['class'=>['glyphicon', 'glyphicon-search']], ]; ); return $form; }
cmodule.module: cmodule.module:
/** * Implements hook_theme(). */ function atps_themecmodule_theme() { $templates = [array( 'manage_cmodule_page' => [array( 'variables' 'variables' => [ 'id' 'id' => NULL, 'form' => NULL, ], 'template' =>'template' 'manage_cmodule'=>'manage_cmodule', 'render element' => 'form', ], ) ]; ); return $templates; }
the controller contains: the controller contains:
public function manageAction() { $id = 1; $form = \Drupal::formBuilder()->getForm('Drupal\cmodule\Forms\RequestForm'); $form['type']['#title_display'] = 'invisible'; return [ return [ '#theme' => 'manage_cmodule_page', '#form' '#form' => $form, '#id' '#id' => $id, ]; }
and then the template/view (contains): and then the template/view (contains):
<div> {{ form.form_token }} {{ form.typeform_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 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).