Field Value Copier
Component ID
Component name
Component type
Maintenance status
Development status
Component security advisory coverage
Component created
Component changed
Component body
This module exposes an API that makes it easier to migrate the values of an existing field in one entity to a field of the same type on a different entity.
For example, if you are moving a field from a Product Display (of type "node") to a Product Variation (of type "commerce_product"), you would first setup the product variation with an instance of the field, then use this module to copy the values of each existing product display to its corresponding product variation. Once complete, you can then remove the field from the Product Display.
By default, Field Value Copier (FVC) ships with ReferenceFieldEntityMapper – an EntityMapper that associates a source (i.e. the entity that has the existing values to migrate) to a destination (i.e. the entity that needs to receive the existing values) via an entity reference field between the two entities. When using FVC with Drupal Commerce, the entity reference field used is typically the field in the product display that refers to its corresponding product.
As long as there is a one-to-one relationship between two existing entities via an entity reference field (or product reference field), you can use the ReferenceFieldEntityMapper to move field values between the two entities. It does not matter which direction the reference field is pointing (i.e. if the product display references the product, you can still use that reference field to move values from the product to the product display). The entities must already exist – the purpose of this mapper is simply to shift data from an existing entity to its corresponding existing entity.
Here's a sample of how you'd use this module:
use Drupalfield_value_copierFieldValueCopier;
use Drupalfield_value_copierEntityMappingReferenceFieldEntityMapper;
$copyMachine = new FieldValueCopier();
// We're going to move the product image from the product display to the product
//
// In this case, both entities are using the same field base, so we're using
// setFieldName() instead of setSrcFieldName() and setDstFieldName().
$copyMachine->setFieldName('field_product_image');
$copyMachine->setSrcEntityType('node');
$copyMachine->setSrcEntityBundle('product');
$copyMachine->setDstEntityType('commerce_product');
$copyMachine->setDstEntityBundle('product');
$mapper =
new ReferenceFieldEntityMapper(
'field_product',
ReferenceFieldEntityMapper::SRC_OWNS_REF);
$copyMachine->setEntityMapper($mapper);
// Sample: Copy values for only the first two entities.
// This illustrates some of the methods available to you, in case you want to
// apply a similar pattern to invoke this through Batch API.
$sourceIds = $copyMachine->getAllSourceIds();
$sampleBatch = array_slice($sourceIds, 0, 2);
// $results will summarize the results of the operation
$results = $copyMachine->copyValues($sampleBatch);
If your project requires you to handle relationships other than 1:1, or if you need to be able to create objects on the fly, you can supply your own class that implements EntityMapper.
Alternatively, instead of using this module, you might be able to use Migrate API instead. Migrate may be able to copy field values between entities using a PDO source and PDO destination. YMMV – FVC just provides a simpler and more straightforward API for the specific use case of moving field values between entities.
