You are here

Drupal 7 - An Annoying Error: Part the Second

This post refers to an earlier post titled Drupal 7 - An Annoying Error in which I claim to have found a fix to the annoying "comment_node_type_delete()" errors I would get when uninstalling a custom built content type. The solution I found works well when having a base value of "node_content" suits the method you are using to build your content type, such as using the Field API, but if you roll your own base type and delete your custom content type during uninstalling then you are bound to see this from time to time:

The annoying comment_node_type_delete() error

The error message refers to one specific function in the comments.module file (\modules\comment\comments.module) and even lists the lines of code that are causing the error. So I decided to take a look:


/**
 * Implements hook_node_type_delete().
 */
function comment_node_type_delete($info) {
  
  field_attach_delete_bundle('comment', 'comment_node_' . $info->type);     // line 341
  $settings = array(
    'comment',
    'comment_default_mode',
    'comment_default_per_page',
    'comment_anonymous',
    'comment_subject_field',
    'comment_preview',
    'comment_form_location',
  );
  foreach ($settings as $setting) {
    variable_del($setting . '_' . $info->type);    // line 352
  }
}

The first thing that jumps out at me based on the "Trying to get property of non-object" message is that both lines try to reference the $info->type value. Time to start doing some backtracing. Based on the function calls being made on both lines I assume that $info->type should be a string. I decided to take a look at the function which calls hook_node_type_delete(), :


function node_type_delete($type) {
  $info = node_type_get_type($type);  //      condition('type', $type)
    ->execute();
  field_attach_delete_bundle('node', $type);
  module_invoke_all('node_type_delete', $info);

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

The $info variable itself should be a string based on the documentation for node_type_get_type() and so comparing the value being passed to what is expected in comment_node_type_delete() we can see that the comment module is expecting an object in it's current configuration. Let's make a couple changes to straighten that up:


/**
 * Implements hook_node_type_delete().
 */
function comment_node_type_delete($info) {
  $type = _node_extract_type($info);
  field_attach_delete_bundle('comment', 'comment_node_' . $type );
  $settings = array(
    'comment',
    'comment_default_mode',
    'comment_default_per_page',
    'comment_anonymous',
    'comment_subject_field',
    'comment_preview',
    'comment_form_location',
  );
  foreach ($settings as $setting) {
    variable_del($setting . '_' . $type );
  }
}

We call the same function node_type_delete() does to derive the content type string and change the '$info->type' calls to be our new variable '$type' and presto! That should do it.

Tags: 

Post new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.