| 4.7 node.module | node_get_types() |
| 5 node.module | node_get_types($op = 'types', $node = NULL, $reset = FALSE) |
| 6 node.module | node_get_types($op = 'types', $node = NULL, $reset = FALSE) |
Builds a list of available node types, and returns all or part of this list.
Parameters
$op: The format in which to return the list: 'type', 'types', 'module', 'name', or 'names'. See return value section below for details.
$node: A node object, an array representation of a node object, or a node type name string. See return value section below for details.
$reset: Whether or not to reset this function's internal cache (defaults to FALSE).
Return value
If $node is supplied and it doesn't correspond to a known node type, or if $op is 'type', 'name', or 'module' and $node is not given, the function returns FALSE. Otherwise, the return value depends on the value of $op:
- 'types': An array of all available node type objects, keyed by machine name.
- 'type': The single node type object indicated by $node.
- 'names': An array of the display names of all available node types, keyed by machine name and sorted by display name.
- 'name': The single node type display name indicated by $node.
- 'module': The name of the node type module indicated by $node.
- blogapi_admin_settings in modules/
blogapi/ blogapi.module - blog_form in modules/
blog/ blog.module - Implementation of hook_form().
- book_admin_settings in modules/
book/ book.admin.inc - Builds and returns the book settings form.
- comment_update_6002 in modules/
comment/ comment.install - Changed comment settings from global to per-node -- copy global settings to all node types.
- forum_form in modules/
forum/ forum.module - Implementation of hook_form().
File
- modules/
node/ node.module, line 425 - The core that allows content to be submitted to the site. Modules and scripts may programmatically submit nodes using the usual form API pattern.
Code
function node_get_types($op = 'types', $node = NULL, $reset = FALSE) {
static $_node_types, $_node_names;
if ($reset || !isset($_node_types)) {
list($_node_types, $_node_names) = _node_types_build();
}
if ($node) {
if (is_array($node)) {
$type = $node['type'];
}
elseif (is_object($node)) {
$type = $node->type;
}
elseif (is_string($node)) {
$type = $node;
}
if (!isset($_node_types[$type])) {
return FALSE;
}
}
switch ($op) {
case 'types':
return $_node_types;
case 'type':
return isset($_node_types[$type]) ? $_node_types[$type] : FALSE;
case 'module':
return isset($_node_types[$type]->module) ? $_node_types[$type]->module : FALSE;
case 'names':
return $_node_names;
case 'name':
return isset($_node_names[$type]) ? $_node_names[$type] : FALSE;
}
}
Comments
Just a quick reference to
PermalinkJust a quick reference to what the different options output.
<?php// call
print_r(node_get_types() );
print_r(node_get_types('types') );
// output
array(
'node_type' => array(
'name' => 'Human Readable name',
'module' => 'node',
...
)
);
// call
print_r(node_get_types('names'));
// output
array(
'node_type' => 'Human readable name', // name not sanitized!
);
// call
print_r(node_get_types('name', 'node_type') );
// output
"Human readable name"
// call
print_r(node_get_types('module', 'node_type') );
// output
node (or whatever module is).
?>
Finally, to get an simple array of node types
<?phparray_keys(node_get_types());
array_keys(node_get_types('types'));
array_keys(node_get_types('names'));
?>
To get a /sanitized/ array of node types names suitable for fapi checkboxes/select
<?phparray_map('check_plain', node_get_types('names'));
?>
Close but no cigar
PermalinkHefox's example output indicates that an array of arrays is returned by this function, but it actually returns an array of objects.
// Outputarray(
'story' => stdClass(
name
module
...
)
);
Suppose you have a $node object and you want to get the human-readable name for this node type, you would use:
<?php$node = node_load(1);
$types = node_get_types();
$human_readable = $types[$node->type]->name;
?>
I want to retrieve all nodes
PermalinkI want to retreive all nodes of type forum. Then what will be the code?
Be interested to see if this
PermalinkBe interested to see if this can be done with a drupal function, however your best bet is to create a View then use views_get_view_result('view_name') to return a PHP array of objects, each one representing a row and its fields in your view.
In Drupal 7, you'll need to
PermalinkIn Drupal 7, you'll need to use :
<?php$types = _node_types_build()->types;
?>
Thanks
Arshad
Use node_type_get_types()
PermalinkUse node_type_get_types() instead.
Note that this function
PermalinkNote that this function returns node objects. if you want a simple list of node types as an array, use node_type_get_names() instead.