Feeds:
Posts
Comments

Archive for the ‘Drupal’ Category

Content Construction Kit (CCK) is one of the best module of Drupal. CCK module helps users to add custom fields to their nodes.  You can create a cck field to any content type easily, but i am going to show you how to add a custom field from a module. The code below shows how to add a text field to a content type from a module. Hope this help you and please post your valuable feedback.

<?php

$text_fields=array(‘field1′,’field2′,’field3’);
hook_install_cck_text_fields($text_fields, ‘CONTENT_TYPE_NAME’, $weight=20);

function hook_install_cck_text_fields($text_fields = array(), $type, $weight){
if (module_exists(‘text’)) {
foreach($text_fields as $text_fields => $text_field){
$field = content_fields(‘field_’ . $text_field);
  if (empty($field)) {
$field = array();
$field[‘field_name’] = ‘field_’ . $text_field;
$field[‘type_name’] = $type;
$field[‘type’] = ‘text’;
$field[‘module’] = ‘text’;
$field[‘required’] = FALSE;
$field[‘widget_type’] = ‘text_textfield’;
$field[‘active’] = TRUE;
content_field_instance_create($field);

$field = content_fields(‘field_’ . $text_field, $type);
$field[‘widget’][‘label’] = str_replace(“_”,” “,t(ucfirst($text_field)));
$field[‘widget’][‘weight’] = $weight++;
$field[‘widget’][‘type’] = ‘text_textfield’;
$field[‘display_settings’][‘weight’] = $weight;
$field[‘display_settings’][‘label’][‘format’] = ‘inline’;
$field[‘display_settings’][‘teaser’][‘format’] = ‘default’;
$field[‘display_settings’][‘full’][‘format’] = ‘default’;
content_field_instance_update($field);
}
else {
$field = content_fields(‘field_’ . $text_field, $type);
if (empty($field)) {
content_field_instance_create(array(‘field_name’ => ‘field_’ . $text_field, ‘type_name’ => $type));
}
}
}
}
}
?>

Read Full Post »