You are here

public function CommerceSmartImporerService::formatField in Commerce Smart Importer 8

Formats one field value based on field settings.

1 call to CommerceSmartImporerService::formatField()
CommerceSmartImporerService::formatMultipleFieldValues in src/Plugin/CommerceSmartImporerService.php
Helper function for createNewProduct.

File

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

Class

CommerceSmartImporerService
This is main Commerce Smart Importer Service.

Namespace

Drupal\commerce_smart_importer\Plugin

Code

public function formatField($field, $product, $folders, $create_mode) {
  $exception = FALSE;
  $product = trim($product);
  $create = '';
  switch (trim($field['field_types'])) {
    case 'text':
    case 'string':
      try {
        $create = $this
          ->createString($product, $field['field_settings']);
        if ($field['machine_names'] == 'sku') {
          $create = $this
            ->createSku($create, $field['field_settings']);
        }
      } catch (Exception $e) {
        $create = $e
          ->getMessage();
        $exception = TRUE;
      }
      break;
    case 'text_long':
    case 'string_long':
    case 'text_with_summary':
      try {
        $create = $this
          ->createTextSummary($product, $field['field_settings']);
      } catch (Exception $e) {
        $create = $e
          ->getMessage();
        $exception = TRUE;
      }
      break;
    case 'integer':
      try {
        $create = $this
          ->createInteger($product, $field['field_settings']);
      } catch (Exception $e) {
        $create = $e
          ->getMessage();
        $exception = TRUE;
      }
      break;
    case 'float':
    case 'decimal':
      try {
        $create = $this
          ->createDecimal($product, $field['field_settings']);
      } catch (Exception $e) {
        $create = $e
          ->getMessage();
        $exception = TRUE;
      }
      break;
    case 'image':
    case 'file':
      try {
        $create = $this
          ->createFile($product, $field['field_settings'], $folders, $create_mode, $field['field_types']);
      } catch (Exception $e) {
        $create = $e
          ->getMessage();
        $exception = TRUE;
      }
      break;
    case 'commerce_price':
      $product = explode(' ', $product);
      if (!array_key_exists(1, $product)) {
        if (is_numeric($product[0])) {
          throw new Exception('No currency');
        }
        else {
          throw new Exception('No price');
        }
      }
      if (is_numeric(trim($product[0]))) {
        if (trim($product[0]) == 0) {
          $create = '';
        }
        else {
          $create = new Price(trim($product[0]), trim($product[1]));
        }
      }
      else {
        $create = 'Price for one variation of ' . $field['label'] . ' is not number!';
        $exception = TRUE;
      }
      break;
    case 'list_float':
    case 'list_string':
    case 'list_integer':
      try {
        $create = $this
          ->createList($product, $field['field_settings']);
      } catch (Exception $e) {
        $create = $e
          ->getMessage();
        $exception = TRUE;
      }
      break;
    case 'timestamp':
      try {
        $create = $this
          ->createTimestamp($product, $field['field_settings']);
      } catch (Exception $e) {
        $create = $e
          ->getMessage();
        $exception = TRUE;
      }
      break;
    case 'email':
      try {
        $create = $this
          ->createEmail($product, $field['field_settings']);
      } catch (Exception $e) {
        $create = $e
          ->getMessage();
        $exception = TRUE;
      }
      break;
    case 'link':
      try {
        $create = $this
          ->createUrl($product, $field['field_settings']);
      } catch (Exception $e) {
        $create = $e
          ->getMessage();
        $exception = TRUE;
      }
      break;
    case 'path':
      try {
        $create = $this
          ->createPath($product, $field['field_settings']);
      } catch (Exception $e) {
        $create = $e
          ->getMessage();
        $exception = TRUE;
      }
      break;
    case 'boolean':
      try {
        $create = $this
          ->createBool($product, $field['field_settings']);
      } catch (Exception $e) {
        $create = $e
          ->getMessage();
        $exception = TRUE;
      }
      break;
    case 'physical_dimensions':
      try {
        $create = $this
          ->createPhysicalDimension($product, $field['field_settings']);
      } catch (Exception $e) {
        $create = $e
          ->getMessage();
        $exception = TRUE;
      }
      break;
    case 'physical_measurement':
      switch ($field['field_settings']['measurement_type']) {
        case 'weight':
          try {
            $create = $this
              ->createPhysicalMesurementWeight($product, $field['field_settings']);
          } catch (Exception $e) {
            $create = $e
              ->getMessage();
            $exception = TRUE;
          }
          break;
        case 'area':
          try {
            $create = $this
              ->createPhysicalMesurementArea($product, $field['field_settings']);
          } catch (Exception $e) {
            $create = $e
              ->getMessage();
            $exception = TRUE;
          }
          break;
        case 'length':
          try {
            $create = $this
              ->createPhysicalMesurementLength($product, $field['field_settings']);
          } catch (Exception $e) {
            $create = $e
              ->getMessage();
            $exception = TRUE;
          }
          break;
        case 'volume':
          try {
            $create = $this
              ->createPhysicalMesurementVolume($product, $field['field_settings']);
          } catch (Exception $e) {
            $create = $e
              ->getMessage();
            $exception = TRUE;
          }
          break;
      }
      break;
    case 'entity_reference':
      if ($field['machine_names'] != 'variations') {
        switch ($field['field_settings']['target_type']) {
          case 'taxonomy_term':
            try {
              $create = $this
                ->createTaxonomyTerm($product, reset($field['field_settings']['handler_settings']['target_bundles']), $create_mode);
            } catch (Exception $e) {
              $create = $e
                ->getMessage();
              $exception = TRUE;
            }
            break;
          case 'commerce_product_attribute_value':
            try {
              $create = $this
                ->createAttribute($product, reset($field['field_settings']['handler_settings']['target_bundles']), $create_mode);
            } catch (Exception $e) {
              $create = $e
                ->getMessage();
              $exception = TRUE;
            }
            break;
          case 'commerce_store':
            try {
              $create = $this
                ->getStore($product);
            } catch (Exception $e) {
              $create = $e
                ->getMessage();
              $exception = TRUE;
            }
            break;
        }
      }
      break;
    default:
      throw new Exception("This field type is not supported!");
  }
  if ($exception) {
    throw new Exception($create);
  }
  else {
    return $create;
  }
}