Drupal node_type_get_type()和hook_uninstall()

时间:2012-02-20 16:04:01

标签: drupal drupal-7

我有一个模块job_post,可以安装job_post内容类型。

在此模块中,我hook_unistall()要求node_type_delete()功能删除我的内容类型。

在unistalling过程之后,我有来自Drupal核心模块comment的错误,该错误在module_invoke_all('node_type_delete', $info)之后从node_type_delete()触发。

错误如下,并重复8次(因为comment_node_type_delete()中的循环):

Notice: Trying to get property of non-object in comment_node_type_delete()
(line 343 of ....\comment.module).

我遇到此错误,因为node_type_delete()函数中的$info变量为false

我的问题是,为什么安装我的模块时以及当我在任何页面上打印var_dump(node_type_get_type('job_post'))时,我都有一个对象,但是当我尝试在我的unistall函数中打印相同的代码时,我得到了是和这个错误?

job_post.install

/**
 * Implements hook_install().
 */
function job_post_install() {
  node_types_rebuild();

  $types = node_type_get_types();
  node_add_body_field($types['job_post']);

  $body_instance = field_info_instance('node', 'body', 'job_post');
  $body_instance['type'] = 'text_summary_or_trimmed';

  field_update_instance($body_instance);
}

/**
 * Implements hook_uninstall().
 */
function job_post_uninstall() {
  $instances = field_info_instances('node', 'job_post');
  foreach ($instances as $instance_name => $instance) {
    field_delete_instance($instance);
  }

  // Force rebuild of the node type cache
  // as Clive suggested didn't help
  // _node_types_build(TRUE);

  node_type_delete('job_post');

  field_purge_batch(1000);
}

job_post.module

/**
 * Implements hook_node_info() to provide our job_post type.
 */
function job_post_node_info() {
  return array(
    'job_post' => array(
      'name' => t('Job Post'),
      'base' => 'job_post',
      'description' => t('Use this content type to post a job.'),
      'has_title' => TRUE,
      'title_label' => t('Job Title'),
      'help' => t('Enter the job title and job description')
    )
  );
}

/**
 * Implement hook_form() with the standard default form.
 */
function job_post_form($node, $form_state) {
  return node_content_form($node, $form_state);
}

注意:此模块示例取自Pro Drupal 7 Development本书(第141页),稍作修改,即使使用原始版本也会出错。

2 个答案:

答案 0 :(得分:2)

嗨,参考文档Drupal 7的核心模块。存在使用问题。

hook_node_info自动定义内容类型。以这种方式创建的内容类型,已自动卸载 - 禁用。

核心博客模块定义了hook_node_info但在hook_uninstall上没有运行任何node_type_delete

http://api.drupal.org/api/drupal/modules%21blog%21blog.module/function/blog_node_info/7

当您在hook_uninstall上调用node_type_delete('job_post');时,节点类型信息已经消失。由于该注释模块会引发错误。

通常,您应该只删除与您的内容类型相关的任何数据。其余部分由核心完成。

此外,如果您真的想要创建/删除您的内容类型,则可能不会使用hook_node_info。您可以在安装/卸载挂钩上手动创建/删除内容类型。

此处卸载示例: http://public-action.org/content/drupal-7-field-api-drupal-7-adding-custom-content-type-custom-fields-field-api

答案 1 :(得分:1)

似乎节点类型缓存由于某种原因尚未完全构建,请在调用node_type_delete()之前尝试强制重建,并且所有节点类型都应该可用:

// Force rebuild of the node type cache
_node_types_build(TRUE);

// Delete our content type
node_type_delete('job_post');