1)creating hook menu hook_menu 2)permission 3)Collecting data through form 4)Form Validation 5)create node on submit 6)Create a new user on submit 7)Insert and update on submit 8)Theme table display_table 9)create taxonomy 10)delete records
Steps for programmatically creating node in drupal7,
1. Create a new node object.
2. Save the object using the node_save() function.
Basic Node Creation :
$complaint_body = 'Your node complaint body text';
$node = new stdClass(); // Create a new node object
$node->type = 'company'; // Content type
$node->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
node_object_prepare($node); //Set some default values
$node->title = 'Your node title';
$node->body[$node->language][0]['value'] = $complaint_body;
$node->body[$node->language][0]['summary'] = text_summary($complaint_body);
$node->body[$node->language][0]['format'] = 'full_html';
$node->status = 1; // (1 or 0): published or unpublished
$node->promote = 0; // (1 or 0): promoted to front page or not
$node->sticky = 0; // (1 or 0): sticky at top of lists or not
$node->comment = 1; // 2 = comments open, 1 = comments closed, 0 = comments hidden
// Add author of the node
$node->uid = 1;
// Set created date
$node->date = 'complaint_post_date';
$node->created = strtotime('complaint_post_date');
$path = 'content/mytest-' . date('YmdHis');
$node->path = array('alias' => $path);
// Save the node
node_save($node);
Add custom fields
$node->field_complaint_address[LANGUAGE_NONE][0]['value'] = 'This is custom cck filed to get complaint address';
Add a node reference to a node
$node->field_review_company_name[$node->language][0]['nid'] = 'company_node_id';
Add a term reference to a node
$node->field_company_industry[$node->language][0]['tid'] = 'company_term_id';
Add file / image fields
// Create image File object and associate with Image field.
$file = new StdClass();
$file->uid = 1;
$file->uri = $imagepath;
$file->filemime = file_get_mimetype($file->uri);
$file->status = 1;
$file->display = 1;
$file->description = "";
$dest = file_default_scheme() . '://complaint_image_directory';
$file = file_copy($file, $dest);
$node->field_complaint_image[LANGUAGE_NONE][0] = (array)$file;function delete_record_confirm($form, &$form_state){
$form['delete'] = array(
'#type' => 'value',
'#value' =>arg(1),
);
return confirm_form(
$form,
t('Are you sure you want to delete??'),
t('themetable_display')
);
}
function delete_record_confirm_submit($form, &$form_state) {
$record = $form_state['values']['delete'];
if ($record ) {
$num_deleted = db_delete('first_custome')
->condition('fid', $record )
->execute();
drupal_set_message('The record has been deleted!');
}
$form_state['redirect'] = "themetable_display";
} <?php
//creating hook_menu
function first_custom_menu() {
$items = array();
$items['first_custom'] = array(
'title' => 'First custom Form',
'description' => 'This is First Module form',
'page callback' => 'drupal_get_form',
'page arguments' => array('mymodule_form'),
'access callback' => TRUE
);
$items['second_custom'] = array(
'title' => 'Second custom Page',
'description' => 'This is First Module form',
'page callback' => 'display_table',
'access callback' => TRUE
);
$items['taxo_custom'] = array(
'title' => 'taxo custom Page',
'description' => 'This is First Module form',
'page callback' => 'drupal_get_form',
'page arguments' => array('cust_taxo_form'),
// 'page callback' => 'cust_taxo',
'access callback' => TRUE
);
$items['themetable_display'] = array(
'title' => 'Theme Table Display',
'description' => 'This is First Module form',
'page callback' => 'display_themetable',
'access callback' => TRUE
);
$items['update_tab'] = array(
'title' => 'Update custom form',
'description' => 'This is First Module form',
'page callback' => 'drupal_get_form',
'page arguments' => array('update_up'),
'access callback' => TRUE
);
$items['delete-tab'] = array(
'title' => 'Delete Record',
'description' => 'This is First Module form',
'page callback' => 'drupal_get_form',
'page arguments' => array('delete_record_confirm'),
'access callback' => TRUE
);
return $items;
}
/**
* Page callback mapped to the url /first_custom
*/
/**
* permission
*/
function first_custom_permission() {
return array(
'administer my module' => array(
'title' => t('Administer Custom Module'),
'description' => t('Administrate access of form to upload content and access of page to retrive content.'),
),
);
}
/**
* Collecting data through form
*/
function mymodule_form($form, &$form_state) {
if (arg(1) != '') {
$field_name=arg(1);
$select = db_select('first_custome', 'cf');
$select ->fields('cf',array('fid','name','mobile','description','date'))
-> where('fid = '. db_escape_field($field_name));
$results = $select->execute();
dpm($results);
$rows = array();
foreach ($results as $row) {
$rows[]=array( $row->fid,
$row->name,
$row->mobile,
$row->description,
$row->date,
);
}
}
else{
$row->fid="";
$row->name="";
$row->mobile="";
$row->description="";
$row->date="";
}
$form['name'] = array(
'#type' => 'textfield',
'#title' => 'Name',
'#required' => TRUE,
'#default_value'=>$row->name
);
$form['dob'] = array(
'#type' => 'date_popup',
'#title' => 'Date',
'#required' => TRUE,
'#default_value'=> $row->date,
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => 'Description',
'#maxlength' => 500,
'#required' => TRUE,
'#default_value'=>$row->description,
);
$form['mobilenumber'] = array(
'#type' => 'textfield',
'#title' => 'Mobile Number',
'#required' => TRUE,
'#default_value'=> $row->mobile,
);
if (arg(1) != ''){
$form['fid'] = array(
'#type' => 'textfield',
'#title' => 'fid',
'#required' => TRUE,
'#default_value'=> arg(1),
);
}
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Click Here!'),
);
return $form;
}
/**
*
* Form Validation
*
*/
function mymodule_form_validate($form, &$form_state) {
$mobile = $form_state['values']['mobilenumber'];
$name1 = $form_state['values']['name'];
$count_mob = strlen($mobile);
if ($count_mob != 10) {
form_set_error('mobilenumber', t('Mobile Number Should be 10 digits'));
}
if (!is_numeric($mobile)) {
form_set_error('mobilenumber', t('mobile number Should be numeric'));
}
if (is_numeric($name1)) {
form_set_error('name', t('Name should not be numeric'));
}
}
/**
*
* Craeting Nodes
*create node on form submit
*/
function mymodule_form_submit($form, &$form_state) {
global $user;
if(arg(1)==""){
//---------------------------------------------------------------
echo $mobile = $form_state['values']['mobilenumber'];
echo $name1 = $form_state['values']['name'];
echo $dob = $form_state['values']['dob'];
echo $description = $form_state['values']['description'];
//-----------------------------------------------------------------
$node = new stdClass();
$node->title = $name1;
$node->type = "first_custom";
node_object_prepare($node); // Sets some defaults. Invokes hook_prepare() and hook_node_prepare().
$node->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
$node->uid = $user->uid;
$node->status = 1; //(1 or 0): published or not
$node->promote = 0; //(1 or 0): promoted to front page
$node->comment = 0; // 0 = comments disabled, 1 = read only, 2 = read/write
$node->field_dob[LANGUAGE_NONE][0]['value'] = $dob;
$node->field_mobile3[LANGUAGE_NONE][0]['value'] = $mobile;
$node->field_description1[LANGUAGE_NONE][0]['value'] = $description;
$node = node_submit($node); // Prepare node for saving
node_save($node);
drupal_set_message("Node with nid " . $node->nid . " saved!\n");
//$form_state['redirect'] = 'SOME WHERE';
/* Create a new user on submit */
//
// $new_user = array(
// 'name' => $name1,
// 'mail' => 'test.bha@email.com',
// 'pass' => '123456',
// 'status' => 1,
// 'access' => REQUEST_TIME,
// 'roles' => array(), // No other roles than Authenticated
// //'roles' => array('10' => '10', '11' => '11'), // If you want to specify additional roles, the numbers are role_id's
// );
// user_save(NULL, $new_user);
//
//INserting data in data base insert
$nidoaoa = db_insert('first_custome')
->fields(array(
'description' =>$form_state['values']['description'],
'name' =>$form_state['values']['name'],
'mobile' =>$form_state['values']['mobilenumber'],
'date' =>$form_state['values']['dob'],
))
->execute();
drupal_set_message("Data Added!\n");
}
else{
//---------------------------------------------------------------
echo $mobileu = $form_state['values']['mobilenumber'];
echo $name1u = $form_state['values']['name'];
dsm($name1u);
echo $dobu = $form_state['values']['dob'];
echo $descriptionu = $form_state['values']['description'];
echo $fidu=$form_state['values']['fid'];
//-----------------------------------------------------------------
db_update('first_custome')
->fields(array(
'mobile' => $mobileu,
'name' => $name1u,
'description' => $descriptionu,
'date' => $dobu,
)
)
-> where('fid ='.$fidu)
->execute();
drupal_set_message("Data Updated!\n");
}
}
function display_table() {
$header = array('Name','Dob','Description','Mobile');
$rows = array();
$nodes = array('95', '100','95','101','106');
// print_r(node_load(92));
foreach ($nodes as $key) {
$row = node_load($key);
//dsm($row);
$rows[]= array(
$row->title,
$row->field_dob[LANGUAGE_NONE][0]['value'],
$row->field_description1[LANGUAGE_NONE][0]['value'],
$row->field_mobile3[LANGUAGE_NONE][0]['value'],
);
}
//dsm($rows);
$output = theme('table', array(
'header' => $header,
'rows' => $rows
)
);
return $output;
}
function display_themetable(){
# configure the table header columns
$header = array(
array('data' => 'id','field' => 'fid','sort' => 'ASC'),
array('data' => 'Name','field' => 'name'),
array('data' => 'mobile','field' => 'mobile'),
array('data' => 'description','field' => 'description'),
array('data' => 'date','field' => 'date'),
);
# set the database table and initial SelectQuery options
# $select is a SelectQuery object.
$select = db_select('first_custome', 'cf')
->extend('PagerDefault')
->extend('TableSort');
$select ->fields('cf',array('fid','name','mobile','description','date'))
->limit(5)
->orderByHeader($header);
$results = $select->execute();
$rows = array();
foreach ($results as $row) {
$rows[]=array(l($row->fid , 'first_custom/'.$row->fid),
$row->name,
$row->mobile,
$row->description,
$row->date,
l('Edit', 'first_custom/'.$row->fid),
l('Delete', 'delete-tab/'.$row->fid),
);
}
$output = theme('table', array('header' => $header,
'rows' => $rows ));
# add the pager
$output .= theme('pager');
# count number of records , count records
$results = db_select('first_custome')
->fields(NULL, array('fid'))
->countQuery()
->execute()
->fetchField();
dsm("Number of records=".$results);
return $output;
}
/* create taxonomy*/
function cust_taxo_form ($form, &$form_state) {
$form['name'] = array(
'#type' => 'textfield',
'#title' => 'Name',
'#required' => TRUE
);
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Click Here!')
);
return $form;
}
function cust_taxo_form_submit($form,&$form_state) {
$name=$form_state['values']['name'];
$vocabulary = taxonomy_vocabulary_machine_name_load('custom');
$term = new stdClass();
$vid = $vocabulary->vid;
$term->name = $name ;
$term->vid = $vid;
taxonomy_term_save($term);
drupal_set_message("taxonomy saved!\n");
}
/* delete records */
function delete_record_confirm($form, &$form_state){
$form['delete'] = array(
'#type' => 'value',
'#value' =>arg(1),
);
return confirm_form(
$form,
t('Are you sure you want to delete??'),
t('themetable_display')
);
}
function delete_record_confirm_submit($form, &$form_state) {
$record = $form_state['values']['delete'];
if ($record ) {
$num_deleted = db_delete('first_custome')
->condition('fid', $record )
->execute();
drupal_set_message('The record has been deleted!');
}
$form_state['redirect'] = "themetable_display";
}