I have a profile2 field that I want to set with the value of a variable I've provided in an action. The field is list(text) with a machine name of 'field_member_type' and allows two possible values: "Family", "Individual"

In hook_rules_action_info (), the variable is provided like this:

'provides' => array(
      'member_type' => array(
        'type' => 'list<text>',
        'label' => t('Membership type')
      ),
    ),

The action itself loops through line items in a completed commerce order looking for certain products, and returns a keyed array for the variable value in this format:

return array(
      'member_type' => $mtype
    );

Back in rules, I can add this action to a rule and it seems to correctly set the value of the variable as expected. However, when I try to set the data value of the profile2 field using these data selectors:

site:current-user:profile-membership:type -- data to be modified
member-type: -- variable provided

I get the error: "Data selector member-type: for parameter value is invalid."

If I try the same thing with a different profile2 text field (just text, not a list), the value is set without trouble, so I think my problem might be the the structure of the variable?

Hoping I'm on the right track here and am approaching this in a sensible way. Another option would be to set the value of the field in the action, but I haven't tried this yet. I'm setting a number of fields at the same time, and it seemed more natural in rules to keep these actions exposed in the UI.

Thanks in advance for any tips or advice.

Comments

dave bruns’s picture

Component: Rules Engine » Rules Core
Status: Active » Fixed

Just following up on my own post to close this down and help clear the issue queue....

I couldn't get this to work and ended up writing a custom rules action to set the profile2 values in code. Setting profile2 fields in code is straightforward. The relevant part of the code I used in my action is below.

if ($account->uid) {
      // we are only loading the membership profile
      $profile = profile2_load_by_user($account, 'membership');
      // populate profile fields
      $profile->field_member_type['und'][0]['value'] = $mtype;
      $profile->field_member_code['und'][0]['value'] = $mcode;
      $profile->field_member_last_renewal_date['und'][0]['value'] = date("Y-m-d");
      $profile->field_member_expiration_date['und'][0]['value'] = $expdate;
      
      // save profile
      profile2_save($profile);
    }

If anyone in the future runs across this post and knows how to set the value of a list(text) field, please chime in - I'm guessing it's possible with minor adjustments to the variable being returned by the action function.

mitchell’s picture

Status: Fixed » Active

>how to set the value of a list(text) field
#1386746: Set a data value of an item in a list provided by VBO?

TR’s picture

Status: Active » Fixed

This support request was answered by the original poster in #1 and marked as 'Fixed'.

I really don't know why @mitchell reopened it in #2 - no reason was given. Regardless, the support question has been answered to the satisfaction of the original poster.

TR’s picture

Status: Fixed » Closed (fixed)