0

I want to make custom page in drupal with form, I checked few examples showing page in drupal via module and example of form via module, i tried to merge them but no success. I made a custom page using drupal module with template (twig) file.

Below is my controller code

namespace Drupal\booktestdrive\Controller;
use Drupal\Core\Controller\ControllerBase;

class BiveController extends ControllerBase {
  public function show() {
    $form_class = '\Drupal\booktestdrive\Form\TestForm';
    return [
        '#theme' => 'bive_page_theme',
        '#form' => \Drupal::formBuilder()->getForm($form_class),
    ];
  }
}

Template file code

bive_page_theme.html.twig

<section> hii
{{ form }}

</section>

i also created form at src/form/BiveForm.php

namespace Drupal\bive\Form;

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

class BiveForm extends FormBase {
  public function getFormId() {
    return 'booktestdrive_form';
  }
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['first_name'] = array(
      '#type' => 'textfield',
      '#attributes' =>array('placeholder' => t('First Name*')),
      '#required' => TRUE,
    );

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

    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this->t('Save'),
      '#button_type' => 'primary',
    );
    return $form;
  }

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

      if (strlen($form_state->getValue('candidate_number')) < 10) {
        $form_state->setErrorByName('candidate_number', $this->t('Mobile number is too short.'));
      }

    }


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

   // drupal_set_message($this->t('@can_name ,Your application is being submitted!', array('@can_name' => $form_state->getValue('candidate_name'))));

    foreach ($form_state->getValues() as $key => $value) {
      drupal_set_message($key . ': ' . $value);
    }

   }
}

Routing.yml

bdrive.book:
  path: '/brive'
  defaults:
    _controller: 'Drupal\brive\Controller\BriveController::book'
    _title: 'Test'
  requirements:
    _permission: 'access content'

brive.form:
 path: '/brive-form'
 defaults:
   _title: 'Application form'
   _form: '\Drupal\booktestdrive\Form\BriveForm'
 requirements:
   _permission: 'access content'    
0

You need define new function theme bive_page_theme before call it. You can see docs and example. In your case, you can write more code in file .module

/**
 * Implements hook_theme().
 */
function Your_Module_theme($existing, $type, $theme, $path) {
    return [
        'bive_page_theme' => [
            'variables' => [
              'form' => NULL,
            ],
            'path' => $path . '/templates',
        ]
    ];
}

and create file bive_page_theme.html.twig in folder templates of module. Clear cache and go to page.

2
  • hey i am already created page with module i want to show custom form on my page
    – gaurav
    Aug 24 '16 at 9:32
  • What's code in file routing.yml of you?
    – MrD
    Aug 24 '16 at 9:38
0

use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Render\Renderer; //Here declare your class class TestController extends ControllerBase { protected $renderer; public function __construct(Renderer $renderer) { $this->renderer = $renderer; }

public static function create(ContainerInterface $container) {
    return new static(
        $container->get('renderer')
    );

public function content() { $markup = $this->renderer->render(\Drupal::formBuilder()->getForm('Drupal\questions\Form\QuestionForm'));

   return [
  '#type' => 'markup',
  '#markup' => $markup,
];

} } in your controller you can do like this also

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.