Is it possible to pre-populate an empty field (of any type) with data?

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
spessex's picture

I've been stuck on this issue all day now. Is there a way to pre-populate an 'empty' field of any type with data?

I've tried Rules, specifically 'Set a data value' but it does not appear to allow data values to be set on empty fields and usually reports back (in 'Recent Log Messages') 'The variable or parameter entity is empty'.

The reason I need this is that I have a 'profile type' (using Profile2) with various fields. These fields are updated - when required - using VBO (Views Bulk Operations) and unfortunately they can only be updated by VBO if the fields have existing content.

Any help or direction would be greatly appreciated.

Thank you

Stephen

Comments

Specifically, how can I get a

spessex's picture

Specifically, how can I get a new profile type to automatically populate data to it's fields as soon a user registers (taking heed of the information above where Rules does not appear to allow data values to be set on blank content.

Field system

Steven Jones's picture

You should be able to do this with the normal field system?

If you edit the field, then there should be a section that looks like this:

Only local images are allowed.

(you may need to view the image here: http://www.evernote.com/shard/s198/sh/57b124ba-5d0a-4fa2-a70e-2bb3646df0...)

I guess that the way that profile2 works might stop those default values being used, is that what you're seeing?

Thanks Steven. Yes I can see

spessex's picture

Thanks Steven.

Yes I can see the Default Value but the problem stems from the fact that I need the field to autosave without the user having physically save it themselves (click save) so essentially it prepopulates itself with the data/or the default data as soon as the user joins the system. It appears that even if I have a default value Rules still see's it as 'The variable or parameter entity is empty' when trying to set a 'Set a data value'.

Please note I have also

spessex's picture

Please note I have also noticed that 'Set a data value' will only work and write data to a field if that field has or 'used to have' data contained within it. If the field is brand new/fresh and has never had any data the 'Set a data value' will not write to it.

To reproduce try the following:

  1. Install Profile2.
  2. Go to 'Profile Types'
  3. 'Add a profile type'.
  4. 'Manage fields'
  5. 'Add a new field' - a 'text' field for instance. Do the usual saves etc.
  6. Create three different user profiles.
  7. Add data to the 'text field' of one of them, save and leave it there. Do the same for the second user but this time go back into the profile and delete the data from the the text field so nothing is there again. On the third user do not do anything to profile i.e do not edit or save so it's never had any data contained within it.
    Now to add the rule - there's various ways to test and they all have the same problem with not being able to save data but for now just try method below or any method of your choosing
  8. 'Add a new rule'
  9. Event ' User has logged in'
  10. Condition 'Entity has field', Parameter:Entity [account:profile-name-of-profile], Field: [name of field]
  11. Actions. 'Show a message on the site' (this is to show whether the rule is working or not to this point). Choose what ever message you want.
  12. Add the final Action (which is the main problem with this issue). 'Set a data value'. Parameter: Data:[account:profile-name-of-profile] Value: [any text].
    Now to test the rule
  13. Login for one by one for all the three users. You will notice that the data will be set for the first two users but not the third (where data has never been set). But please notice user 2's data is set even though there is no data which had been deleted. I believe it has been set because Drupal is aware that it 'used' to have data present.

You probably need to add the

PQ's picture

You probably need to add the 'Save entity' action after setting the data value, otherwise the rule isn't doing anything.

As for the second user, If you still have issues after that change then it might be worth adding a second condition, 'Data comparison' and seeing if the field matches '' (and empty string) and wrapping both conditions in and 'OR' group, but first check with the 'Save entity' added as this second step might be redundant.

Hi Paul Unfortunately 'Save

spessex's picture

Hi Paul

Unfortunately 'Save Entity' doesn't work. I've even tried it at its most base level i.e. Event User Logs in, Action "Save Entity' (I have placed default text in the field to see if it would save).

Devel Debug generally reports the following:

Rules debug information:
"

Reacting on event User has logged in.
• 0 ms Reacting on event User has logged in.
• 16.444 ms Evaluating conditions of rule Test. [edit]
• 16.462 ms AND evaluated to TRUE.
• "

Rule Test fires. [edit]
• 0 ms Rule Test fires.
• 0.193 ms Evaluating the action drupal_message. [edit]
• 4.078 ms The variable or parameter data is empty.
• 4.795 ms Unable to evaluate action entity_save. [edit]
• 4.839 ms Rule Test has fired.
• 21.361 ms Finished reacting on event User has logged in.

As for the second user this was really only to see the differences/experimentation as there will not be a case of such a second user with this method - it's always going to be a new/the third user case where I need to add default text to their new profile without them having to go in and physically save it themselves so unfortunately there will never be case for the need to compare data. However I did try this before as an experiment using 'data comparison' just to see if it evaluated to 0 or similar but again it couldn't see the variable or parameter.

If you do have any more thoughts or any luck in doing something similar please keep me posted :)

Could you export the rule and

PQ's picture

Could you export the rule and paste it here.

