You are here

protected function ProductAttributeFieldManager::buildFieldMap in Commerce Core 8.2

Builds the field map.

Return value

array The built field map.

1 call to ProductAttributeFieldManager::buildFieldMap()
ProductAttributeFieldManager::getFieldMap in modules/product/src/ProductAttributeFieldManager.php
Gets a map of attribute fields across variation types.

File

modules/product/src/ProductAttributeFieldManager.php, line 136

Class

ProductAttributeFieldManager
Default implementation of the ProductAttributeFieldManagerInterface.

Namespace

Drupal\commerce_product

Code

protected function buildFieldMap() {
  $field_map = [];
  $bundle_info = $this->entityTypeBundleInfo
    ->getBundleInfo('commerce_product_variation');
  foreach (array_keys($bundle_info) as $bundle) {

    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
    $form_display = commerce_get_entity_display('commerce_product_variation', $bundle, 'form');
    foreach ($this
      ->getFieldDefinitions($bundle) as $field_name => $definition) {
      $handler_settings = $definition
        ->getSetting('handler_settings');
      $component = $form_display
        ->getComponent($field_name);
      $field_map[$bundle][] = [
        'attribute_id' => reset($handler_settings['target_bundles']),
        'field_name' => $field_name,
        'weight' => $component ? $component['weight'] : 0,
      ];
    }
    if (!empty($field_map[$bundle])) {
      uasort($field_map[$bundle], [
        '\\Drupal\\Component\\Utility\\SortArray',
        'sortByWeightElement',
      ]);

      // Remove the weight keys to reduce the size of the cached field map.
      $field_map[$bundle] = array_map(function ($map) {
        return array_diff_key($map, [
          'weight' => '',
        ]);
      }, $field_map[$bundle]);
    }
  }
  return $field_map;
}