I want to create a new content type " News " with a custom field Author which refers to the author-of-the-content(taxonomy reference or user reference).
This is my .module file:
<?php
/**
* Implements hook_node_info()
* Drupal calls this hook when it’s discovering which node types are available. You’ll
* provide some metadata about your custom node.
*/
function news_node_info() {
return array(
'news' => array(
'name' => t('News'),
'base' => 'news',
'description' => t('You can add News here'),
'has_title' => TRUE,
'title_label' => t('News title')
)
);
}
/**
* Implement hook_form()
*/
function news_form($node, $form_state) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('news Title'),
'#default_value' => !empty($node->title) ? $node->title : '',
'#required' => TRUE,
'#weight' => -5
);
return $form;
}
This is my .install file:
<?php
/**
* Implements hook_install().
*/
function news_install() {
node_types_rebuild();
$types = node_type_get_types();|
node_add_body_field($types['news']);
add_custom_fields();
}
function _news_installed_fields() {
$t = get_t();
return array(
'news_title' => array(
'field_name' => 'news_title',
'label' => $t('Title'),
'type' => 'text',
),
'news_image' => array(
'field_name' => 'news_image',
'label' => $t('Image'),
'type' => 'image',
),
'news_tags' => array(
'field_name' => 'news_tags',
'label' => $t('Add Tags'),
'type' => 'taxonomy_term_reference',
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => 'news_tags_vocab',
'parent' => 0,
),
),
),
),
),
);
}
function _news_installed_instances() {
$t = get_t();
return array(
'news_title' => array(
'field_name' => 'news_title',
'type' => 'text',
'label' => $t('The heading of the news topic'),
'widget' => array(
'type' => 'text_textfield',
),
'display' => array(
'news_display_option' => array(
'label' => $t('The heading of the news topic (display option)'),
'type' => 'text',
),
),
),
'news_image' => array(
'field_name' => 'news_image',
'type' => 'image',
'label' => $t('Add Image'),
),
'news_tags' => array(
'field_name' => 'news_tags',
'type' => 'taxonomy_term_reference',
'label' => $t('Add Tags'),
'widget' => array(
'type' => 'taxonomy_autocomplete'
),
'display' => array(
'default' => array('type' => 'hidden'),
'teaser' => array('type' => 'hidden')
),
),
)
);
}
function add_custom_fields() {
foreach (_news_installed_fields() as $field) {
field_create_field($field);
}
foreach (_news_installed_instances() as $fieldinstance) {
$fieldinstance['entity_type'] = 'node';
$fieldinstance['bundle'] = 'news';
print_r($fieldinstance);
field_create_instance($fieldinstance);
}
}
/**
* Implements hook_uninstall().
*/
function news_uninstall() {
// Delete the taxonomy with its terms
$vocab = taxonomy_vocabulary_machine_name_load('news_tags_vocab');
taxonomy_vocabulary_delete($vocab->vid);
// Gather all the example content that might have been created while this
// module was enabled.
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => 'news'));
$nids = array();
foreach ($result as $row) {
$nids[] = $row->nid;
}
// Delete all the nodes at once
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.
foreach (array_keys(_news_installed_fields()) as $field) {
field_delete_field($field);
}
// Loop over any remaining field instances attached to the news
// content type (such as the body field) and delete them individually.
$instances = field_info_instances('node', 'news');
foreach ($instances as $instance_name => $instance) {
field_delete_instance($instance);
}
// Delete our content type
node_type_delete('news');
// Purge all field infromation
field_purge_batch(1000);
}