You are here

public function CommerceSmartImporerService::getFieldDefinition in Commerce Smart Importer 8

Formats field definition for given product and variation type.

File

src/Plugin/CommerceSmartImporerService.php, line 985
Main Commerce Smart Importer Service.

Class

CommerceSmartImporerService
This is main Commerce Smart Importer Service.

Namespace

Drupal\commerce_smart_importer\Plugin

Code

public function getFieldDefinition($with_identifiers = FALSE) {
  $excluded_fields = $this
    ->getExcludedFieldNames($with_identifiers);
  $config = $this
    ->getConfig();
  $product_field_definitions = $this->entityFieldManager
    ->getFieldDefinitions('commerce_product', $config['commerce_product_bundle']);
  $fields['product'] = [];
  foreach ($product_field_definitions as $field_name => $product_field_definition) {
    if (!in_array($field_name, $excluded_fields)) {
      $fields['product'][] = $this
        ->formatFieldDefinition($product_field_definition, $field_name);
    }
  }
  $variation_field_definitions = $this->entityFieldManager
    ->getFieldDefinitions('commerce_product_variation', $config['commerce_product_variation_bundle']);
  $fields['variation'] = [];
  foreach ($variation_field_definitions as $field_name => $variation_field_definition) {
    if (!in_array($field_name, $excluded_fields) && $field_name != 'title' && $field_name != 'product_id') {
      if ($field_name == 'price') {
        $price = $this
          ->formatFieldDefinition($variation_field_definition, $field_name);
      }
      else {
        $fields['variation'][] = $this
          ->formatFieldDefinition($variation_field_definition, $field_name);
      }
    }
  }
  if (isset($price)) {
    $fields['variation'][] = $price;
  }
  else {
    throw new Exception("Missing price object in variation");
  }
  $help_fields['label'] = 'Currency';
  $help_fields['machine_names'] = 'currency';
  $help_fields['field_types'] = 'currency';
  $help_fields['required'] = TRUE;
  $help_fields['cardinality'] = 1;
  $fields['variation'][] = $help_fields;
  if ($this
    ->moduleHandler()
    ->moduleExists('redirect')) {
    $help_fields['label'] = 'Redirection';
    $help_fields['machine_names'] = 'redirection';
    $help_fields['field_types'] = 'redirection';
    $help_fields['required'] = FALSE;
    $help_fields['cardinality'] = -1;
    $help_fields['field_settings']['read-only'] = FALSE;
    $fields['product'][] = $help_fields;
  }
  $this
    ->renameDuplicateFieldDefinitions($fields);
  return $fields;
}