1

I have simple question concerning enabling a custom module after having disinstalled it. The module provides a simple creation of a new content type based on node content. It implements a hook_install() function that first time i enabled the module worked perfectly. For debugging reasons i had to disable and unistall the module, then when i tried to enable again the module the system checks it as enabled but the new content type that is the result of the hook_install() function does not appear anymore.

It seems to me that when i try to re-activate the module the installation script doesn't work anymore, and this although i cleared the cache after unistallind the module the first time.

Any ideas?

Here's the install code:

function bachecanews_install() {
  $t = get_t();
  $node_bachecanews = array(
    'type' => 'node_bachecanews',
    'name' => $t('Bacheca News'),
    'base' => 'node_content',
    'description' => $t('Usa Bacheca News per inserire le notizio sul sito.'),
    'body_label' => $t('Contenuto della News')
  );
  $content_type = node_type_set_defaults($node_bachecanews);
  node_add_body_field($content_type);
  node_add_date_field($content_type);
  $status = node_type_save($content_type);
  $t_args = array('%name' => $content_type->name);
  if ($status == SAVED_UPDATED) { // in caso di modifica
    drupal_set_message($t('Il nuovo tipo di contenuto %name è stato modificato.', $t_args));
  } 
  elseif ($status == SAVED_NEW) { // in caso di creazione
    drupal_set_message($t('Il nuovo tipo di contenuto %name è stato creato.', $t_args));
    watchdog('node', 'Creato il nuovo tipo di contenuto %name.', $t_args, WATCHDOG_NOTICE, l($t('view'), 'admin/structure/types')); 
  }
}

function bachecanews_uninstall() {
  $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  $result = db_query($sql, array(':type' => 'node_bachecanews'));
  $nids = array();
  foreach ($result as $row) {
    $nids[] = $row->nid;
  }
  node_delete_multiple($nids);
  node_type_delete('node_bachecanews');
}
2
  • I have disabled the module and then used the uninstall tab in the modules GUI. No the module does not create any tables in the database but only sets a new node type. The install file contains also a hook_uninstall() function that deletes all references to the module including the node type just created in the node_type table and all the record of that particular node type. In fact when I disinstalled the module all was deleted fine. Now when i try to reinstall the module nothing happens... i can post the code of the install file :
    – Marco
    Commented Apr 5, 2013 at 15:03
  • can you post the code of the install module?
    – Aboodred1
    Commented Apr 5, 2013 at 15:46

1 Answer 1

1

I found out that you have 2 issues with your hook_install code.

  1. You're adding body field first then saving the node type. It should be the opposite.
  2. There's no such function called node_add_date_field in Drupal package, unless you've created it yourself in your custom module.

Your hook_install should be as the following code:

function bachecanews_install() {
  $t = get_t();
  $node_bachecanews = array(
    'type' => 'node_bachecanews',
    'name' => $t('Bacheca News'),
    'base' => 'node_content',
    'description' => $t('Usa Bacheca News per inserire le notizio sul sito.'),
    'body_label' => $t('Contenuto della News')
  );
  $content_type = node_type_set_defaults($node_bachecanews);
  $status = node_type_save($content_type);
  node_add_body_field($content_type);
  $t_args = array('%name' => $content_type->name);
  if ($status == SAVED_UPDATED) { // in caso di modifica
    drupal_set_message($t('Il nuovo tipo di contenuto %name è stato modificato.', $t_args));
  } 
  elseif ($status == SAVED_NEW) { // in caso di creazione
    drupal_set_message($t('Il nuovo tipo di contenuto %name è stato creato.', $t_args));
    watchdog('node', 'Creato il nuovo tipo di contenuto %name.', $t_args, WATCHDOG_NOTICE, l($t('view'), 'admin/structure/types')); 
  }
}

If you want to add additional field to your custom content type and this field is not body field, you use field_create_field()

As an example, you can look at standard_install().

1
  • 1
    Dear Aboodred1 thank you so much, it seems that the problem actually was the position of node_add_body_field(). Without your observation i guess i would have done this error endlessly since i don't remember any indication on documentation about positioning the add field function after saving the custom content type. Now i have to understand logically why has to be done this way...
    – Marco
    Commented Apr 5, 2013 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.