Monday, September 7, 2020

Drupal 8 Module to programmatically create a database and tables

 The objective of this module was to programmatically create a database of Metsi employees, their respective departments and the location of the departments.  Metsi has sixteen employees in four departments located in two buildings in the Ottawa, Canada region.  The departments are software development, new software testing, IT Education, and software promotion.

There are two tables in the database that were created programmatically using hook schema: employee name and department, and department name and location.

The names of the employees were then outputted to a table with 8 employees per page with the option to go to the next page.

The following screenshot shows the table of Metsi employees with a table header and a pager.

New fields and data were added to the tables as updates using Drush.



Using Drupal 8 to expose database fields to Views

There are two tables in this database each with four fields.  The objective of this project was to extract the information in those fields using Views.  Because data in the table fields are different, the tables had to be joined in order to extract the information from both table fields.  The relationship feature of Views was used for this. 

To get going, a new view was created in the Admin->Structure->Views menu.  A table was created in Views and the database table fields were extracted and added to the Views table.  The table below shows the result using Views.  The information was organized as follows: player ID, player name, team ID, team name and team description.  A pager was added to the Views table set to allow 8 items per page.

Provisions were included for filters and sorters to be applied to the extracted data.

Views table showing data from two joined database tables