0

I have a module that created a custom content type with some fields.

Now I need to update the content type with a new field and wanted to know what hook do I use? As of now my content type was created via the .install file but need a way to process the changes without uninstalling and re-intsalling the module. My .install file

function example_node_install() {
  $t = get_t();

  // Define the node type
  $example_node = array(
   'type' => 'example_node',
   'name' => $t('Node Example'),
   'base' => 'node_content',
   'description' => $t('This is an example node type.'),
   'body_label' => $t('Example Description')
  );

  //Add defualts
  $content_type = node_type_set_defaults($example_node);
  node_add_body_field($content_type);

  // Create the field
  $field = array(
    'field_name' => 'item_description',
    'cardinality' => 1,
    'type' => 'text',
  );
  field_create_field($field);

  // Create the instance map description.
  $instance = array(
    'field_name' => 'item_description',
    'label' => 'The item description.',
    'bundle' => 'example_node',
    'entity_type' => 'node',
    'widget' => array(
      'type' => 'text_textfield',
      'weight'  => 2,
    ),
  );
  field_create_instance($instance);

  //Save Content Type
  node_type_save($content_type);
}

Any guidance will be appreciated.

1

Try the hook_update_n function https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_update_N/7

You would define a hook_update_n function in your install file in which you place the functionality to add your field to your content type. See for example: http://cgit.drupalcode.org/entity/tree/entity.install#n35

After added this functionality run http://example.com/update.php

A second option would be to use the features module to export your content type to a feature and when you are making changes in the UI to that content type (like adding a field for example) you can export the content type again to the feature and deploy that feature to your other (live) environment and revert the feature (which will bring the new code (the newly added field) into the database.

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.