By bonn on
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
_
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.
There will be a warning on
There will be a warning on content-type deletion (if it is being used by a content).
thx for the info.
You might want to check the
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.
CCK or Drupal 7 Field API
Hi,
Are you using CCK or the new Drupal 7 Field API?
Hope that helps.
Jason
As I understand, the title of
As I understand, the title of this thread should be Add Content type programmatically, not CCK.
take a look through Examples
should you still be looking...take a look through Examples the node_example seems to cover this topic.
The uninstall process with hook_uninstall()
With the uninstall process you do not need to redefine the content type. Instead try this:
Well, this example seems very
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.
Modify code
Replace with following
Hi,
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