By dinakaran.ilango on
hello
i tried to create a custom content type programmatically by following example module
drupal.org/project/examples
in my content type(account_km). i got following fields
fields:
title -textfield
The most recent known balnce. - integer
Type of account - select box //for the select box i followed http://www.andypangus.com/drupal-7-simple-content-type-select-widget
date -datefield
Every time when i go to the page the select box not expanding it only shows the content after clearing the cache everytime
here is my .install file
<?php
/**
* @file
* Install file for Node Example module.
*
* The definition of the fields for the module is here,
*
* @see http://drupal.org/node/707832
* @see http://drupal.org/node/443536
* @see field
*/
/**
* Implements hook_install().
*
* This hook is called when the user enables the module for the first time
* (or on subsequent enables after the module has been uninstalled). So it's
* a good place to define our new node type.
*/
function account_km_install(){
// During installation, the t() function is unavailable, so we use get_t()
// to store the name of the translation function.
$t = get_t();
// We define the node type as an associative array.
$account_km = array(
'type' => 'account_km',
'name' => $t('Account'),
// 'base' tells Drupal the base string for hook functions.
// This is often the module name; if base is set to 'mymodule', Drupal
// would call mymodule_insert() or similar for node hooks.
// In this case, we set base equal to 'node_content' so Drupal will handle
// our node as if we had designed it in the UI.
'base' => 'node_content',
'description' => $t('This is an account node type.'),
'title_label' => $t('Title'),
'custom' => TRUE,
);
// Complete the node type definition by setting any defaults not explicitly
// declared above.
// http://api.drupal.org/api/function/node_type_set_defaults/7
$content_type = node_type_set_defaults($account_km);
// Save the content type
node_type_save($content_type);
// Create all the fields we are adding to our content type.
// http://api.drupal.org/api/function/field_create_field/7
foreach (_account_km_installed_fields() as $field) {
field_create_field($field);
}
// Create all the instances for our fields.
// http://api.drupal.org/api/function/field_create_instance/7
foreach (_account_km_installed_instances() as $instance) {
$instance['entity_type'] = 'node';
$instance['bundle'] = $account_km['type'];
field_create_instance($instance);
}
}
/**
* Implements hook_uninstall().
*
* This hook is called when the user not only has disabled the module,
* but also uninstalls it from the 'uninstall' tab in the module page.
*
* So it's a perfect time to remove our fields and instances and new
* node type from the database.
*
* @ingroup node_example
*/
function account_km_uninstall() {
// Gather all the example content that might have been created while this
// module was enabled. Simple selects still use db_query().
// http://api.drupal.org/api/function/db_query/7
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => 'account_km'));
$nids = array();
foreach ($result as $row) {
$nids[] = $row->nid;
}
// Delete all the nodes at once
// http://api.drupal.org/api/function/node_delete_multiple/7
node_delete_multiple($nids);
// Loop over each of the fields defined by this module and delete
// all instances of the field, their data, and the field itself.
// http://api.drupal.org/api/function/field_delete_field/7
foreach (array_keys(_account_km_installed_fields()) as $field) {
field_delete_field($field);
}
// Loop over any remaining field instances attached to the node_example
// content type (such as the body field) and delete them individually.
// http://api.drupal.org/api/function/field_delete_field/7
$instances = field_info_instances('node', 'account_km');
foreach ($instances as $instance_name => $instance) {
field_delete_instance($instance);
}
// Delete our content type
// http://api.drupal.org/api/function/node_type_delete/7
node_type_delete('account_km');
// Purge all field infromation
// http://api.drupal.org/api/function/field_purge_batch/7
field_purge_batch(1000);
}
/**
* Returns a structured array defining the fields created by this content type.
*
* This is factored into this function so it can be used in both
* node_example_install() and node_example_uninstall().
*
* @return
* An associative array specifying the fields we wish to add to our
* new node type.
*
* @ingroup node_example
*/
function _account_km_installed_fields() {
$t = get_t();
return array(
'account_km_balance' => array(
'field_name' => 'account_km_balance',
'cardinality' => 1,
'type' => 'number_integer',
'settings' => array(
'max_length' => 60,
),
),
'account_km_dob' => array(
'field_name' => 'account_km_dob',
'type' => 'date',
'cardinality' => 1, // default
'translatable' => FALSE, // default
'entity_types' => array(), // default
'active' => TRUE, // default
'locked' => FALSE, // default
'deleted' => FALSE, // default
'settings' => array(
'repeat' => FALSE, // TODO
'todate' => '', // default, other values are 'optional' and 'required'
'granularity' => array(
'year' => 'year',
'month' => 'month',
'day' => 'day',
),
'tz_handling' => 'none',
'timezone_db' => '', // TODO
),
),
'account_km_type' => array(
'field_name' => 'account_km_type',
'type' => 'list_text',
'cardinality' => '1', // change this to -1 to enable ctrl+select
'foreign keys' => array(),
'indexes' => array(
'value' => array(
0 => 'value',
),
),
'module' => 'list',
'settings' => array(
'allowed_values_function' => '_account_km_type_list',
),
),
);
}
/**
* Returns a structured array defining the instances for this content type.
*
* The instance lets Drupal know which widget to use to allow the user to enter
* data and how to react in different view modes. We are going to display a
* page that uses a custom "node_example_list" view mode. We will set a
* cardinality of three allowing our content type to give the user three color
* fields.
*
* This is factored into this function so it can be used in both
* node_example_install() and node_example_uninstall().
*
* @return
* An associative array specifying the instances we wish to add to our new
* node type.
*
* @ingroup node_example
*/
function _account_km_installed_instances() {
$t = get_t();
return array(
'account_km_balance' => array(
'field_name' => 'account_km_balance',
'label' => $t('The most recent known balnce.'),
'widget' => array(
'type' => 'number',
),
'display' => array(
'account_km_list' => array(
'label' => 'hidden',
'type' => 'account_km_balance',
),
),
),
'account_km_dob' => array(
'field_name' => 'account_km_dob',
'entity_type' => 'node',
'bundle' => 'example',
'required' => TRUE,
'label' => t('Date'),
'description' => '', // default
'deleted' => FALSE, // default
'widget' => array(
'weight' => 1,
'type' => 'date_select',
'active' => TRUE, // default
'settings' => array(
'input_format' => 'm/d/Y - H:i:s',
'input_format_custom' => 'F j, Y - H:i:s', // January 1, 2000 - 00:00:00, default is ''
'year_range' => '-3:+3', // reversed year range '+3:-3' is not supported
'increment' => 1, // default, if it set to 2, then the minutes will increment by 2: 0, 2, 4, 6
'label_position' => 'within',
'text_parts' => array(), // If you would like to use both text field and select options
'repeat_collapsed' => FALSE, // default
),
),
'settings' => array(
'default_value' => 'now',
'default_value_code' => '', // default
'default_value2' => 'blank', // default
'default_value_code2' => '', // default
'default_format' => 'standard', // Custom date type
),
'display' => array(
'default' => array(
'label' => 'above',
'type' => 'date_standard',
'weight' => 1,
'settings' => array(
'format_type' => 'standard',
'fromto' => 'both',
'multiple_number' => '',
'multiple_from' => '',
'multiple_to' => '',
'show_repeat_rule' => 'show',
),
),
),
),
'account_km_type' => array(
'field_name' => 'account_km_type',
'label' => t('Type of account'),
'description' => t('Select an option from the list.'),
'default_value' => NULL, // add a default value here that matches your key => index values
'display' => array(
'default' => array(
'label' => 'above',
'module' => 'list',
'settings' => array(),
'type' => 'list_default',
'weight' => -1,
),
'teaser' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => -1,
),
),
'required' => 1,
'settings' => array(
'user_register_form' => FALSE,
),
'widget' => array(
'active' => 1,
'module' => 'options',
'settings' => array(),
'type' => 'options_select',
'weight' => '-1',
),
),
);
}
/**
* Options callback for account_km_checkboxes field
* @return - returns an indexed array as integer => string
*/
function _account_km_type_list() {
$options = array(
1 => 'Checking',
2 => 'Savings',
3 => 'Credit Card',
4 => 'Line of Credit or Other Credit',
5 => 'PayPal',
6 => 'Merchant Account',
7 => 'Investment Account',
8 => 'Mortgage',
9 => 'Other Asses(House,Car,etc)',
10 => 'Other Loan or Liablitity',
);
return $options;
}
here is my .module file
<?php
/**
*implementing hook_menu
*/
function account_km_menu()
{
$items['km/account'] = array(
'page callback' => 'account_km_add_page',
'access arguments' => array('access content'),
'title' => 'Add Account',
);
return $items;
}
/**
*implmenting page callback
*/
function account_km_add_page(){
$renderable_array = array();
// We query the database and find all of the nodes for the type we defined.
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type AND n.status = :status';
$result = db_query($sql,
array(
':type' => 'account_km',
':status' => 1,
)
);
// Loop through each of our node_example nodes and instruct node_view
// to use our "example_node_list" view.
// http://api.drupal.org/api/function/node_load/7
// http://api.drupal.org/api/function/node_view/7
foreach ($result as $row) {
$node = node_load($row->nid);
$renderable_array['node_list'][]= node_view($node, 'account_km_list');
}
return $renderable_array;
}
/**
* Implements hook_entity_info_alter().
*
* We need to modify the default node entity info by adding a new view mode to
* be used in functions like node_view() or node_build_content().
*/
function account_km_entity_info_alter(&$entity_info) {
// Add our new view mode to the list of view modes...
$entity_info['node']['view modes']['account_km_lists'] = array(
'label' => t('Account List'),
'custom settings' => TRUE,
);
}
/**
*implementing hook_help
*/
function account_km_help($path, $arg) {
switch ($path) {
case 'km/account':
return "<p>" . t("creating Account content type using hooks you can create accounts using <a href='!accountadd'>account add form</a>.",
array('!accountadd' => url('node/add/account-km'))) . "</p>";
}
}
what can i do now
thanks in advance
dinathecool