function node_example_node_type_insert

Implements hook_node_type_insert().

Much like hook_node_insert() lets us know that a node is being inserted into the database, hook_node_type_insert() lets us know that a new content type has been inserted.

Since Drupal will at some point insert our new content type, this gives us a chance to add the fields we want.

It is called for all inserts to the content type database, so we have to make sure we're only modifying the type we're concerned with.

Related topics

File

node_example/node_example.module, line 112

Code

function node_example_node_type_insert($content_type) {
    if ($content_type->type == 'node_example') {
        // First we add the body field. Node API helpfully gives us
        // node_add_body_field().
        // We'll set the body label now, although we could also set
        // it along with our other instance properties later.
        $body_instance = node_add_body_field($content_type, t('Example Description'));
        // Add our example_node_list view mode to the body instance
        // display by instructing the body to display as a summary.
        $body_instance['display']['example_node_list'] = array(
            'label' => 'hidden',
            'type' => 'text_summary_or_trimmed',
        );
        // Save our changes to the body field instance.
        field_update_instance($body_instance);
        // Create all the fields we are adding to our content type.
        foreach (_node_example_installed_fields() as $field) {
            field_create_field($field);
        }
        // Create all the instances for our fields.
        foreach (_node_example_installed_instances() as $instance) {
            $instance['entity_type'] = 'node';
            $instance['bundle'] = 'node_example';
            field_create_instance($instance);
        }
    }
}