I'm in the process of writing a module that will essentially publish a node to two sites (the one the user is creating the node on and another one specified in code). Both sites run on the same Drupal installation, with all the needed database info defined in $databases.

In short, part of the process involves populating a hidden field in the second website's "article" content type. That field isn't defined on the first site because it doesn't need to be referred to there. I want to make Drupal "aware" of the field by the time it performs node_save(), but without actually creating the field on the first site, because the resulting tables in the database would go unused.

So, the basic logic of my module's code is:

  • Modify the node form slightly to include a new checkbox
  • If it's checked:
    • Manipulate $node to specify values for a field that only exists on the other site
    • Use hook_node_insert and hook_node_update to switch to the other site's database and perform a node_save in it

It's a bit more complex than that and the other site also checks said field's value to display a message, but the steps above are the ones relevant to my problem. Initially I thought just adding an array for the field in $node would do the trick, but then I learned that the field needs to have an instance attached to the bundle before any subsequent database interactions will do anything with it. In order to do that, it looks like the standard procedure is to use field_create_field and field_create_instance - only I would like to get away with only using the latter, since I don't want the field to actually exist on the first site, only the second one.

Are there any reliable ways of achieving this? I hope I explained it well enough but will try to provide more detail if necessary.


I've figured out a solution to this, although I'm not sure if it's the best performance wise. It would seem that even after switching database with db_set_active(), node_save() (and presumably any other field<>database operations) will continue to use the field info as defined beforehand, so even if you've specified a value for a field that only exists in the second database, it will assume the field doesn't exist. Using field_info_cache_clear() right after db_set_active() solves the problem.

Obviously clearing any cache in a hook like this sort of defeats the purpose of the cache; fortunately in my case it won't be necessary too often.

If anyone knows of a better way of handling it without clearing any caches or creating the field on both/all sites, I'd love to know. I won't mark this as the accepted answer for the time being.

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.