Saturday, March 26, 2011

Create cck using custom module

1. Create an table with for fields other than title & body.Here i have created an table employee with three fields vid,nid,age.

2.Create an custom module with module folder name employee.

3.Copy the above code & paste in your module file.

4.Go through the code once.

<?php
/**
* Implementation of hook_node_info().
*/
function employee_node_info() {
return array(
'employee' => array(
'name' => t('Employee'),
'module' => 'employee',
'description' => "Create an Employee",
)
);
}

/**
* Implementation of hook_perm().
*/
function employee_perm() {
return array('create employee', 'edit own employee');
}

/**
* Implementation of hook_access().
*/
function employee_access($op, $node) {
global $user;

if ($op == 'create') {
// Only users with permission to do so may create this node type.
return user_access('create employee');
}

// Users who create a node may edit or delete it later, assuming they have the
// necessary permissions.
if ($op == 'update' || $op == 'delete') {
if (user_access('edit own employee') && ($user->uid == $node->uid)) {
return TRUE;
}
}
}

/**
* Implementation of hook_form().
*/
function employee_form(&$node, $form_state) {
$type = node_get_types('type', $node);

// We need to define form elements for the node's title and body.
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => -5
);

$form['age'] = array(
'#type' => 'textfield',
'#title' => 'Age',
'#required' => TRUE,
'#default_value' => $node->age,
'#weight' => -4
);
// We want the body and filter elements to be adjacent. We could try doing
// this by setting their weights, but another module might add elements to the
// form with the same weights and end up between ours. By putting them into a
// sub-array together, we're able force them to be rendered together.
$form['body_filter']['body'] = array(
'#type' => 'textarea',
'#title' => check_plain($type->body_label),
'#default_value' => $node->body,
'#required' => FALSE
);
$form['body_filter']['filter'] = filter_form($node->format);

// NOTE in node_example there is some addition code here not needed for this simple node-type

return $form;
}

/**
*Load the values of custom fields in CCK
*/
function employee_load($node) {
$additions = db_fetch_object(db_query('SELECT age FROM {employee} WHERE vid = %d', $node->vid));
return $additions;
}

/**
*Insert the values of custom field of CCK into an custom table created
*/
function employee_insert($node) {
db_query("INSERT INTO {employee} (vid, nid, age) VALUES (%d, %d, '%d')", $node->vid, $node->nid, $node->age);
}

/**
*Delete the node from the custom table
*/
function employee_delete($node) {
// Notice that we're matching all revision, by using the node's nid.
db_query('DELETE FROM {employee} WHERE nid = %d', $node->nid);
}


/**
*Update the node into the custom table
*/
function employee_update($node) {
// if this is a new node or we're adding a new revision,
if ($node->revision) {
employee_insert($node);
}
else {
db_query("UPDATE {employee} SET age = %d WHERE vid = %d", $node->age, $node->vid);
}
}


/**
* Implementation of hook_help().
*/
function employee_help($path, $arg) {
switch ($path) {
case 'admin/help/employee':
return '

' . t('Help Custom CCK') . '

';
break;
}
}

?>

2 comments:

  1. Hi,

    I am new to Drupal.

    I have added extra fields to user registration form using UI in drupal 7. But drupal creates a separate table for each of the fields and I dont want that. Instead I want to save all the field data into a single table which I have created.

    Could you help me on this.

    ReplyDelete
  2. Hi Mani,

    Please follow my blog
    I will surely post a solution to your doubt soon

    thks

    ReplyDelete