Step by step Guide to create a custom form in Drupal 8

,

In Drupal 8 form API is similar to Drupal 7 Form API. forms still uses array structure to render the data. but having separate validation and submission form. Drupal 8 has some new (HTML 5) elements available.  

New HTML 5 elements like

'tel','email','number','date','url','search','range' etc.

In Drupal 8 Form classes implement the \Drupal\Core\Form\FormBuilderInterface and the basic workflow of a form is defined by the buildForm, validateForm, and submitForm methods of the interface.

There are a different classes to choose depending on the type of form you are creating.


  1. ConfigFormBase : For creating system configuration forms like the one found at admin/config/system/site-information.
  2. ConfirmFormBase : For providing users with a form to confirm an action such as deleting a piece of content.
  3. FormBase : The most generic base class for generating forms.

Step 1: Create .info.yml file

An essential part of a Drupal 8 module, theme, or install profile is the .info.yml file (aka, "info yaml file") to store metadata about the project.

In Drupal 8, .info file changes to .info.yml. Old .info files have been converted to YAML.

Added name, description, core, package, dependencies, type (The type key, which is new in Drupal 8, is required and indicates the type of extension, e.g. module, theme or profile.

name: Resume
description: A Resume form to enter user details.
core: 8.x
package: Custom
type: module


Step 2: Creating .routing.yml

This routing system replaces the routing parts of hook_menu() in Drupal 7. The parts of hook_menu() that were used for creating menu entries, tabs.

A route is a path which return some sort of content on.

For example, the default front page, '/node/2' is a route. When Drupal receives a request, it tries to match the requested path to a route it knows about. If the route is found, then the route's definition is used to return content. Otherwise, Drupal returns a 404.

resume.form:
 path: '/resume/myform'
 defaults:
   _title: 'Application form'
   _form: '\Drupal\resume\Form\ResumeForm'
 requirements:
   _permission: 'access content'

More Info Click Here



Related Post


Latest Post


Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.