Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hello
I'm trying to prepopulate a couple of CCK text fields that are used in several different content types (it's basically contact information), however I cannot find a working solution. How should I go by doing this from template.php for instance?
I would really like the solution to work whenever the field is used - even if it's in a group or not.
I know of the Prepopulate module, and how that can work in this scenario - however I've got some business logic needed for the field values and I don't want the arguments to be passed in the query-string.
Best regards,
Thomas René Sidor
Comments
Comment #1
markus_petrux CreditAttribution: markus_petrux commentedI think you need to use hook_form_alter(). You have an example in Prepopulate module itself. That one gets the values from the URL, your may get the values from somewhere else... or based on any logic that you may want to apply.
Comment #2
Sidor CreditAttribution: Sidor commentedThanks for the reply.
I've already had a look at the Prepopulate module, but it doesn't really work well for CCK fields - especially not when they're in groups. Then the reference for the field is something like $form['group']['field_something'][0]['#default_value']['value'] as far as I can remember.
What I really could use is a method that iterates the form object and finds the proper location of the form_something field (independent on what group it is in) and then assigns it some value. This is because I'm using field_something a lot of places and in differently named groups.
Comment #3
markus_petrux CreditAttribution: markus_petrux commentedNote that the fieldgroup module has a weight of... 10, IIRC. That means its hook_form_alter() implementation runs after the one implemented by CCK itself, which uses a module weight of 0. If your module has a weight between 0 and 10 (exclusive), it will run between CCK and fieldgroup, so you do not need to care about $form['group'] stuff.
Comment #4
Sidor CreditAttribution: Sidor commentedGreat - thanks to your help I managed to create a working solution with some changes to the Preload module.
Here's the code if anyone else should stumble upon this post in the future:
Basically the module (called altomtrekroner) sets the default values for three fields (currently they're hard coded - but you could easily create a solution that take the field names from a list and does the same thing - I just won't bother today) based on the following conditions
- If the user has saved a node with values for the fields previously, those values are loaded as default the next time.
- If the user has never used those fields before values from the user profile are used (if the user is logged in of course).
The values are stored in a cookie.