1.20.x node.module node_type_save($info)

Saves a node type to the database.

Parameters

object $info: The node type to save; an object with the following properties:

  • type: A string giving the machine name of the node type.
  • name: A string giving the human-readable name of the node type.
  • base: A string that indicates the base string for hook functions. For example, 'node_content' is the value used by the UI when creating a new node type.
  • description: A string that describes the node type.
  • help: A string giving the help information shown to the user when creating a node of this type.
  • custom: TRUE or FALSE indicating whether this type is defined by a module (FALSE) or by a user (TRUE) via Add Content Type.
  • modified: TRUE or FALSE indicating whether this type has been modified by an administrator. Currently not used in any way.
  • locked: TRUE or FALSE indicating whether the administrator can change the machine name of this type.
  • disabled: TRUE or FALSE indicating whether this type has been disabled.
  • has_title: TRUE or FALSE indicating whether this type uses the node title field.
  • title_label: A string containing the label for the title.
  • module: A string giving the module defining this type of node.
  • orig_type: A string giving the original machine-readable name of this node type. This may be different from the current type name if the locked field is 0.

Return value

int: A status flag indicating the outcome of the operation, either SAVED_NEW or SAVED_UPDATED.

File

modules/node/node.module, line 509
The core module that allows content to be submitted to the site.

Code

function node_type_save($info) {
  $type = node_type_set_defaults($info);
  $type_data = (array) $type;
  $type_data['has_title'] = (bool) $type_data['has_title'];
  $type_data['modified'] = (bool) $type_data['modified'];
  $type_data['disabled'] = (bool) $type_data['disabled'];

  // Don't ever save the is_new flag or old_type into config.
  if (isset($type_data['is_new'])) {
    unset($type_data['is_new']);
  }
  if (isset($type_data['old_type'])) {
    unset($type_data['old_type']);
  }

  $config = config('node.type.' . $type->type);
  foreach ($type_data as $key => $value) {
    $config->set($key, $value);
  }
  $config->save();

  if (empty($type->is_new)) {
    if (!empty($type->old_type) && $type->old_type != $type->type) {
      field_attach_rename_bundle('node', $type->old_type, $type->type);
    }
    module_invoke_all('node_type_update', $type);
    $status = SAVED_UPDATED;
  }
  else {
    field_attach_create_bundle('node', $type->type);
    module_invoke_all('node_type_insert', $type);
    $status = SAVED_NEW;
  }

  // Delete the old node type (if any).
  if (isset($type->old_type) && $type->type !== $type->old_type) {
    config('node.type.' . $type->old_type)->delete();
  }

  // Clear the node type cache.
  node_type_cache_reset();

  return $status;
}