1

Is there a way to make a change to the value of a multi-select field, leaving the labels intact, and update the field across all the entries while preserving the current selections?
I have a multi-select field used to denote where an entry should exist on a multi-site from a setup that predates Crafts native support of multi-site. The values of each option contains the handle used for the old multi-site(i.e. siteUs, siteCn, siteSg, siteAu etc). The new site handles( siteUS, siteCN, siteSG, siteASTL) have a slightly different naming pattern. The ask was to propagate entries to the new multi-sites based on this fields values.

1 Answer 1

2

Select fields store selected options by their value, so changing the values will result in data loss the next time the entries are saved. You can solve this with migrations.

One option is to change the values of the multi-select field, then write a migration that will replace the old values with the new ones in the database. If I recall correctly, multi-select fields store the selected options as JSON strings, so you'll probably need to iterate through all rows, decode the JSON, replace the old values with the new ones, encode the JSON again and save it back to the database.

If this doesn't work for any reason, you can also do this in two steps:

  1. Rename your existing field and append something like _OLD to the handle (i.e. activeSites_OLD) and use field conditions to hide it for all users except admins. Create a new field with the new option values and add it to all field layouts where the old fields are in use. Update your templates and modules to work with the new field values. Then write a migration that iterates through all entries and saves the correct values to the new field based on a mapping of the values from the old field. In the production environment, run this migration immediately after applying the project config (which creates the new field). This should happen automatically as part of your deployment if you follow the recommended deployment best practices.
  2. After you've checked that the migration succeeded and all entries have the correct data in the new fields, you can safely remove the old field from all field layouts and delete it.

This two-step approach is a bit safer than manually modifying the database, since you can write your migration using high-level APIs, and the old data is still there in case anything goes wrong with the migration. Though, you should always create a full backup before deploying any changes to the production environment.


As a side note, Craft recently added an option for sections to manually select which sites an entry should be propagated to. This might be a better solution than a custom multi-select field, make sure to test if this works for your use-case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.