1

I'm working on a custom module that creates a new content type. Recently I added a new field to the content type, but it doesn't get updated. I don't have any nodes of this type and when I disable my module, the content type is deleted. After I enable it again, the content type is back, but with the old setup and without the newly added field. I'm probably overlooking something very basic, but I couldn't figure out what. I cleared the cache and run cron before and after I disabled and enabled my module.

I even tried to give a different name to my content type and fields, but then the content type is not created. I bet it's just some small mistyping or something.

What could be the problem?

Here is the code. I put the field arrays into functions, but I don't think this causes any problem.

function simplegal_node_info() {
  return array(
    'simplegal' => array(
      'name' => t('Simple Gallery'),      
      'base' => 'simplegal',
      'description' => t('A simple gallery that will display all images from a given directory'),
      'title_label' => t('Title of gallery'),      
      'locked' => TRUE,
    ),
  );
}
function simplegal_node_type_insert($content_type) {
  if ($content_type->type == 'simplegal') {
    $body_instance = node_add_body_field($content_type, t('Description'));
    field_update_instance($body_instance);
    foreach (_simplegal_installed_fields() as $field) {
      field_create_field($field);
    }
    foreach (_simplegal_installed_instances() as $instance) {
      $instance['entity_type'] = 'node';
      $instance['bundle'] = 'simplegal';
      field_create_instance($instance);
    }
  }
}

function _simplegal_installed_fields() {
  return array(
    'simplegal_gallery_dir' => array(
      'field_name' => 'simplegal_gallery_dir',
      'cardinality' => 1,
      'type'        => 'text',
      'settings'    => array(
        'max_length' => 60,
      ),
    ),
    'simplegal_thumb_width' => array(
      'field_name' => 'simplegal_thumb_width',
      'cardinality' => 1,
      'type'        => 'text',
      'settings'    => array(
        'max_length' => 10,
      ),      
    ),
    'simplegal_thumb_height' => array(
      'field_name' => 'simplegal_thumb_height',
      'cardinality' => 1,
      'type'        => 'text',
      'settings'    => array(
        'max_length' => 10,
      ),
    ),
  );
}
function _simplegal_installed_instances() {
  return array(
    'simplegal_gallery_dir' => array(
      'field_name' => 'simplegal_gallery_dir',
      'label'       => t('The directory of the gallery'),
      'widget'      => array(
        'type'    => 'text_textfield',
      ),      
    ),
    'simplegal_thumb_width' => array(
      'field_name' => 'simplegal_thumb_width',
      'label'       => t('Width of thumbnails'),
      'widget'      => array(
        'type'    => 'text_textfield',
      ),      
      'default_value' => variable_get('simplegal_thumb_width',120),
    ),
    'simplegal_thumb_height' => array(
      'field_name' => 'simplegal_thumb_height',
      'label'       => t('Height of thumbnails'),
      'widget'      => array(
        'type'    => 'text_textfield',
      ),
      'default_value' => variable_get('simplegal_thumb_height',120),
    ),
  );
}
  • There, I attached the code. I can only see the gallery dir field, but not the other two. – Moha Aug 13 '13 at 12:19
1

My wild guess is:

From hook_node_type_insert documentation:

Respond to node type creation. This hook is invoked from node_type_save() after the node type is added to the database.

Because you're not "Uninstalling" the module after you disable it, the content type will be kept, but it will simply be flagged as "Disabled" in the "disabled" column in the {node_type} table.

Therefore that hook is not invoked when you re-enable the module, hence the new fields not added.

So what you need to do is after disabling the module, go to the Uninstall tab and uninstall it, or if you're using drush:

drush dis -y simplegal && drush pm-uninstall -y simplegal

  • Ok, I disabled my module, then uninstalled it, then deleted every reference of it from the DB, cleared the cache and then re-enabled my module. Now I only have the title in my content type, nothing else. I can't see any of the three custom fields. – Moha Aug 14 '13 at 12:09
  • Ok, I found the error. There was a revision table in the DB for my module that blocked everything. After I dropped it, my fields are finally there. Thanks for your help! – Moha Aug 14 '13 at 13:14
  • 1
    @Moha thanks for accepting my answer and giving me the cookies. But you should edit my answer and post the exact solution for future visitors (What was the revision table name? Is it field revision? etc), or you should post your answer and accept that instead. – Beebee Aug 14 '13 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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