How to create a custom filter form and update custom table data in Drupal


Here we are going to create add/edit form and save data in a custom table and then list data  with a filter form.

So assuming you have custom module . here I have a custom module with name dn_students.

First we are going to create a table , say students is the table name. It has below fields

We dividing this article in to below steps

  • List students in a HTML table
  • Filter form for students list
  • Add/edit and delete rows

List students in a HTML table

Assuming  data base table has some data . here we are going to list students data in a table with edit and delete link, we are creating a URL for listing students  and mapping this URL with and a controller and a function in controller which is used to render  data in a html form.

First we are going to list all data in students table for this we have to add below code in your  .routing.yml file.

We have provided URL permission as administer students.  So users having this permission can only access the path  /adminatructure/dn_students/students.

In above we have mentioned controller name so we have to create StudentController. For  this create a Src folder inside your custom module and create a Controller folder. Inside Controller folder create a php file and name it as StudentController.php.

We have mentioned function   listStudents function router files. So we have to implement  listStudent() function in StudentController.

So list function StudentController will be looks like as below code.

Before we are creating table we need to query student data from table to populate rows in table.
We are creating a common function in .module file for filter form data and listing all data.
Please see code below.

As shown above $opt parameter determines whether full data to be shown or based on the parameters $fname and $marks.
Variable $delete and $edit used of generating delete and edit respectively for each row in table. Above function return an array $ret with students details.

Now we are again going to StudentController and create logic to create table form in listStudents function.
Implement listSrudents function as shown in below.

Add some dummy data in data base table and view link http://localhost/digitalnadeemDrupal1/admin/structure/dn_students/students
Here digitalnadeemDrupal1 is my project name. So you can see below table in Admin screen.

Filter form for students list

No we are going to create filter form just above list table.

For this we have created a file under Form folder as shown below.

Implementation of form using formbase interface is shown below.

Now we want to show this form just above list table in admin screen. For this go to your listStudents function in StudentController.php

Now you see filter form in students list page.
You can filter values by providing first name and marks.

Add/edit and delete rows

Next we are going to provide functionality for edit and delete button. Also we are using single form for add and edit functionality.

Your edit and delete links have below path



Now it will show page not found.

So first we are going to create routers for above links.


Next we have to create Student Form and StudentDeleteForm in Form folder.
See below code for Student Form.

Here we are handling both edit and add functionality. if parameter present , treats as an edit form by loading field data from database.
Next StudentDeleteForm.php has been created as below.

Next we are moving to StudentController.php file and add below function.

Also add below code after header variable so that admin can have links to add form and list student s page.

So complete code of StudentCotroller.php is shown below.

So now you can edit , delete and add entries.


Drupal 8 – You can download above explained sample code here. 

Drupal 9 – You download the above-explained sample code here. some functions. deprecated in Drupal 9

Get Free E-book
Get a free Ebook on Drupal 8 -theme tutorial
I agree to have my personal information transfered to MailChimp ( more information )
if you like this article Buy me a Coffee this will be an ispiration for me to write articles like this.

You may also like...