0

my problem is that my drupal 8 form is not getting rendered in twig template.

I created a form with build form method in drupal 8 when i tried it to pass in twig template it is not getting passed.for example my code is

modulenameconfig.php

class modulenameConfigForm extends ConfigFormBase {

  /**

   * {@inheritdoc}

   */

  public function getFormId() {

    return 'modulename';

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

    $form = parent::buildForm($form, $form_state);
    $config = $this->config('modulename.settings');


    $form['first_name'] = array(

      '#type' => 'textfield',

      '#title' => $this->t('First Name'),

      '#default_value' => $config->get('modulename.first_name'),

      '#required' => TRUE,

    );


    $form['last_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Last Name'),
      '#default_value' => $config->get('modulename.last_name'),
      '#required' => TRUE,
    );
    return $form;

  }
 protected function getEditableConfigNames() {

    return [

      'modulename.settings',

    ];

  }
}

modulename.module

<?php


namespace Drupal\modulename\Form;

use Drupal\Core\Form\ConfigFormBase;

use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_menu()
*/



function modulename_theme() {
  return array(
    'modulename_form' => array(
      'render element' => 'form',
      'template' => 'modulename-form',
      'base hook' => 'form'
    ),
  );
}

my twig file in templates folder

modluename-form.html.twig

{%
  set body_classes = [
    logged_in ? 'user-logged-in',
    not root_path ? 'path-frontpage' : 'path-' ~ root_path|clean_class,
    node_type ? 'page-node-type-' ~ node_type|clean_class,
    db_offline ? 'db-offline',
  ]
%}                           


                           <h2>Personal Information</h2>    
                                    <form method="post">
                                        <div class="form-group">
                                            <label for="name">First Name</label>
                                            <div class="input-group">
                                              {{form.first_name}}

                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="name">Last Name</label>

                                            <div class="input-group">

                                                <input type="text" class="form-control" name="lname" id="lastname" placeholder="Last Name" required>

                                                <span class="input-group-addon danger"><span class="glyphicon icon-remove"></span></span>
                                            </div>
                                        </div>  

any help will be greatly appriciated help asap

2

This structure will not work with the autoloader right now - your namespaces and directory structure needs to follow these PSR-4 standards. (In particular, setting a namespace in your .module will cause Drupal to not find your hooks, which must be in the global namespace.)

I'm not sure I understand your use case fully, but it sounds like you want to:

  1. Create a form.
  2. Create a route that serves this form.
  3. Render the form with a custom template.

For the first, your form class should be something like this (and must be saved in a file matching the class name under modulename/src/Form/{ClassName}.php):

namespace Drupal\modulename\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

class MyModuleConfigForm extends ConfigFormBase {

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

  public function getEditableConfigNames() {
    return ['modulename.settings'];
  }

  public function buildForm(array $form, FormStateInterface $formState) {
    // build the form
    $form['...'] = [
      //...
    ]; 

    parent::buildForm();
    return $form;
  }

  public function submitForm(array $form, FormStateInterface $formState) {
    $this->config('modulename.settings')
      // set the values from the form
      ->set('...', $formState->getValue('...'))
      ->save();

    parent::submitForm($form, $formState);
  }

}

For the second part, you need a modulename.routing.yml file that looks something like this:

modulename.settings:
  path: '/admin/config/mymodule'
  defaults:
    _form: '\Drupal\modulename\Form\MyModuleConfigForm'
  requirements:
    _permission: 'administer site configuration'

(This is for admin configuration forms, since you mentioned ConfigFormBase. If you want a more general form and open it to anonymous users, extend FormBase instead and put _access: 'TRUE' in the route requirements.)

Finally, for the third part, you need to create your theme hook in modulename_theme(). (Your code already works, although the template key is superfluous as it matches the default.)

Then, modify the buildForm() as follows:

  public function buildForm(array $form, FormStateInterface $formState) {
    // build the form
    $form['...'] = [
      //...
    ]; 

    parent::buildForm();

    // *After* parent call.
    $form['#theme'] = 'modulename_form';

    return $form;
  }
  • Thanks Christoph . for helping me . but unfortunately your method is also not working. you mentioned modulename.theme. but in drupal8 module development modulename.module file implements the hook_theme() method. please check again my code. – gulshan Nov 27 '17 at 7:44
  • Hi, I am afraid there is a misunderstanding; I did not specify any modulename.theme file (which is not needed here, as there is no preprocessing). Most hooks including modulename_theme() must be defined in modulename.module, or they will not be found. – Christoph Burschka Nov 27 '17 at 8:56
  • hi Christoph . now my twig file is being called . but my custom css is not being applied . and please tell me how do we use bootstrap in drupal 8 module i.e how add it in modulename.libraries.yml file . and what is the use of form alter in this form. – gulshan Nov 28 '17 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.