7 node.api.php hook_node_info()
4.7 node.php hook_node_info()
5 node.php hook_node_info()
6 node.php hook_node_info()

Define module-provided node types.

This hook allows a module to define one or more of its own node types. For example, the blog module uses it to define a blog node-type named "Blog entry." The name and attributes of each desired node type are specified in an array returned by the hook.

Only module-provided node types should be defined through this hook. User- provided (or 'custom') node types should be defined only in the 'node_type' database table, and should be maintained by using the node_type_save() and node_type_delete() functions.

Return value

An array of information defining the module's node types. The array contains a sub-array for each node type, with the machine-readable type name as the key. Each sub-array has up to 10 attributes. Possible attributes:

  • name: (required) The human-readable name of the node type.
  • base: (required) The base name for implementations of node-type-specific hooks that respond to this node type. Base is usually the name of the module or 'node_content', but not always. See Node API hooks for more information.
  • description: (required) A brief description of the node type.
  • help: (optional) Help information shown to the user when creating a node of this type.
  • has_title: (optional) A Boolean indicating whether or not this node type has a title field.
  • title_label: (optional) The label for the title field of this content type.
  • locked: (optional) A Boolean indicating whether the administrator can change the machine name of this type. FALSE = changeable (not locked), TRUE = unchangeable (locked).

The machine name of a node type should contain only letters, numbers, and underscores. Underscores will be converted into hyphens for the purpose of constructing URLs.

All attributes of a node type that are defined through this hook (except for 'locked') can be edited by a site administrator. This includes the machine-readable name of a node type, if 'locked' is set to FALSE.

Related topics

3 functions implement hook_node_info()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

blog_node_info in modules/blog/blog.module
Implements hook_node_info().
forum_node_info in modules/forum/forum.module
Implements hook_node_info().
poll_node_info in modules/poll/poll.module
Implements hook_node_info().
3 invocations of hook_node_info()
aggregator_form_aggregator_admin_form_alter in modules/aggregator/aggregator.processor.inc
Implements hook_form_aggregator_admin_form_alter().
node_type_reset in modules/node/content_types.inc
Resets relevant fields of a module-defined node type to their default values.
_node_types_build in modules/node/node.module
Builds and returns the list of available node types.

File

modules/node/node.api.php, line 916
Hooks provided by the Node module.

Code

function hook_node_info() {
  return array(
    'blog' => array(
      'name' => t('Blog entry'),
      'base' => 'blog',
      'description' => t('Use for multi-user blogs. Every user gets a personal blog.'),
    )
  );
}

Comments

You also need to implement hook_form() if you want your content type to appear in Content Type admin page on admin/structure/types.

Thanks!.
Furthermore, if your content type has title field enabled, it would not be available in node edit form unless you implement hook_form() like this:

<?php
function mymodule_form($node, $form_state) {
  return
node_content_form($node, $form_state);
}
?>

See node_content_form().

For see your custom content type in admin/structure/types you only must set key 'base' => 'node_content'.

Example:

<?php
 
return array(
   
'event' => array(
     
'name' => t('Event'),
     
'base' => 'node_content',
     
'description' => t('Content type for manage events'),
    )
  );
?>

What if I want to define settings like publishing and options ?

@a77icu5, there is some info about that here: https://www.drupal.org/node/1169864 — I'm not sure where the canonical documentation is, though. I guess it's spread around the various modules that provide node-type options.

According to the above comments the content type would not appear unless you implement hook_form(). But hook_form() needs to be implemented using the content type's base key value NOT THE MODULE NAME. Ex: Assume that my module name is my_module and i implement hook_node_info() in my_module.module file like below

/**
* Implements hook_node_info().
* @return array
*/
function my_module_node_info() {
  $types = array(
    'custom_ct' => array(
      'name' => t('My custom content type'),
      'base' => 'custom_ct',
      'description' => t('A Description of my custom content type'),
    )
  );

  return $types;
}

/**
* Implements hook_form
* @param $node
* @param $form_state
*/

function custom_ct_form($node, $form_state) {
/**
* TODO
*/
}