3

I have two content types: Person and Office with existing content. The Person content type has an entity reference field to Office nodes, which only allows a single reference. I need to change this to allow an unlimited number of Office nodes to be selected.

Drupal 8 prevents changing the cardinality of the field, and it gives the following error message.

There is data for this field in the database. The field settings can no longer be changed.

At first, I thought I needed to write a migration, but they seem to be aimed at bringing data into a Drupal instance. Then, I read about writing update functions (also this documentation).

I discovered a migrate-plus plugin that adds a couple pages me to export the field storage settings, change the cardinality, and re-import the settings.
Given that I'm going from cardinality of 1 to unlimited, I could imagine this change wouldn't destroy data. And my empirical data suggests this is the case. I've now made this change via the migrate-plus screens as well as via configuration export and import and the data is still in the field.

Is this a safe operation, or should I write an update function?

6

There is data for this field in the database. The field settings can no longer be changed.

That message is misleading, it will actually not limit the ability to increase the cardinality. The only limitation is that it won't allow you to set the cardinality to a value lower than the highest amount of items you currently have.

  • Thanks Berdir! That answers my short term question. For the future, if I did need to reduce cardinality, would I write a migration or an update hook? – Jed Anderson Sep 10 '18 at 18:04
  • For the same site more likely an update hook, in-site-migration isn't really a thing yet. But the only thing that update hook could do is delete the additional values that are above the new limit that you want to set if it's too much to do it manually. – Berdir Sep 11 '18 at 8:36
  • This is great information. I always took that message literally and never tried to change it. One clarification: I initially interpreted Berdir's answer as meaning you could raise the cardinality but not lower it, but in fact he means you can't set it lower than the highest number of values you currently have in the database. So by finding the two pieces of content I had that were above my desired limit and deleting their extra items, I was then able to change the field cardinality from Unlimited to 1 through the UI — no migration or update hooks required. – Kelley Curry Feb 15 at 15:06

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.