2

I've got a handful of content types that all use different fields to hold the same type of data. For instance,

Content Type A -- Field Name: field_type_a_user, Field type: Entity Reference

Content Type B -- Field Name: field_type_b_user, Field type: Entity Reference

Content Type C -- Field Name: field_type_c_user, Field type: Entity Reference

I'm building out a view displaying nodes that might reference any of the three of these content types, and the view needs to display information about the user referenced in the node's user field.

It seems inefficient to be using multiple different fields to be storing the exact same type of data. Is there an efficient way to go about creating a new field, field_user, and moving the data from all of these old fields into the new field? And then update all references throughout the site to use this new field?

Or would it be better to just add all three fields to my new view and call it a day?

2

I can't say which is better, because it depends on what customization you intend to do in the future.

That said, generally speaking, if you have different content types that use the same field for the same purpose, you should choose to re-use the field across content types in the design stage; doing so would've saved you the trouble you face now.

How to consolidate multiple fields

First, create a new field on one content type.

Next, re-use the field on all the other content types you want to use it on.

Then, see this question on renaming fields for information about how to use SQL to copy the data from the old fields to the new fields. ALWAYS make a backup before doing this; if you make a mistake in this step, you can break your site.

Now, you will have to fix your views, twig templates, and anything else that might be broken. You can go to your exported config folder and search for the field names of the old fields, but I wouldn't change the config directly; I would use the config folder as a hint of what to change, and then update the values in the UI. I'd do this because sometimes one setting changes another, and if you do it in the UI, everything should get changed properly.

  • Unfortunately I wasn't the one who built the site. I'm just the one fixing / upgrading it, hence trying to approach this headache! Forgot to mention that I'm on D7 but I think those instructions should work fine regardless. – Mrweiner Nov 30 '17 at 21:47

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.