How to render a form in custom twig/template in drupal 8?

custom_module.routing.yml

custom_module.form.page:
  path: '/custom-form'
  defaults:
    _form: '\Drupal\custom_module\Form\CustomModuleForm'
    _title: 'Custom Form'
  requirements: 
    _permission: 'access content'

 

CustomModuleForm.php

/**
 * @file
 * Contains \Drupal\custom_module\Form\CustomModuleForm.
 */

namespace Drupal\custom_module\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
 

class CustomModuleForm extends FormBase {

    public function getFormId() {
        return 'custom_form';
    }

    public function buildForm(array $form, FormStateInterface $form_state,) {
          $form=array();
         $form['first_name'] = array(
            '#type' => 'textfield',
            '#title' => t('First Name'),
            '#required' => TRUE,         );

         $form['last_name'] = array(
            '#type' => 'textfield',
            '#title' => t('Last Name'), 
            '#required' => TRUE,
          );

          $form['email'] = array(
            '#type' => 'email',
            '#title' => t('Email'),
            '#required' => TRUE,

           );

           $form['submit'] = array(
            '#type' => 'submit',
            '#value' => $this->t('Submit'),
          );
          $form['#theme'] = 'custom_form';
        
        return $form;
    }

    public function validateForm(array &$form, FormStateInterface $form_state) {
        
    }

    public function submitForm(array &$form, FormStateInterface $form_state) {
        
    }   

}

custom_module.module 

/**

* Implplements hook_theme().

*/

function custom_module_theme(){

}

custom-form.html.twig 

{{ form.form_build_id }}
{{ form.form_token }}
{{ form.form_id }}
{{ form.tags_fieldset }}


    {{ form.first_name }}


    {{ form.last_name }}


    {{ form.email }}


    {{ form.submit }}


 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.