You are here

protected function CommerceEntityViewsData::addBundleFieldData in Commerce Core 8.2

Adds views data for the given bundle field.

Based on views_field_default_views_data(), which is only invoked for configurable fields.

Assumes that the bundle field is not shared between bundles, since the bundle plugin API doesn't support that.

Parameters

array $data: The views data.

\Drupal\entity\BundleFieldDefinition $bundle_field: The bundle field.

1 call to CommerceEntityViewsData::addBundleFieldData()
CommerceEntityViewsData::getViewsData in src/CommerceEntityViewsData.php
Returns views data for the entity type.

File

src/CommerceEntityViewsData.php, line 126

Class

CommerceEntityViewsData
Provides improvements to core's generic views integration for entities.

Namespace

Drupal\commerce

Code

protected function addBundleFieldData(array &$data, BundleFieldDefinition $bundle_field) {
  $field_name = $bundle_field
    ->getName();
  $entity_type_id = $this->entityType
    ->id();
  $base_table = $this
    ->getViewsTableForEntityType($this->entityType);
  $revision_table = '';
  if ($this->entityType
    ->isRevisionable()) {
    $revision_table = $this->tableMapping
      ->getRevisionDataTable();
    if (!$revision_table) {
      $revision_table = $this->tableMapping
        ->getRevisionTable();
    }
  }
  $field_tables = [];
  $field_tables[EntityStorageInterface::FIELD_LOAD_CURRENT] = [
    'table' => $this->tableMapping
      ->getDedicatedDataTableName($bundle_field),
    'alias' => "{$entity_type_id}__{$field_name}",
  ];
  if ($this->entityType
    ->isRevisionable()) {
    $field_tables[EntityStorageInterface::FIELD_LOAD_REVISION] = [
      'table' => $this->tableMapping
        ->getDedicatedRevisionTableName($bundle_field),
      'alias' => "{$entity_type_id}_revision__{$field_name}",
    ];
  }
  $table_alias = $field_tables[EntityStorageInterface::FIELD_LOAD_CURRENT]['alias'];
  $data[$table_alias]['table']['group'] = $this->entityType
    ->getLabel();
  $data[$table_alias]['table']['join'][$base_table] = [
    'table' => $this->tableMapping
      ->getDedicatedDataTableName($bundle_field),
    'left_field' => $this->entityType
      ->getKey('id'),
    'field' => 'entity_id',
    'extra' => [
      [
        'field' => 'deleted',
        'value' => 0,
        'numeric' => TRUE,
      ],
    ],
  ];
  if ($bundle_field
    ->isTranslatable()) {
    $data[$table_alias]['table']['join'][$base_table]['extra'][] = [
      'left_field' => 'langcode',
      'field' => 'langcode',
    ];
  }
  if ($this->entityType
    ->isRevisionable()) {
    $table_alias = $field_tables[EntityStorageInterface::FIELD_LOAD_REVISION]['alias'];
    $data[$table_alias]['table']['group'] = $this
      ->t('@group (historical data)', [
      '@group' => $this->entityType
        ->getLabel(),
    ]);
    $data[$table_alias]['table']['join'][$revision_table] = [
      'table' => $this->tableMapping
        ->getDedicatedRevisionTableName($bundle_field),
      'left_field' => $this->entityType
        ->getKey('revision'),
      'field' => 'revision_id',
      'extra' => [
        [
          'field' => 'deleted',
          'value' => 0,
          'numeric' => TRUE,
        ],
      ],
    ];
    if ($bundle_field
      ->isTranslatable()) {
      $data[$table_alias]['table']['join'][$revision_table]['extra'][] = [
        'left_field' => 'langcode',
        'field' => 'langcode',
      ];
    }
  }
  foreach ($field_tables as $type => $table_info) {
    $table_alias = $table_info['alias'];
    $data[$table_alias]['table']['title'] = $bundle_field
      ->getLabel();
    $data[$table_alias]['table']['help'] = $bundle_field
      ->getDescription();
    $data[$table_alias]['table']['entity type'] = $this->entityType
      ->id();
    $data[$table_alias]['table']['provider'] = $this->entityType
      ->getProvider();
    $this
      ->mapFieldDefinition($table_info['table'], $field_name, $bundle_field, $this->tableMapping, $data[$table_alias]);
  }
}