Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6800001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:54:58+00:00 2026-05-26T18:54:58+00:00

I’m trying to make a simple custom field field_book_year for the new node type

  • 0

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 fieldthe page of adding the Book looks like this

What am I doing wrong? Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:54:59+00:00Added an answer on May 26, 2026 at 6:54 pm

    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 a testimonial content type):

    // Make sure a testimonial content type doesn't already exist
    if (!in_array('testimonial', node_type_get_names())) {
      $type = array(
        'type' => 'testimonial',
        'name' => st('Testimonial'),
        'base' => 'node_content',
        'description' => st("Use <em>basic pages</em> for your static content, such as an 'About us' page."),
        'custom' => 1,
        'modified' => 1,
        'locked' => 0,
        'title_label' => 'Customer / Client Name'
      );
    
      $type = node_type_set_defaults($type);
      node_type_save($type);
      node_add_body_field($type);
    }
    
    // See if the testimonial date field exists
    if (!field_info_field('field_testimonial_date')) {
      field_create_field(array(
        // Field info here
      ));
    }
    
    // If the date field is not attached, attach it
    if (!field_info_instance('node', 'field_testimonial_date', 'testimonial')) {
      field_create_instance(array(
        // Field instance info here
      ));
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I want use html5's new tag to play a wav file (currently only supported
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.