You are here

protected function CommerceEntityViewsData::mapFieldDefinition in Commerce Core 8.2

Puts the views data for a single field onto the views data.

Parameters

string $table: The table of the field to handle.

string $field_name: The name of the field to handle.

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition defined in Entity::baseFieldDefinitions()

\Drupal\Core\Entity\Sql\TableMappingInterface $table_mapping: The table mapping information

array $table_data: A reference to a specific entity table (for example data_table) inside the views data.

Overrides EntityViewsData::mapFieldDefinition

1 call to CommerceEntityViewsData::mapFieldDefinition()
CommerceEntityViewsData::addBundleFieldData in src/CommerceEntityViewsData.php
Adds views data for the given bundle field.

File

src/CommerceEntityViewsData.php, line 202

Class

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

Namespace

Drupal\commerce

Code

protected function mapFieldDefinition($table, $field_name, FieldDefinitionInterface $field_definition, TableMappingInterface $table_mapping, &$table_data) {
  $field_column_mapping = $table_mapping
    ->getColumnNames($field_name);
  $field_storage = $this
    ->getFieldStorageDefinitions()[$field_name];
  $field_schema = $field_storage
    ->getSchema();
  $field_definition_type = $field_definition
    ->getType();

  // Add all properties to views table data. We need an entry for each
  // column of each field, with the main one given special treatment.
  $main_property = $field_storage
    ->getMainPropertyName();
  if (!$main_property) {

    // The mapSingleFieldViewsData() method always expects a main property,
    // so there must be a fallback to the first defined property.
    // See #2337517 for the related core issue.
    $property_names = array_keys($field_column_mapping);
    $main_property = reset($property_names);
  }
  foreach ($field_column_mapping as $field_column_name => $schema_field_name) {
    $first = $main_property == $field_column_name;
    $table_data[$schema_field_name] = $this
      ->mapSingleFieldViewsData($table, $field_name, $field_definition_type, $field_column_name, $field_schema['columns'][$field_column_name]['type'], $first, $field_definition);
    $table_data[$schema_field_name]['entity field'] = $field_name;

    // By default core makes every property render the entire field, which
    // confuses users. Fix that so only the property itself is rendered.
    // Workaround for core issue #3097636.
    if (!$first && $table_data[$schema_field_name]['field']['id'] == 'field') {
      $table_data[$schema_field_name]['field']['id'] = 'standard';
    }

    // Many handlers (datetime, list) crash without the field_name defined.
    foreach ([
      'argument',
      'filter',
      'sort',
    ] as $handler_type) {
      if (isset($table_data[$schema_field_name][$handler_type])) {
        $table_data[$schema_field_name][$handler_type]['field_name'] = $field_name;
      }
    }
  }

  // Expose additional delta column for multiple value fields.
  // Workaround for core issue #3097568.
  if ($field_storage
    ->isMultiple()) {
    $label = $field_definition
      ->getLabel();
    $table_data['delta'] = [
      'title' => $this
        ->t('@label (@name:delta)', [
        '@label' => $label,
        '@name' => $field_name,
      ]),
      'title short' => t('@label:delta', [
        '@label' => $label,
      ]),
    ];
    $table_data['delta']['field'] = [
      'id' => 'numeric',
    ];
    $table_data['delta']['argument'] = [
      'field' => 'delta',
      'table' => $table,
      'id' => 'numeric',
      'empty field name' => $this
        ->t('- No value -'),
      'field_name' => $field_name,
      'entity_type' => $this->entityType
        ->id(),
    ];
    $table_data['delta']['filter'] = [
      'field' => 'delta',
      'table' => $table,
      'id' => 'numeric',
      'field_name' => $field_name,
      'entity_type' => $this->entityType
        ->id(),
      'allow empty' => TRUE,
    ];
    $table_data['delta']['sort'] = [
      'field' => 'delta',
      'table' => $table,
      'id' => 'standard',
      'field_name' => $field_name,
      'entity_type' => $this->entityType
        ->id(),
    ];
  }
}