I'm trying to make a simple custom field "field_book_year" for the new node type "synopsis_book". I have wrote in .install file:
function synopsis_install() {
node_types_rebuild();
$types = node_type_get_types();
// In Drupal 7 you must explicitly add a "body" field when you create a custom content type,
// thus the call to node_add_body_field($types['newsletter']);.
node_add_body_field($types['synopsis_book']);
$body_instance = field_info_instance('node', 'body', 'synopsis_book');
$body_instance['type'] = 'text';
field_update_instance($body_instance);
field_create_field(array(
'field_name' => 'field_book_year',
'label' => t('Company posting the job listing'),
'type' => 'text',
'translatable' => TRUE,
)
);
field_create_instance(array(
'field_name' => 'field_book_year',
'label' => t('Company posting the job listing'),
'entity_type' => 'node',
'bundle' => 'synopsis_book',
'type' => 'text',
'widget' => array(
'type' => 'text_textfield',
),
'display' => array(
'example_node_list' => array(
'label' => t('Company posting the job listing'),
'type' => 'text',
),
),
'description' => 'Begin Date',
)
);
}
Then, I have in .module these functions:
function synopsis_node_info()
{$types = node_type_get_types();print_r($types);
return array(
'synopsis_author' => array('name' => t('Author'), 'base' => 'synopsis_author', 'description' => t('Author description, biography etc.'),
'has_body' => true, 'has_title' => true, /*'min_word_count' => 11,*/ 'help' => t('Enter blah-blah-blah'),
'title_label' => t('Author full name')
),
'synopsis_book' => array('name' => t('Book'), 'base' => 'synopsis_book', 'description' => t('Book description, synopsis etc.'),
'has_body' => true, 'has_title' => true, /*'min_word_count' => 11,*/ 'help' => t('Enter blah-blah-blah'),
'title_label' => t('Book title')
),
);
}
/**
* Implement hook_form() with the standard default form.
*/
function synopsis_book_form($node, $form_state) {
return node_content_form($node, $form_state);
}
/**
* Implements hook_validate().
*/
function synopsis_book_validate($node)
{
// Enforce a minimum character count of 2 on company names.
if (isset($node->field_book_year) &&
strlen($node->field_book_year['und'][0]['value']) < 2
) {
form_set_error('job_post_company',
t('The company name is too short. It must be atleast 2 characters.'),
$limit_validation_errors = NULL);
}
}
Not matter all I've written, the page of adding the Book looks like this! No 'field_book_year' field. There is even no body field
What am I doing wrong? Thank you.
I think the problem you're having is that
hook_install
is run when your module is installed, before the node type is created. So essentially you're trying to attach a field to a non-existant node type at that point.The way I've always done this is to create the node type in
hook_install
before trying to attach the field (this is an example from a module that provides atestimonial
content type):If you look in the standard installation profile install file you'll see this is actually how the Drupal core performs this task, so I would expect it's the 'correct' way to do it.