1

I want to add a new date field to an existing content type, and set the new field automatically for existing nodes to be equal to another date field from that content type.

For example, if the new field is called 'Event date', and there is an existing field 'Publication Date', I want the 'Event date' field to contain the date from the 'Publication date' field for all existing nodes, without assigning each one manually.

Is this possible?

0

It is possible and the best way to do it is in a custom module. Here's how:

  • Assume you have a module called mymodule.
  • Inside mymodule.install create a function called mymodule_update_N where N is a number starting at 7100. See the API documentation for hook_update_N for details but this is what runs when you run update.php
  • Inside your mymodule_update_7100, load the nodes you want to act on and then iterate over them, setting your newly created field to the value from Event Date.

If you have a lot of nodes, you'll want to look to the Batch API system to batch the nodes one by one so you don't run out of PHP memory or run up against the timeout. If it's not a ton of nodes, you don't need to worry about that usually.

And last, but not least, you should use an EntityFieldQuery to get the nodes you want to act on.

Hope that helps. Good luck!

0

I have done this with Rules.

  1. Set up a rule that fires before the content type is saved
  2. Have the rule set the value of the new field to the value of the existing field.
  3. Then, install Admin Views and Views Bulk Operations
  4. Filter the content list to show just that content type
  5. Select them all
  6. Choose Save Content from the Bulk Operations dropdown and follow the steps from there.
  7. Check that the field has populated correctly. If not, check your rule.

If the Save Content option is not available, you can edit the Bulk Operations field in the Content view to enable it.

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.