1

For my Drupal 7 User Accounts I have 5 separate fields (Field Type: Boolean, Widget: 'Single on/off checkbox') used to indicate comms preferences.

Without losing any data I want to migrate these fields to single field (Field type: 'List', Widget: 'Check boxes/radio buttons') but I am not sure how to achieve this?

1

Step 1: Create a Rules Component

Have a look at this example of a Rules Component (in Rules export format):

{ "rules_search_and_replace_values_in_some_user_account_fields" : {
    "LABEL" : "Search and replace values in some user account fields",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "user" : { "label" : "User", "type" : "user" } },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "user" ], "field" : "field_accept_boolean" } },
      { "data_is" : { "data" : [ "user:field-accept-boolean" ], "value" : "1" } }
    ],
    "DO" : [
      { "data_set" : { "data" : [ "user:field-accept-list" ], "value" : "yes" } },
      { "drupal_message" : { "message" : "Boolean field converted to list field for user with id \u0022[user:uid]\u0022 and name \u0022[user:name]\u0022." } }
    ]
  }
}

Some more details to further explain the above above example:

  • This Rules Component will be used in the subsequent step.
  • In my case I have a boolean field with machine name field_accept_boolean (values true or false, of course) and a list field with machine name field_accept_list (values yes, no, later).
  • If field_accept_boolean = true, then the value of the field with machine name field_accept_list will be set to yes.

Note that I also added the Rules Condition Entity has field to make that field available for further processing in this Rules Component. That's also what is mentioned in the Rules UI when you're adding a "data comparison" condition: somewhere it says "The data selector helps you drill down into the data available to Rules. To make entity fields appear in the data selector, you may have to use the condition 'entity has field' (or 'content is of type')". For a video tutorial that illustrates the importance of this Entity has field condition, refer to Data types and data selection, especially what is shown between about 13:30 and 17:30 in that video.

If you want, just grab (copy) the exported rule above, change all occurrences of the machine name of the fields to match the machine name of your fields, and then you can simply import this Rules Component in your own environment.

After you did this, you can navigate to admin/config/workflow/rules/components in your own site, and then use the hyperlink "execute" to qa-test it in your own site. After you use that link, use the button that says Switch to direct input mode. Then you'll be able to enter some User identifier and hit the Execute mode. Depending on the user id you selected (if it has that field, and if the Rules Condition is satisfied), your user will be updated.

After you're confident your rule works perfect for all variations of your users, move on to the next step below.

Step 2: Let VBO to the bulk of the work

This step is pretty easy ... Just create an appropriate view of all your users to be processed, and transform it into a Views Bulk Operations view. The clue to what you want VBO to do as an operation is ... you guessed it: Execute the Rules Component from Step 1.

If you're not familiar with how to do so, have a look at the video about Using Rules components with VBO". If you want even more, then also watch the subsequent "Using Rules components with more than one parameter" video.

Of course there are multiple variations of the above approach, like narrowing down your Views output to only "a" specific value, etc. But that should be straight forward, and up to your own imagination.

Attention: to process many users at once is quite a challenge (memory requirements, etc). Therefor you might want to also watch the video about "More details about VBO settings" ... Before you are tempted to use VBO for processing them all at once ...

Step 3: 2nd iteration

With the previous Step 1 and Step 2, all users who have field_accept_boolean = true will have their field_accept_list will be set to yes.

If appropriate, just adapt your rule in Step 1 (or clone it to a 2nd Rules component) so that all users who have field_accept_boolean = false will have their field_accept_list will be set to no. Then repeat Step 2 (with this updated Rules component).

After doing so, ask yourself if there could be any users with a value for that field_accept_boolean which was never assigned either value. For those users, you may want to use yet another variation of Step 1 and Step 2, to have their field_accept_list will be set to later.

Of course, you could have multiple fields similar to field_accept_boolean, which you might either migrate to the same field_accept_list (maybe with another value?), or a yet another field. For all thee variations it's just a matter of tuning the Rules component in Step 1, and then performing Step 2 again.

Step 4: Straight forward variations

There are dozens (hundreds?) of straight forward variations of this question that can also be solved using the above combination of VBO and Rules. To just mention 1 of them, have a look at my answer to "How to bulk delete selected content/nodes?" ...

Happy VBO-ing while Rules-ing!

  • WOW! Best answer ever. That did it. Thank you so much. – Danny Browne Nov 21 '16 at 21:45
  • @DannyBrowne : merci (thank you) for the kudos. Maybe you want to consider marking this answer as "accepted" also? Similar to what is explained here ... – Pierre.Vriens Nov 21 '16 at 21:51
0

You can use "Feeds" -> https://www.drupal.org/project/feeds to export all the data you need and import it again in the structure you want.

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.