Drupal Crud Sample Module
modules\contrib\basic_crud_operation_in_drupal_sites\my_crud.info.yml
name: my_crud
type: module
description: simple crud operation
package: Custom
core_version_requirement: ^8 || ^9 || ^10
# Information added by Drupal.org packaging script on 2022-10-08
version: '1.0.1'
project: 'basic_crud_operation_in_drupal_sites'
datestamp: 1665204829
modules\contrib\basic_crud_operation_in_drupal_sites\my_crud.install
<?php
/**
* @file
* Install, update and uninstall functions for the my_crud module.
*/
/**
* Implements hook_install().
*/
function my_crud_install() {
\Drupal::messenger()->addStatus(__FUNCTION__);
}
/**
* Implements hook_uninstall().
*/
function my_crud_uninstall() {
\Drupal::messenger()->addStatus(__FUNCTION__);
}
/**
* Implements hook_schema().
*/
function my_crud_schema() {
$schema['my_crud'] = [
'description' => 'Table description.',
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique record ID.',
],
'claimsnumber' => [
'type' => 'varchar',
'length' => '40',
'not null' => TRUE,
'description' => 'Claims Number',
],
'patientname' => [
'type' => 'varchar',
'length' => '40',
'not null' => TRUE,
'description' => 'Patient Name',
],
'type' => [
'type' => 'varchar',
'length' => '64',
'not null' => TRUE,
'default' => '',
'description' => 'Service Type',
],
'providername' => [
'type' => 'varchar',
'length' => '40',
'not null' => TRUE,
'description' => 'Provider Name',
],
'claimsvalue' => [
'type' => 'varchar',
'length' => '64',
'not null' => TRUE,
'default' => '',
'description' => 'Claims Value',
],
'date' => [
'type' => 'varchar',
'length' => '64',
'not null' => TRUE,
'default' => '',
'description' => 'Submission date',
],
],
'primary key' => ['id'],];
return $schema;
}
/**
* Implements hook_requirements().
*/
function my_crud_requirements($phase) {
$requirements = [];
if ($phase == 'runtime') {
$value = mt_rand(0, 100);
$requirements['my_crud_status'] = [
'title' => t('my_crud status'),
'value' => t('my_crud value: @value', ['@value' => $value]),
'severity' => $value > 50 ? REQUIREMENT_INFO : REQUIREMENT_WARNING,
];
}
return $requirements;
}
modules\contrib\basic_crud_operation_in_drupal_sites\my_crud.module
<?php
/**
* @file
* Primary module hooks for my_crud module.
*
* @DCG
* This file is no longer required in Drupal 8.
* @see https://www.drupal.org/node/2217931
*/
function my_crud_menu()
{
$item['my_crud']=array('title' => 'List Records', 'description'=>'', 'route'=>
'my_crud.mycrud_controller_listing');
$item['my_crud/form/data']=array('title' => 'Claim Form', 'description'=>'', 'route'=>
'my_crud.mycrud_form');
$item['my_crud/form/delete/{cid}']=array('title' => 'Delete Claim', 'description'=>'', 'route'=>
'my_crud.delete_form');
return $item;
}
modules\contrib\basic_crud_operation_in_drupal_sites\my_crud.routing.yml
my_crud.mycrud_controller_listing:
path: 'my_crud'
defaults:
_controller: 'Drupal\my_crud\Controller\MycrudController::Listing'
_title: 'Claim Records'
requirements:
_permission: 'access content'
my_crud.mycrud_form:
path: 'my_crud/form/data'
defaults:
_form: 'Drupal\my_crud\Form\MycrudForm'
_title: 'Claim Form'
requirements:
_permission: 'TRUE'
my_crud.delete_form:
path: 'my_crud/form/delete/{cid}'
defaults:
_form: 'Drupal\my_crud\Form\DeleteForm'
_title: 'Delete Claim'
requirements:
_permission: 'TRUE'
modules\contrib\basic_crud_operation_in_drupal_sites\src\Controller\MycrudController.php
<?php
/**
* @file
* Install, update and uninstall functions for the my_crud module.
*/
namespace Drupal\my_crud\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Database;
use Drupal\Core\Url;
use Drupal\Core\Messenger;
use Drupal\Core\Link;
class MycrudController extends ControllerBase
{
public function Listing()
{
// table apache_request_headers
$header_table = ['id' => t('ID'), 'claimsnumber' => t('Claims Number'), 'patientname' => t('Patient Name'), 'type' => t('Service Type'), 'providername' => t('Provider Name'), 'claimsvalue' => t('Claims Value'), 'date' => t('Submission Date'), 'opt' => t('Edit Claim'), '
opt1' => t('Delete Claim'),];
$row = [];
$conn = Database::getConnection();
$query = $conn->select('my_crud', 'm');
$query->fields('m', ['id', 'claimsnumber', 'patientname', 'type', 'providername', 'claimsvalue', 'date']);
$result = $query->execute()->fetchAll();
foreach ($result as $value) {
$delete = Url::fromUserInput('/my_crud/form/delete/' . $value->id);
$edit = Url::fromUserInput('/my_crud/form/data?id=' . $value->id);
$row[] = ['id' => $value->id, 'claimsnumber' => $value->claimsnumber, 'patientname' => $value->patientname, 'type' => $value->type, 'providername' => $value->providername,'claimsvalue' => "$".$value->claimsvalue, 'date' => $value->date,'opt' => Link::fromTextAndUrl('Edit', $edit)->toString(), 'opt1' => Link::fromTextAndUrl('Delete', $delete)->toString(),];
}
$add = Url::fromUserInput('/my_crud/form/data');
$text = "Add Claim";
$data['table'] = ['#type' => 'table', '#header' => $header_table, '#rows' => $row, '#empty' => t('
No record found'), '#caption' => Link::fromTextAndUrl($text, $add)->toString(),];
$this->messenger()->addMessage('Records Listed');
return $data;
}
}
modules\contrib\basic_crud_operation_in_drupal_sites\src\Form\DeleteForm.php
<?php
/**
* @file
* Install, update and uninstall functions for the my_crud module.
*/
namespace Drupal\my_crud\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Url;
use Drupal\Core\Messenger;
use Drupal\Core\Link;
class DeleteForm extends ConfirmFormBase {
public function getformId(){
return 'delete_form';
}
public $cid;
public function getQuestion() {
return t('Delete Record?');
}
public function getCancelUrl(){
return new Url('my_crud.mycrud_controller_listing');
}
public function getDescription() {
return t('Are you sure you want to delete record?');
}
public function getConfirmText() {
return t('Delete it');
}
public function getCancelText(){
return t('Cancel');
}
public function buildForm(array $form, FormStateInterface $form_state, $cid = NULL)
{
$this->id = $cid;
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state){
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state){
$query = \Drupal::database();
$query->delete('my_crud')->condition('id',$this->id)->execute();
$this->messenger()->addMessage('Successfully deleted record');
$form_state->setRedirect('my_crud.mycrud_controller_listing');
}
}
modules\contrib\basic_crud_operation_in_drupal_sites\src\Form\MycrudForm.php
<?php
namespace Drupal\my_crud\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Database\Database;
use Drupal\Core\Url;
use Drupal\Core\Messenger;
use Drupal\Core\Link;
class MycrudForm extends FormBase
{
public function getFormid()
{
return 'mycrud_form';
}
public function buildform(array $form, FormStateInterface $form_state)
{
$conn = Database::getConnection();
$record = [];
if (isset($_GET['id'])) {
$query = $conn->select('my_crud', 'm')->condition('id', $_GET['id'])->fields('m');
$record = $query->execute()->fetchAssoc();
if($record['type'] == "Medical"){
$record['type'] = 1;
}else{
$record['type'] =2;
}
}
// $form['claimsnumber'] = ['#type' => 'hidden', '#default_value' => (isset($record['claimsnumber']) && $_GET['id']) ? $record['claimsnumber'] : '',];
$form['patientname'] = ['#type' => 'textfield','#attributes' => array('placeholder' => t('Patient Name') ), '#required' => TRUE, '#default_value' => (isset($record['patientname']) && $_GET['id']) ? $record['patientname'] : '',];
$form['type'] = ['#type' => 'select','#options' => array(t('--- Select Service Type---'), t('Medical'), t('Dental')), '#required' => TRUE, '#default_value' => (isset($record['type']) && $_GET['id']) ? $record['type'] : '',];
$form['providername'] = ['#type' => 'textfield','#attributes' => array('placeholder' => t('Provider Name') ), '#required' => TRUE, '#default_value' => (isset($record['providername']) && $_GET['id']) ? $record['providername'] : '',];
$form['claimsvalue'] = ['#type' => 'textfield','#attributes' => array('placeholder' => t('Claims Value') ), '#required' => TRUE, '#default_value' => (isset($record['claimsvalue']) && $_GET['id']) ? $record['claimsvalue'] : '',];
$form['date'] = ['#type' => 'hidden', '#default_value' => (isset($record['agdatee']) && $_GET['id']) ? $record['date'] : '',];
$form['action'] = ['#type' => 'action',];
$form['action']['submit'] = ['#type' => 'submit', '#value' => t('Save'),];
$form['action']['reset'] = ['#type' => 'button', '#value' => t('Reset'), '#attributes' => ['onclick' => '
this.form.reset(); return false;',],];
$link = Url::fromUserInput('/my_crud/');
$form['action']['cancel'] = ['#markup' => Link::fromTextAndUrl(t('Back to page'), $link, ['
attributes' => ['class' => 'button']])->toString(),];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state)
{
/* $name = $form_state->getValue('name');
if (preg_match('/[^A-Z[ ]a-z]/', $name)) {
$form_state->setErrorByName('name', $this->t('Name must be in Characters Only'));
}
$age = $form_state->getValue('age');
if (!preg_match('/[^A-Za-z]/', $age)) {
$form_state->setErrorByName('age', $this->t('Age must be in Numbers only'));
}
parent::validateForm($form, $form_state);*/
}
public function submitForm(array &$form, FormStateInterface $form_state)
{
$field = $form_state->getValues();
$pin = rand(10000000,20000000);
$numArray = explode(" ", $pin);
$arraySum = array_sum($numArray);
$remainder = $arraySum % 9;
$difference = 9 - $remainder;
$claimsnumber = $pin.$difference;
// $claimsnumber = $field['claimsnumber'];
$patientname = $field['patientname'];
$type = $field['type'];
if ($type == 1){
$type= "Medical";
}else{
$type= "Dental";
}
$providername = $field['providername'];
$claimsvalue = $field['claimsvalue'];
$current_time = \Drupal::time()->getCurrentTime();
//To return 30/04/2019
$date = date('Y-m-d H:i:s', $current_time);
// $date = $field['date'];
if (isset($_GET['id'])) {
$field = [ 'patientname' => $patientname,'type' => $type, 'providername' => $providername,'claimsvalue' => $claimsvalue, 'date' => $date,];
$query = \Drupal::database();
$query->update('my_crud')->fields($field)->condition('id', $_GET['id'])->execute();
$this->messenger()->addMessage('Successfully updated records');
} else {
$field = ['claimsnumber' => $claimsnumber, 'patientname' => $patientname,'type' => $type, 'providername' => $providername,'claimsvalue' => $claimsvalue, 'date' => $date,];
$query = \Drupal::database();
$query->insert('my_crud')->fields($field)->execute();
$this->messenger()->addMessage('Successfully saved records');
}
$form_state->setRedirect('my_crud.mycrud_controller_listing');
}
}
Share this