You are here

public function CommerceFieldName::transform in Commerce Migrate 8.2

Same name and namespace in other branches
  1. 3.1.x modules/commerce/src/Plugin/migrate/process/commerce1/CommerceFieldName.php \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\CommerceFieldName::transform()
  2. 3.0.x modules/commerce/src/Plugin/migrate/process/commerce1/CommerceFieldName.php \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\CommerceFieldName::transform()

Performs the associated process.

Parameters

mixed $value: The value to be transformed.

\Drupal\migrate\MigrateExecutableInterface $migrate_executable: The migration in which this process is being executed.

\Drupal\migrate\Row $row: The row from the source to process. Normally, just transforming the value is adequate but very rarely you might need to change two columns at the same time or something like that.

string $destination_property: The destination property currently worked on. This is only used together with the $row above.

Return value

string|array The newly transformed value.

Overrides ProcessPluginBase::transform

File

modules/commerce/src/Plugin/migrate/process/commerce1/CommerceFieldName.php, line 77

Class

CommerceFieldName
Determines the field name.

Namespace

Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  $field_name = $row
    ->getSourceProperty('field_name');
  $entity_type = $row
    ->getSourceProperty('entity_type');
  $type = $row
    ->getSourceProperty('type');

  // Get the commerce attribute style field name.
  if ($entity_type == 'commerce_product' && $type == 'taxonomy_term_reference') {
    $instances = $row
      ->getSourceProperty('instances');

    // If any instance has a select widget, then this is an attribute.
    foreach ($instances as $instance) {
      $data = unserialize($instance['data']);
      if ($data['widget']['type'] == 'options_select') {

        // If the row contains the property 'field_name_attribute' then we
        // need to maintain BC and use that as the attribute field name.
        // Otherwise, make a new name without 'field', make sure it is unique
        // and <= 32 characters.
        if ($row
          ->getDestinationProperty('field_name_attribute')) {
          $field_name = $row
            ->getDestinationProperty('field_name_attribute');
          @trigger_error("Migrating attribute field names with a prefix of 'field_' is deprecated. Re configure the d6_field migration.", E_USER_DEPRECATED);
        }
        else {
          $field_name = preg_replace('/^field_/', 'attribute_', $field_name);

          // The field name may be > 32 characters so make it unique.
          $migration = $this->migrationPluginManager
            ->createStubMigration([]);
          $configuration = [
            'entity_type' => 'commerce_product_attribute',
            'field' => 'field_name',
            'length' => 29,
          ];
          $plugin = $this->processPluginManager
            ->createInstance('make_unique_entity_field', $configuration, $migration);
          $field_name = $plugin
            ->transform($field_name, $migrate_executable, $row, 'tmp');
        }
        break;
      }
    }
  }

  // For profiles the name of the addressfield changes to address.
  if ($entity_type == 'commerce_customer_profile' && $type == 'addressfield') {
    $field_name = 'address';
  }
  return $field_name;
}