Where to use the php functions when adding fields to existing nodes?

Solutions

Fields are attached to Entities, not Nodes. So the field is either attached to bundle that is your node (for all nodes, pre-existing or not), or it hasnt been attached to that bundle yet.

You can use field_info_field and look at the element bundles to see if your given field is attached to the given Entity. If it isn't you can use field_create_instance to add the field to your entity.

You can get a list of all entity types and inspect their bundle types with the bundles key returned from entity_get_info by calling $site_entity_list = entity_get_info(NULL).

Entities and fields are cached, so be sure you use something like drush cc all to be sure you're looking at up-to-date values for all these function calls.

EDIT: You use these functions and PHP from essentially a small php script. Such business logic could be in a small module with hook_update_n() for instance. Or, you could use Drush and write a very simple PHP snippet using hte above 3 functions and some loops and call it using drush like: drush php-script your-script-filename.php

Tags: Nodes / Entities

Similar questions

Views and theme functions for nodes
Is there a way for a view to pull out x number of nodes of the same content type but use a different theme function for each one depending based on the value of one of its fields? I need each node to be themed according to a field which will have the name of a theme function.
Update existing nodes with fields from XML or CSV
There are 2 greate modules for importing nodes Feeds and Feed Import. Is there a way to update fields of existing nodes on import from XML or CSV file? For example, there is an XML feed from another website with products (id + price) and file with additional data for the same products (id + description). How to import both sources into the same nod...
Importing data into specific fields from CSV - into pre-existing nodes?
I've used the Feeds module before to import nodes and users. I need to import data into new fields on pre-existing nodes - is that possible?
My module adds a CCK field to an existing content type. How do I then modify an existing view to sort using the new field?
I am building a new module ('best_answer') that builds on the functionality an existing module ('answers'). The new module adds a new CCK field ('best_answer_p') to a CCK content type ('answer') that was defined in the original module. The original module also defined a view ('question_answers') which lists answer nodes, sorted by several factors. ...
Can I override/modify an existing hook_widget_info for an existing CCK field type
I want to modify the default Link field, so that instead of just a text field it will always have a Validate Button next to it (for active checking). I don't want to create a new Widget, I want to override the basic link, so that any link field deriving from the original will inherit these changes. Also of course, is it then possible to override th...
Create new table in existing module without affecting existing table data
I have my custom module myCustomModule in drupal 7 website along with installation file to create table. I Have already saved lot of content in table but I need to create a new table in same module without affecting the previous table data. if I add something new in .INSTALL file, I have to uninstall and install module which will remove the existin...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.