You are here

public function BulkVariationsCreator::getAttributeFieldNames in Commerce Bulk 8

Gets the names of the entity's attribute fields.

Parameters

\Drupal\commerce_product\Entity\ProductVariation $variation: The commerce product variation.

Return value

string[] The attribute field names.

Overrides BulkVariationsCreatorInterface::getAttributeFieldNames

1 call to BulkVariationsCreator::getAttributeFieldNames()
BulkVariationsCreator::getAttributeFieldOptionIds in src/BulkVariationsCreator.php
Gets the IDs of the variation's attribute fields.

File

src/BulkVariationsCreator.php, line 479

Class

BulkVariationsCreator
Default implementation of the BulkVariationsCreatorInterface.

Namespace

Drupal\commerce_bulk

Code

public function getAttributeFieldNames(ProductVariation $variation) {
  $attribute_field_manager = \Drupal::service('commerce_product.attribute_field_manager');
  $field_map = $attribute_field_manager
    ->getFieldMap($variation
    ->bundle());
  $attribute_ids = array_column($field_map, 'attribute_id');
  $field_names = array_column($field_map, 'field_name');
  $storage = \Drupal::entityTypeManager()
    ->getStorage('commerce_product_attribute_value');
  $names = $fields = [];
  foreach ($field_names as $index => $name) {
    $fields[$name][] = $attribute_ids[$index];
  }
  foreach ($fields as $field_name => $ids) {
    $values = [];
    foreach ($ids as $attribute_id) {
      foreach ($storage
        ->loadMultipleByAttribute($attribute_id) as $id => $value) {
        $values[$id] = $value
          ->getName();
      }
      $names[$field_name] = $values;
    }
  }
  return $names;
}