7

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?

0

3 Answers 3

10

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.

3
  • 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? Commented Sep 10, 2018 at 18:04
  • 1
    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
    Commented Sep 11, 2018 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. Commented Feb 15, 2019 at 15:06
1

For those looking for an update hook solution, the following example in the entity_test module in core works well. Note that you must do the retrieve, delete, insert method of moving data around if you have any existing data in there.

https://git.drupalcode.org/project/drupal/-/blob/8.8.x/core/modules/system/tests/modules/entity_test/update/entity_definition_updates_8001.inc#L13

0

The Field Config Cardinality module can help in resolving the issue.

[It] Allows the cardinality of specific config[uration] fields to be overridden [by the] cardinality of [the] Storage field.

0

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.