Dmitrii Mikhailov
Dmitrii Mikhailov

Reputation: 5231

hook_node_type_insert is not invoked

I try to add some fields to content type through this hook as it done in node_example module in Drupal 7 examples but it's not even invoked. What can be wrong?

function education_node_type_insert($content_type){
            $fields = _anketa_installed_fields();
            foreach($fields as $field){
                field_create_field($field);
            }

            $instances = _anketa_installed_instances();
            foreach($instances as $instance){
                $instance['entity_type'] = 'node';
                $instance['bundle'] = 'anketa';
                field_create_field($instance);
            }
    }

Upvotes: 1

Views: 456

Answers (6)

Leonid Usachov
Leonid Usachov

Reputation: 341

Why don't you use hook_node_insert?

It's a working example for adding components to every new webform:

/**
 * Implements hook_node_insert().
 */
function modulename_node_insert($node) {
  if($node->type == 'webform' && $node->is_new) {
    module_load_include('inc', 'webform', 'includes/webform.components');

    $components = array();
    $components[0] = array(
      'name' => 'Submitted Page URL',
      'nid' => $node->nid,
      'form_key' => 'hidden_submitted_page_url',
      'type' => 'hidden',
      'mandatory' => 0,
      'weight' => 99,
      'pid' => 0,
      'value' => '',
      'required' => 0,
      'extra' => array(
        'hidden_type' => 'hidden',
        'description' => '',
        'wrapper_classes' => 'hidden-submitted-page-url-wrap',
        'css_classes' => 'hidden-submitted-page-url',
        'private' => 0,
      ),
    );
    $components[1] = array(
      'name' => 'Referrer Page URL',
      'nid' => $node->nid,
      'form_key' => 'hidden_referrer_page_url',
      'type' => 'hidden',
      'mandatory' => 0,
      'weight' => 99,
      'pid' => 0,
      'value' => '',
      'required' => 0,
      'extra' => array(
        'hidden_type' => 'hidden',
        'description' => '',
        'wrapper_classes' => 'hidden-referrer-page-url-wrap',
        'css_classes' => 'hidden-referrer-page-url',
        'private' => 0,
      ),
    );
    foreach ($components as $component) {
      webform_component_insert($component);
    }
  }
}

Upvotes: 0

Berend de Boer
Berend de Boer

Reputation: 2112

You will need to actually delete your node type manually after the first install.

function example_uninstall () {
  node_type_delete ('my_type');
}

There are probably good reasons why Drupal doesn't do this by default: what's the correct behaviour?

Upvotes: 0

liquidcms
liquidcms

Reputation: 341

When you disable a node module and uninstall it Drupal doesn't clear out the entries in the node_type table for the node types associated with your module (i would call this a bug in Drupal core). If those entries remain; the hook_node_type_insert hook does not get run when you re-enable the module.

If you first delete those entries manually from the node_type table; the hook should run.

Upvotes: 2

Sam Wilson
Sam Wilson

Reputation: 4512

Had you already run this before trying to add the fields? Because the education_node_type_insert($type) function is not called if the node type $type is already in the node_type database table, which it will be after the first time that this is run.

I think the correct way to do this is to add the fields in an implementation of hook_install instead (in yourmod.install, and checking at the same time whether they've already been added perhaps).

Also, during development, you need to uninstall-reinstall (e.g. drush dis -y yourmod && drush uninstall -y yourmod && drush en -y yourmod) every time you make a change to the fields.

Upvotes: 0

JaT5
JaT5

Reputation: 49

When working on custom modules - whether building, debugging, QAing, migrating, updating, etc- the following steps often help. Without looking at your code closely, I'd suggest trying these steps:

Disable the module, uninstall/reinstall (if ok to wipe the module data from the DB), re-enable the module then run update.php. Check Drupal & PHP/MySQL logs, run cron.php clear browser & Drupal cache, logout & log back in, edit roles & perms. Rinse. Repeat... often knocks unexplainable issues loose.

Also this all assumes you've already confirmed overall module architecture & function name/spellings are ok. If all else fails, try installing on another instance to see if the issue can be replicated.

Upvotes: 0

Beebee
Beebee

Reputation: 160

Have you tried uninstalling (Not disable, but really uninstall after you disable it from Uninstall tab) the module and re-enabling it again?

Upvotes: 0

Related Questions