In Drupal 7, I wanna make a content-type student by installing module Student. Here is what my code look like:

Install Process: Works, I installed it, then check the content was created.

function student_install()
{
    $type_values = array(
		'op' => 'Save content type', 
		'type' => 'student',
        'name' => 'Student', 
		'orig_type' => '', 
		'old_type' => '', 
		'description' => 'Desc', 
		'help' => 'Exp', 
		'title_label' => '', 
		'body_label' => '', 
		'base' => '',
        'custom' => '1', 
		'locked' => '0', 
		'modified' => '1'
	);

    $op = isset($type_values['op']) ? $type_values['op'] : '';

    $type = node_type_set_defaults();

    $type->type = trim($type_values['type']);
    $type->name = trim($type_values['name']);
    $type->orig_type = trim($type_values['orig_type']);
    $type->old_type = isset($type_values['old_type']) ? $type_values['old_type'] : $type->type;

    $type->description = $type_values['description'];
    $type->help = $type_values['help'];
    $type->title_label = $type_values['title_label'];
    $type->body_label = $type_values['body_label'];

    // title_label is required in core; has_title will always be true, unless a
    // module alters the title field.
    $type->has_title = ($type->title_label != '');
    $type->has_body = ($type->body_label != '');

    $type->base = !empty($type_values['base']) ? $type_values['base'] : 'node_content';
    $type->custom = $type_values['custom'];
    $type->modified = true;
    $type->locked = $type_values['locked'];

    variable_set('teaser_length_' . 600);
    variable_set('node_preview_' . 1);

    // Saving the content type after saving the variables allows modules to act
    // on those variables via hook_node_type_insert().
    $status = node_type_save($type);

    node_types_rebuild();
    menu_rebuild();
    $t_args = array('%name' => $type->name);

    if ($status == SAVED_UPDATED)
    {
        drupal_set_message(t('The content type %name has been updated.', $t_args));
    } elseif ($status == SAVED_NEW)
    {
        drupal_set_message(t('The content type %name has been added.', $t_args));
        watchdog('node', 'Added content type %name.', $t_args, WATCHDOG_NOTICE, l(t('view'), 'admin/structure/types'));
    }
}

Uninstall Process: this DOESN'T work. I uninstalled the Student module, but the content still exists.

function student_uninstall()
{
	$type_values = array(
		'op' => 'Save content type', 
		'type' => 'student',
        'name' => 'Student', 
		'orig_type' => '', 
		'old_type' => '', 
		'description' => 'Desc', 
		'help' => 'Exp', 
		'title_label' => '', 
		'body_label' => '', 
		'base' => '',
        'custom' => '1', 
		'locked' => '0', 
		'modified' => '1'
	);
	
	node_type_delete($type_values['type']);

    variable_del('teaser_length_' . $type_values['type']);
    variable_del('node_preview_' . $type_values['type']);
    
    $t_args = array('%name' => $type_values['name']);
    drupal_set_message(t('The content type %name has been deleted.', $t_args));
    watchdog('menu', 'Deleted content type %name.', $t_args, WATCHDOG_NOTICE);

    node_types_rebuild();
    menu_rebuild();
}

My Question:
1. Install Process: Is there any standard ways doing it? coz' I haven't found the docs to create CCK programmatically in Drupal7.
2. Uninstall Process: Why doesn't it work? any helps?

Comments

WorldFallz’s picture

Don't know why it's not working-- but deleting a content type when there is already content created with it can cause all sorts of headaches. Most modules that create content type leave it up to the user to delete them.

bonn’s picture

There will be a warning on content-type deletion (if it is being used by a content).
thx for the info.

scb’s picture

You might want to check the node_example.module found inside the drupal 7 version of the Examples for developers module.

On uninstall, the module deletes all nodes of the custom content type, and also all fields and field instances before deleting the content type itself.

jason_gates’s picture

Hi,
Are you using CCK or the new Drupal 7 Field API?

Hope that helps.
Jason

tomas.teicher’s picture

As I understand, the title of this thread should be Add Content type programmatically, not CCK.

skaught’s picture

should you still be looking...take a look through Examples the node_example seems to cover this topic.

moss.dev’s picture

With the uninstall process you do not need to redefine the content type. Instead try this:

/**
 * Implement of hook_uninstall().
 */

function student_uninstall() {
  // Gather all the example content that might have been created while this
  // module was enabled.  Simple selects still use db_query().
  // api.drupal.org/api/function/db_query/7

  $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  $result = db_query($sql, array(':type' => 'student'));
  $nids = array();
  foreach ($result as $row) {
    $nids[] = $row->nid;
  }

  // Delete all the nodes at once
  // api.drupal.org/api/function/node_delete_multiple/7

  node_delete_multiple($nids);


  // Loop over each of the fields defined by this module and delete
  // all instances of the field, their data, and the field itself.
  // api.drupal.org/api/function/field_delete_field/7

  foreach (array_keys(_student_installed_fields()) as $field) {
    field_delete_field($field);
  }

  // Delete our content type
  // api.drupal.org/api/function/node_type_delete/7

  node_type_delete('student');

  // Purge all field information
  // api.drupal.org/api/function/field_purge_batch/7

  field_purge_batch(1000);
}
spM-1’s picture

Well, this example seems very much as a bare edit of the hook_uninstall of the node_example.install file somewhere referenced in the previous posts. But, this should also not work, because there is this open bug report in the node_example module of content type being still present even after uninstalling the module.

lalit774’s picture

$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  $result = db_query($sql, array(':type' => 'student'));
  $nids = array();
  foreach ($result as $row) {
    $nids[] = $row->nid;
  }
  // Delete all the nodes at once
  // api.drupal.org/api/function/node_delete_multiple/7
  node_delete_multiple($nids);

Replace with following

 $results = db_select('node', 'n')
            ->fields('n', array('nid'))
            ->condition('type', 'student', '=')
            ->execute();
  foreach ($results as $result) {
    $nids[] = $result->nid;
  }
  if (!empty($nids)) {
    node_delete_multiple($nids);
  }

sagar ramgade’s picture

Hi,

You should be deleting the content type student using :

node_type_delete('student'); //this will take care of fields too

Also to remove student nodes you should use hook_node_type_delete (http://api.drupal.org/hook_node_type_delete), inside this you should write your logic of node_delete_multiple.

Also to define a content type, you may use : hook_node_info (http://api.drupal.org/hook_node_info)

Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form