1 node_type_example.module node_type_example_node_type_insert($content_type)

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 Backdrop 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

modules/examples/node_type_example/node_type_example.module, line 71
Hook implementations for the Node Type Example module.

Code

function node_type_example_node_type_insert($content_type) {
  if ($content_type->type == 'node_type_example') {
    // First, we add the body field with node_add_body_field(). We 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 node_type_example_list view mode to the body instance display by
    // instructing the body to display as a summary.
    $body_instance['display']['node_type_example_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_type_example_installed_fields() as $field) {
      field_create_field($field);
    }

    // Create all the instances for our fields.
    foreach (_node_type_example_installed_instances() as $instance) {
      $instance['entity_type'] = 'node';
      $instance['bundle'] = 'node_type_example';
      field_create_instance($instance);
    }
  }
}