5

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 }} 
8
  • 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. Commented Jun 28, 2018 at 13:33
  • hi, I put here id var only to show you that others variables work. Commented Jun 28, 2018 at 14:46
  • 2
    Possible duplicate of Send a form to twig template Commented Jul 2, 2018 at 11:02
  • This question gets asked a lot lately. Also see drupal.stackexchange.com/a/263382/15055 Commented Jul 2, 2018 at 11:03
  • @leymannx i already saw both links; in one of them i also put a question (i cannot create comment yet), but it has been removed with the suggestion to create a new question. Commented Jul 2, 2018 at 11:40

2 Answers 2

6

'render element' => 'form'

Your render element is form which means all provided variables will be passed there.

In your twig template you can render your form like here

<div>{{ form.form }}</div> 

UPDATE #1

Wow man, you have wrong namespace Drupal\cmodule\Forms\RequestForm

Should be Drupal\cmodule\Form\RequestForm

Please rename Forms to Form (the same for directory) and clear cache. It should help you.

4
  • it doesn't work. please read the update #1 Commented Jul 2, 2018 at 12:04
  • @quele I have updated my answer. Please check Commented Jul 2, 2018 at 14:10
  • @quele any news? Commented Jul 3, 2018 at 9:39
  • I fixed it changing the Form directory, thank you! Commented Jul 4, 2018 at 12:25
3

See: https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api.php/function/hook_theme/8.2.x

Each information array must contain either a 'variables' element (for using a #theme element) or a 'render element' element (for render elements), but not both.

Your manage_cmodule_page expects both variables and render element which is not allowed. Removing variables array from your hook_theme should make this work, provided the rest of the code is functional.

1
  • 1
    I updated my question, because it still doesn't work. Commented Jul 2, 2018 at 13:38

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.