You are here

public function CommerceSmartImporerService::getReferencedTaxonomyTerms in Commerce Smart Importer 8

Gets all Terms from one vocabulary.

File

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

Class

CommerceSmartImporerService
This is main Commerce Smart Importer Service.

Namespace

Drupal\commerce_smart_importer\Plugin

Code

public function getReferencedTaxonomyTerms($entity = 'all', $type = 'default') {
  $commerce_product_variation_fields = $this->entityFieldManager
    ->getFieldDefinitions('commerce_product_variation', $type);
  $commerce_product_fields = $this->entityFieldManager
    ->getFieldDefinitions('commerce_product', $type);
  $entity_fields = [];
  if ($entity == 'all') {
    $entity_fields = array_merge($commerce_product_fields, $commerce_product_variation_fields);
  }
  elseif ($entity == 'variation') {
    $entity_fields = $commerce_product_variation_fields;
  }
  elseif ($entity == 'product') {
    $entity_fields = $commerce_product_fields;
  }
  $taxonomies = [];
  foreach ($entity_fields as $key => $entity_field) {
    if ($entity_field
      ->getType() == 'entity_reference') {
      if ($entity_field
        ->getSettings()['target_type'] == 'taxonomy_term') {
        $field = [];
        if ($entity_field
          ->getLabel() instanceof TranslatableMarkup) {
          $field['name'] = $entity_field
            ->getLabel()
            ->getUntranslatedString();
        }
        else {
          $field['name'] = is_object($entity_field
            ->getLabel()) ? current($entity_field
            ->getLabel()) : $entity_field
            ->getLabel();
        }
        $field['machine_name'] = $key;
        $field['target_bundles'] = $entity_field
          ->getSettings()['handler_settings']['target_bundles'];
        $taxonomies[] = $field;
      }
    }
  }
  return $taxonomies;
}