You are here

private function CommerceSmartImporerService::formatFieldDefinition in Commerce Smart Importer 8

Helper function for getFieldDefinition, formats one field.

See also

getFieldDefinition()

1 call to CommerceSmartImporerService::formatFieldDefinition()
CommerceSmartImporerService::getFieldDefinition in src/Plugin/CommerceSmartImporerService.php
Formats field definition for given product and variation type.

File

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

Class

CommerceSmartImporerService
This is main Commerce Smart Importer Service.

Namespace

Drupal\commerce_smart_importer\Plugin

Code

private function formatFieldDefinition($field_definition, $machine_name) {
  $field = [];
  if ($field_definition
    ->getLabel() instanceof TranslatableMarkup) {
    $label = $field_definition
      ->getLabel()
      ->getUntranslatedString();
  }
  else {
    $label = is_object($field_definition
      ->getLabel()) ? current($field_definition
      ->getLabel()) : $field_definition
      ->getLabel();
  }
  switch ($machine_name) {
    case 'product_id':
      $field['label'] = 'ID(product)';
      break;
    case 'variation_id':
      $field['label'] = 'ID(variation)';
      break;
    default:
      $field['label'] = $label;
  }
  $field['machine_names'] = $machine_name;
  $field['field_types'] = $field_definition
    ->getType();
  $field['field_settings'] = $field_definition
    ->getSettings();
  if ($field_definition instanceof FieldConfig) {
    $fieldStorage = $field_definition
      ->getFieldStorageDefinition();
    $field['required'] = $field_definition
      ->get('required');
    $field['cardinality'] = $fieldStorage
      ->get('cardinality');
    $field['field_settings']['default_value'] = $field_definition
      ->get('default_value');
    $field['field_settings']['default_value'] = is_array($field['field_settings']['default_value']) ? current($field['field_settings']['default_value']) : $field['field_settings']['default_value'];
  }
  elseif ($field_definition instanceof BaseFieldDefinition) {
    $field['cardinality'] = $field_definition
      ->getCardinality();
    $field['required'] = $field_definition
      ->isRequired();
    if ($machine_name == 'sku') {
      $field['field_settings']['default_value'] = 'generateSKU';
    }
    elseif (!isset($field['field_settings']['default_value'])) {
      if ($field_definition
        ->getDefaultValueCallback() == NULL) {
        $field['field_settings']['default_value'] = FALSE;
      }
    }
  }
  $identifiers = $this
    ->getIdentifierFields();
  if (in_array($machine_name, $identifiers['product']) || in_array($machine_name, $identifiers['variation'])) {
    $field['field_settings']['read-only'] = TRUE;
  }
  else {
    $field['field_settings']['read-only'] = FALSE;
  }
  return $field;
}