Warning message

Newsletter category Drupal Developer newsletter format has not been set to HTML. There will be no statistics recorded for this newsletter.

Drupal8: Programmatically create a form and block

This CodeLet let you programmatically create a form page and render same form on a block.
Create custom module and follow CodeLet instruction to create a form and block programmatically.

Module Info

name: Custom module
description: 'Custom module for Enhance feature'
type: module
core: 8.x

CodeLet: 

Create a file name with "custom.routing.yml" on your module's root directory and place bellowed code on it.

custom.my-custom-form:
path: '/my-custom-form'
defaults:
_form: 'Drupal\custom\Form\MyCustomForm'
_title: 'Custom Form'
requirements:
_permission: 'access content'

Drupal8 form: let’s define our simple MyCustomForm class inside src/Form/MyCustomForm.php
/**
* @file
* Contains \Drupal\custom\Form\MyCustomForm.
*/

namespace Drupal\custom\Form;

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

/**
* My Custom form.
*/
class MyCustomForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'my_custom_form';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {

$form['name'] = array(
'' => 'textfield',
'' => 15,
'' => t('Name'),
);

$form['description'] = array(
'' => 'textarea',
'' => TRUE,
'' => t('Description'),
);

$form['email'] = array(
'' => 'email',
'' => TRUE,
'' => t('E-mail address'),
);

$form['submit'] = array(
'' => 'submit',
'' => t('Submit'),
);

return $form;
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($form_state->getValue('name') == '' ) {
$form_state->setErrorByName('name', t('This is a required field.'));
}
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {

$name = $form_state->getValue('name');
$description = $form_state->getValue('description');
$email = $form_state->getValue('email');

$message = t('Your information has been successfully submitted.') ;
drupal_set_message($message);
}
}

Drupal8 block: let’s define our simple CustomBlock class inside src/Plugin/Block/CustomBlock.php

This "Custom Block" will be available at admin block interface you can place it on any region.
/**
* @file
* Contains \Drupal\custom\Plugin\Block\CustomBlock.
*/
namespace Drupal\custom\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides Custom Block.
*
* @Block(
* id = "my_custom_block",
* admin_label = @Translation("Custom Block"),
* category = @Translation("Blocks")
* )
*/
class CustomBlock extends BlockBase {

/**
* {@inheritdoc}
*/
public function build() {
$build = array();

$build[''] = '' . t('My Custom Form') . '';
$build['form'] = \Drupal::formBuilder()->getForm('Drupal\custom\Form\MyCustomForm');

return $build;
}
}

By saru1683 on Fri, 08/01/2016 - 11:49am

Add comment

Log in or register to post comments
QR code for this page URL This page URL
Feedback