You are here

public function EntityViewsData::getViewsData in Entity API 8

Returns views data for the entity type.

Return value

array Views data in the format of hook_views_data().

Overrides EntityViewsData::getViewsData

File

src/EntityViewsData.php, line 44

Class

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

Namespace

Drupal\entity

Code

public function getViewsData() {
  $data = parent::getViewsData();
  $this->tableMapping = $this->storage
    ->getTableMapping();
  $entity_type_id = $this->entityType
    ->id();

  // Workaround for core issue #3004300.
  if ($this->entityType
    ->isRevisionable()) {
    $revision_table = $this->tableMapping
      ->getRevisionTable();
    $data[$revision_table]['table']['entity revision'] = TRUE;
  }

  // Add missing reverse relationships. Workaround for core issue #2706431.
  $base_fields = $this
    ->getEntityFieldManager()
    ->getBaseFieldDefinitions($entity_type_id);
  $entity_reference_fields = array_filter($base_fields, function (BaseFieldDefinition $field) {
    return $field
      ->getType() == 'entity_reference';
  });
  $this
    ->addReverseRelationships($data, $entity_reference_fields);

  // Add views integration for bundle plugin fields.
  // Workaround for core issue #2898635.
  if ($this->entityType
    ->hasHandlerClass('bundle_plugin')) {
    $bundles = $this
      ->getEntityTypeBundleInfo()
      ->getBundleInfo($entity_type_id);
    foreach (array_keys($bundles) as $bundle) {
      $field_definitions = $this
        ->getEntityFieldManager()
        ->getFieldDefinitions($entity_type_id, $bundle);
      foreach ($field_definitions as $field_definition) {
        if ($field_definition instanceof BundleFieldDefinition) {
          $this
            ->addBundleFieldData($data, $field_definition);
        }
      }
    }
  }
  return $data;
}