This is one of version of

spessex's picture

This is one of version of many (I've also tried it without the condition 'entity had field' but with no luck.

{ "rules_test" : {
"LABEL" : "Test",
"PLUGIN" : "reaction rule",
"REQUIRES" : [ "rules" ],
"ON" : [ "user_login" ],
"IF" : [
{ "entity_has_field" : {
"entity" : [ "account:profile-live-surveys" ],
"field" : "field_test_text"
}
}
],
"DO" : [
{ "entity_save" : { "data" : [ "account:profile-live-surveys" ], "immediate" : 1 } },
{ "drupal_message" : { "message" : "The value of Link in Active Surveys is empty and rules will now add a link." } }
]
}
}

If you want to autosave, then

aburrows's picture

If you want to autosave, then look at the autosave module

Thanks for this. Took a look,

spessex's picture

Thanks for this. Took a look, useful for other projects I'll be working on but unfortunately only works with nodes and not the Profile entity which is the issue I'm having here.

Looks like the problem is

PQ's picture

Looks like the problem is that a profile2 profile is not automatically created when a user account is created, only when one of it's fields gets a value. Therefore your rule was failing because it was trying to set a value of a field on an entity that didn't exist.

I've got it woking using a separate rules component to add a profile2 profile if the user account doesn't already have it, and a reaction rule that checks the account has a profile and the filed is empty, then it runs the component and then sets the field value and saves the entity:

Component:

{ "rules_add_profile_if_required" : {
    "LABEL" : "Add profile if required",
    "PLUGIN" : "rule",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "account" : { "label" : "Account", "type" : "user" } },
    "IF" : [ { "data_is_empty" : { "data" : [ "account:profile-person" ] } } ],
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "profile2",
            "param_type" : "person",
            "param_user" : [ "account" ]
          },
          "PROVIDE" : { "entity_created" : { "profile" : "Profile" } }
        }
      }
    ]
  }
}

Rule:

{ "rules_test_rule" : {
    "LABEL" : "test rule",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "user_login" ],
    "IF" : [
      { "OR" : [
          { "data_is_empty" : { "data" : [ "account:profile-person" ] } },
          { "data_is_empty" : { "data" : [ "account:profile-person:field-test" ] } }
        ]
      }
    ],
    "DO" : [
      { "component_rules_add_profile_if_required" : { "account" : [ "account" ] } },
      { "drupal_message" : { "message" : "set field" } },
      { "data_set" : {
          "data" : [ "account:profile-person:field-test" ],
          "value" : "default value goes here"
        }
      }
    ]
  }
}

Hi Paul. I hadn't noticed

spessex's picture

Hi Paul.

I hadn't noticed this come in yesterday. Many, many thanks for taking your time to look into this. I'll take a look at this and see how it works :)

Amazing. It looks like it's

spessex's picture

Amazing. It looks like it's worked. All I had to do was tweak the Condition to select the other profile and it worked absolutely perfectly. A real life saver. I was very close to giving up on a near completion project as this is the crux of the main part (this will now allow me a method to add surveys links to users profiles that are specific to them using VBO ' set entity' - or similar). Thanks again.

Condition

{ "rules_add_profile_if_required" : {
"LABEL" : "Add profile if required",
"PLUGIN" : "rule",
"REQUIRES" : [ "rules" ],
"USES VARIABLES" : { "account" : { "label" : "Account", "type" : "user" } },
"IF" : [ { "data_is_empty" : { "data" : [ "account:profile-live-surveys" ] } } ],
"DO" : [
{ "entity_create" : {
"USING" : {
"type" : "profile2",
"param_type" : "live_surveys",
"param_user" : [ "account" ]
},
"PROVIDE" : { "entity_created" : { "profile" : "Profile" } }
}
}
]
}
}

Rule

{ "rules_test_rule" : {
"LABEL" : "test rule",
"PLUGIN" : "reaction rule",
"REQUIRES" : [ "rules" ],
"ON" : [ "user_login" ],
"IF" : [
{ "OR" : [
{ "data_is_empty" : { "data" : [ "account:profile-live-surveys" ] } },
{ "data_is_empty" : { "data" : [ "account:profile-live-surveys:field_test_text" ] } }
]
}
],
"DO" : [
{ "component_rules_add_profile_if_required" : { "account" : [ "account" ] } },
{ "drupal_message" : { "message" : "set field" } },
{ "data_set" : {
"data" : [ "account:profile-live-surveys:field_test_text" ],
"value" : "default value goes here"
}
}
]
}
}

no problem... good luck with

PQ's picture

no problem... good luck with the project.

P.

Along a similar path...

System Lord's picture

I have a profile2 profile with a "Postal Address" field type. Users can create content and I want a map to be displayed on their content automatically of their registered location (postal address from above). Is there a module out there that I haven't found that can retrieve their address from their profile and have it prepopulate an address field in their "new content" which would then render a map? I would rather not make my users enter their address every time they create content.

United Kingdom

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week