Hi all, when I first began using this module, I had a single content type that could be referenced to 4 other content types. To do this, I foolishly created an entity reference field on the single content type for each of the four types that could be merged, and now I am realizing that I didn't need to do that. And, in fact, there are very good reasons that I should NOT have.

So, I am interested in merging these entity refs into a single field. Does anyone have an idea on how to do this?

Thanks in advance,
r.b.

Comments

Status:Active» Needs review

As so often happens, once a question is given voice, it's solution becomes apparent to the asker. I managed to do this at the SQL command line. Since entity_references are fairly simple data constructs (as far as I can tell), I was able to do this somewhat simply. If anyone wanted to review my methodology, that would be very cool. I use postgreSQL.

http://sifn.bse.vt.edu/sifnwiki/index.php/Drupal_merge_entity_reference

Initial State:
* "Water Withdrawal" node type with 3 entity reference columns, one each for "CWS" (field_gw_cws_link), "Ag Users" (field_src_agr_link) and "SSU > 300k" (field_data_field_gw_ssugt300_link).

Steps:
1. Create the destination entity link type (note the name of the new field "field_withdrawal_parent")
2. Copy Old Refs to New
2a. Drupal parlance sees the name of the field, in this case "field_withdrawal_parent", stored in the Drupal database as "field_data_field_withdrawal_parent"
2b. Dump data from all source tables for individual entity reference fields into the single entity reference field (see Query 1a-c)

Query 1a-c:

-- 1a. AGR
insert into field_data_field_withdrawal_parent ( entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_withdrawal_parent_target_id)
select entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_src_agr_link_target_id
from field_data_field_src_agr_link;
-- 1b. SSU > 300k
insert into field_data_field_withdrawal_parent ( entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_withdrawal_parent_target_id)
select entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_gw_ssugt300_link_target_id
from field_data_field_gw_ssugt300_link;
-- 1c. CWS
insert into field_data_field_withdrawal_parent ( entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_withdrawal_parent_target_id)
select entity_type, bundle, deleted, entity_id, revision_id, language, delta,
field_gw_cws_link_target_id
from field_data_field_gw_cws_link;

r.b.