Hi there, First, these examples have seriously helped me at nearly every step of my big project. next, If I missed this (the solution to my query) somewhere else I apologize, but I am very confused as to how one might create an update, for example, if you build a node module, and then after it is out in the wild, you need to add an additional field to the nodetype.

My feature request would be a node_example update to answer the question:

how does one go about creating a deployable update if you created your node using the technique in this example

.

I created my node type, using the following in the .install file:

field_info_instance
field_create_field
hook_installed_fields
hook_installed_instances

and these in my .module file:

hook_node_info
hook_form

This is not exactly like the node example, but very similar. Anyhow, I now find myself scratching my head on how to deply and update to add a field to this complex content type that is already in use by users.

Thanks for all the hard work on these examples! and any help in advance :)
Franco Nogarin

Comments

alberto56’s picture

It would be a plus to demonstrate upgrading and updating. I have started a discussion on this in #1699440: A dedicated to module to demonstrate hook_update_N() and automatically testing upgrades and updates

mile23’s picture

hook_installed_fields() and hook_installed_instances() are part of the EntityAPI API, as opposed to the Entity API. :-)

That is, not the core entity API, but the contrib Entity API.

But yah, an example of how to add/remove fields during an update would be great.

amphioxus’s picture

This would be extremely helpful to me as well. After searching for a while, I still haven't come up with a way of doing exactly what you're trying to do. Uninstalling and re-installing with the extra fields is out of the question for me, because there's just too much data already there. Did you find any information to help you with your quest?

Edit: I think I figured out how to add a simple field to my already existing node. (I got my code mostly from this comment.) Since I don't need to update any existing nodes, there's no $sandbox for multipass updating. Not sure whether I'm following best practices, but here's my code (in the module's install file) in case it helps someone:

/**
 *
 * Add "my_new_field" field to "my_exising_node"
 *
 */
function my_module_update_7001() {
  // just checking whether field is already there:
  if (!field_info_field('my_new_field')) {
    $new_field = array(
      'field_name' => 'my_new_field',
      'cardinality' => 1,
      'type' => 'text',
      'settings' => array(
          'max_length' => 255,
      ),
    );
    field_create_field($new_field);

    // Create the instance on my content type (bundle):
    $new_field_instance = array(
        'field_name' => 'my_new_field',
        'entity_type' => 'node',
        'bundle' => 'my_exising_node', // this is the name of the content type I already created with pre-existing fields
        'label' => 'My new field label',
        'widget' => array(
          'type' => 'text_textfield',
        ),
    );
    field_create_instance($new_field_instance);
    
    // debug message:
    drupal_set_message($message = t('The new field has been installed successfully.'), $type = 'status');
  } else {
    drupal_set_message($message = t('No field has been installed'), $type = 'status');
  }
}
mile23’s picture

Before you attach, you'd really also want to check that the node type exists, with node_type_get_type().

Then check that the field isn't already attached with field_info_instance(). Also use this function to verify that it's been attached after you attach it.

We really should have an example module for attaching and removing fields. Then people could just copy those techniques into their updates.

Any volunteers to write it? :-)

mile23’s picture

Status: Active » Needs work

Hmm. As it turns out, it was me.

Node Example shows you how, in node_example_node_type_insert(): http://drupalcode.org/project/examples.git/blob/HEAD:/node_example/node_...

It would be great if someone wanted to finish the job and incorporate this into an update/upgrade process, with tests.

mile23’s picture

Issue summary: View changes
Status: Needs work » Fixed

I'm going to call this fixed, because node_example_node_type_insert() shows us how to add fields.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.