Hi,
Many times, we required to create custom node and the same we need to display as form in front end. In this case, we can create custom module Quick and Easily as below.
For eg. We have "contactinfo" node type and it's having fields like..
- Name
- Phone Number
- Message
First we need to create : contactinfo.info file
name = contactinfo module
description = contactinfo test module
package = contactinfo modules
core = 7.x
; Information added by Drupal.org packaging script on 2014-06-07
version = "7.x-1.x-dev"
core = "7.x"
project = "sandip"
datestamp = "1402151651"
Second We need to create : contactinfo.module file
/**
* @file
* An example of calling defualt Node and Block via Module
*/
/**
* Implements hook_field_info().
*
* Provides the description of the field.
*/
function contactinfo_info() {
return array(
// We name our field as the associative name of the array.
'contactinfo_basic_action' => array(
'label' => t('Simple contact form creation'),
),
);
}
function contactinfo_menu(){
// Will be only used while we are dealing with ready made node form
$items['contactinfo/test'] = array(
'title' => t('Contact Info Form'),
'page callback' => 'node_form_contactinfo',
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
'file path' => drupal_get_path('module', 'node'),
'file' => 'node.pages.inc',
);
return $items;
}
/*
* Creating Sample Block
*/
function contactinfo_block_info(){
$blocks['contactinfo_block'] = array(
'info' => t('Create contactinfo block'),
'status' => TRUE,
'region' => 'sidebar_first',
'visibility' => BLOCK_VISIBILITY_NOTLISTED,
);
return $blocks;
}
/*
* Creating Sample Block view for contact info form
*/
function contactinfo_block_view($delta = '') {
// echo $delta;
switch ($delta) {
case 'contactinfo_block':
module_load_include('inc', 'node', 'node.pages');
$block['subject'] = t('Quick Contact');
$block['content'] = node_form_contactinfo();
break;
}
return $block;
}
/*
* Function will display node on page as well as block
*/
function node_form_contactinfo(){
//$nval = node_load(5);
global $user;
//echo $user->uid;
$type = 'contactinfo';
$form_id = $type . '_node_form';
$node = new stdClass();
$node->uid = $user->uid;
$node->type = $type;
node_object_prepare($node);
$output = drupal_get_form($form_id, $node);
return render($output);
}
/*
* Default node form alter hook
* You can add form CSS/Label as per your need.
*/
function contactinfo_form_contactinfo_node_form_alter(&$form, &$form_state, $form_id) {
/* $form['field_phone'] = array(
'#type' => 'textfield',
'#title' => t('Phone Number/Mobile'),
'#required' => false,
);
*/
}
?>
========= SAME CODE USING FORM API ============
/**
* @file
* An example of calling default Node and Block via Module
*/
/**
* Implements hook_field_info().
*
* Provides the description of the field.
*/
function contactinfocustom_info() {
return array(
// We name our field as the associative name of the array.
'contactinfocustom_basic_action' => array(
'label' => t('Simple contact form creation'),
),
);
}
function contactinfocustom_menu(){
$items = array();
// Will be only used while we are dealing with ready made node form
$items['contactinfocustom/test'] = array(
'title' => t('Contact Info Custom Form '),
'page callback' => 'drupal_get_form',
'page arguments' => array('contactinfocustom_getform'),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM
);
return $items;
}
/*
* custom form creation
*/
function contactinfocustom_getform($form, &$form_state){
$form = array();
$form['title'] = array(
"#title" => t('Name'),
"#type" => t('textfield'),
'#size' => 20,
'#maxlength' => 20,
"#required" => TRUE,
);
$form['field_phone'] = array(
'#type' => 'textfield',
'#title' => t('Phone Number/Mobile'),
'#size' => 20,
'#maxlength' => 10,
'#required' => false,
);
$form['body'] = array(
'#type' => 'textarea',
'#title' => t('Information About You'),
'#required' => false,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
return $form;
}
function contactinfocustom_getform_validate($form, &$form_state){
$values = $form_state['values'];
if($values['title'] == ''){
form_set_error('title', t('Set Any Error MSG'));
}
}
function contactinfocustom_getform_submit($form, &$form_state){
global $user;
$values = $form_state['values'];
$node = new stdClass();
$node->type = "contactinfo";
node_object_prepare($node);
$node->title = $values['title'];
$node->uid = $user->uid;
$node->status = 1;
$node->promote = 0;
$node->language = LANGUAGE_NONE;
$node->field_phone[$node->language][] = array('value' =>$values['field_phone'] ,);
$node->body[$node->language][] = array('value' =>$values['body'] ,);
if($node = node_submit($node)){
node_save($node);
drupal_set_message(t('Contact detail form data has been saved successfully.'));
/* $params['puser']['subject'] = t('New Contact entry Found!!!');
$params['puser']['message'] = t('Please check the admin panel for contact entry!!!');
$params['padmin']['subject'] = t('Thank you for submission!!!');
$params['padmin']['message'] = t('Thanks for contacting us');
$site_admin = variable_get('site_mail', '');
drupal_mail('personal_frm', 'puser', $values['email'], 'EN', $params);
drupal_mail('personal_frm', 'auser', $site_admin, 'EN', $params);
*/
}
}
?>
Many times, we required to create custom node and the same we need to display as form in front end. In this case, we can create custom module Quick and Easily as below.
For eg. We have "contactinfo" node type and it's having fields like..
- Name
- Phone Number
- Message
First we need to create : contactinfo.info file
name = contactinfo module
description = contactinfo test module
package = contactinfo modules
core = 7.x
; Information added by Drupal.org packaging script on 2014-06-07
version = "7.x-1.x-dev"
core = "7.x"
project = "sandip"
datestamp = "1402151651"
Second We need to create : contactinfo.module file
/**
* @file
* An example of calling defualt Node and Block via Module
*/
/**
* Implements hook_field_info().
*
* Provides the description of the field.
*/
function contactinfo_info() {
return array(
// We name our field as the associative name of the array.
'contactinfo_basic_action' => array(
'label' => t('Simple contact form creation'),
),
);
}
function contactinfo_menu(){
// Will be only used while we are dealing with ready made node form
$items['contactinfo/test'] = array(
'title' => t('Contact Info Form'),
'page callback' => 'node_form_contactinfo',
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
'file path' => drupal_get_path('module', 'node'),
'file' => 'node.pages.inc',
);
return $items;
}
/*
* Creating Sample Block
*/
function contactinfo_block_info(){
$blocks['contactinfo_block'] = array(
'info' => t('Create contactinfo block'),
'status' => TRUE,
'region' => 'sidebar_first',
'visibility' => BLOCK_VISIBILITY_NOTLISTED,
);
return $blocks;
}
/*
* Creating Sample Block view for contact info form
*/
function contactinfo_block_view($delta = '') {
// echo $delta;
switch ($delta) {
case 'contactinfo_block':
module_load_include('inc', 'node', 'node.pages');
$block['subject'] = t('Quick Contact');
$block['content'] = node_form_contactinfo();
break;
}
return $block;
}
/*
* Function will display node on page as well as block
*/
function node_form_contactinfo(){
//$nval = node_load(5);
global $user;
//echo $user->uid;
$type = 'contactinfo';
$form_id = $type . '_node_form';
$node = new stdClass();
$node->uid = $user->uid;
$node->type = $type;
node_object_prepare($node);
$output = drupal_get_form($form_id, $node);
return render($output);
}
/*
* Default node form alter hook
* You can add form CSS/Label as per your need.
*/
function contactinfo_form_contactinfo_node_form_alter(&$form, &$form_state, $form_id) {
/* $form['field_phone'] = array(
'#type' => 'textfield',
'#title' => t('Phone Number/Mobile'),
'#required' => false,
);
*/
}
?>
========= SAME CODE USING FORM API ============
/**
* @file
* An example of calling default Node and Block via Module
*/
/**
* Implements hook_field_info().
*
* Provides the description of the field.
*/
function contactinfocustom_info() {
return array(
// We name our field as the associative name of the array.
'contactinfocustom_basic_action' => array(
'label' => t('Simple contact form creation'),
),
);
}
function contactinfocustom_menu(){
$items = array();
// Will be only used while we are dealing with ready made node form
$items['contactinfocustom/test'] = array(
'title' => t('Contact Info Custom Form '),
'page callback' => 'drupal_get_form',
'page arguments' => array('contactinfocustom_getform'),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM
);
return $items;
}
/*
* custom form creation
*/
function contactinfocustom_getform($form, &$form_state){
$form = array();
$form['title'] = array(
"#title" => t('Name'),
"#type" => t('textfield'),
'#size' => 20,
'#maxlength' => 20,
"#required" => TRUE,
);
$form['field_phone'] = array(
'#type' => 'textfield',
'#title' => t('Phone Number/Mobile'),
'#size' => 20,
'#maxlength' => 10,
'#required' => false,
);
$form['body'] = array(
'#type' => 'textarea',
'#title' => t('Information About You'),
'#required' => false,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
return $form;
}
function contactinfocustom_getform_validate($form, &$form_state){
$values = $form_state['values'];
if($values['title'] == ''){
form_set_error('title', t('Set Any Error MSG'));
}
}
function contactinfocustom_getform_submit($form, &$form_state){
global $user;
$values = $form_state['values'];
$node = new stdClass();
$node->type = "contactinfo";
node_object_prepare($node);
$node->title = $values['title'];
$node->uid = $user->uid;
$node->status = 1;
$node->promote = 0;
$node->language = LANGUAGE_NONE;
$node->field_phone[$node->language][] = array('value' =>$values['field_phone'] ,);
$node->body[$node->language][] = array('value' =>$values['body'] ,);
if($node = node_submit($node)){
node_save($node);
drupal_set_message(t('Contact detail form data has been saved successfully.'));
/* $params['puser']['subject'] = t('New Contact entry Found!!!');
$params['puser']['message'] = t('Please check the admin panel for contact entry!!!');
$params['padmin']['subject'] = t('Thank you for submission!!!');
$params['padmin']['message'] = t('Thanks for contacting us');
$site_admin = variable_get('site_mail', '');
drupal_mail('personal_frm', 'puser', $values['email'], 'EN', $params);
drupal_mail('personal_frm', 'auser', $site_admin, 'EN', $params);
*/
}
}
?>