You are here

public function CivicrmEntityViewsData::getViewsData in CiviCRM Entity 8.3

Returns views data for the entity type.

Return value

array Views data in the format of hook_views_data().

Overrides EntityViewsData::getViewsData

File

src/CivicrmEntityViewsData.php, line 48

Class

CivicrmEntityViewsData

Namespace

Drupal\civicrm_entity

Code

public function getViewsData() {
  $data = [];
  $base_table = $this->entityType
    ->getBaseTable() ?: $this->entityType
    ->id();
  $base_field = $this->entityType
    ->getKey('id');

  // Setup base information of the views data.
  $data[$base_table]['table']['group'] = sprintf('%s (CiviCRM Entity)', $this->entityType
    ->getLabel());
  $data[$base_table]['table']['provider'] = $this->entityType
    ->getProvider();
  $data[$base_table]['table']['entity type'] = $this->entityType
    ->id();
  $views_base_table = $base_table;
  $data[$views_base_table]['table']['base'] = [
    'field' => $base_field,
    'title' => $this->entityType
      ->getLabel(),
    'cache_contexts' => $this->entityType
      ->getListCacheContexts(),
  ];
  $data[$base_table]['table']['entity revision'] = FALSE;
  if ($label_key = $this->entityType
    ->getKey('label')) {
    $data[$views_base_table]['table']['base']['defaults'] = [
      'field' => $label_key,
    ];
  }

  // Entity types must implement a list_builder in order to use Views'
  // entity operations field.
  if ($this->entityType
    ->hasListBuilderClass()) {
    $data[$base_table]['operations'] = [
      'field' => [
        'title' => $this
          ->t('Operations links'),
        'help' => $this
          ->t('Provides links to perform entity operations.'),
        'id' => 'entity_operations',
      ],
    ];
  }
  if ($this->entityType
    ->hasViewBuilderClass()) {
    $data[$base_table]['rendered_entity'] = [
      'field' => [
        'title' => $this
          ->t('Rendered entity'),
        'help' => $this
          ->t('Renders an entity in a view mode.'),
        'id' => 'rendered_entity',
      ],
    ];
  }
  $this
    ->addEntityLinks($data[$base_table]);

  // Load all typed data definitions of all fields. This should cover each of
  // the entity base, revision, data tables.
  $field_definitions = $this->entityFieldManager
    ->getBaseFieldDefinitions($this->entityType
    ->id());

  /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
  $table_mapping = $this->storage
    ->getTableMapping();
  if ($table_mapping) {
    foreach ($field_definitions as $field_definition) {
      if ($table_mapping
        ->allowsSharedTableStorage($field_definition
        ->getFieldStorageDefinition())) {
        $this
          ->mapFieldDefinition($views_base_table, $field_definition
          ->getName(), $field_definition, $table_mapping, $data[$views_base_table]);

        // Provide a reverse relationship for the entity type that is referenced by
        // the field.
        if ($field_definition
          ->getType() === 'entity_reference') {
          $target_entity_type_id = $field_definition
            ->getFieldStorageDefinition()
            ->getSetting('target_type');
          $target_entity_type = $this->entityTypeManager
            ->getDefinition($target_entity_type_id);
          assert($target_entity_type !== NULL);
          $target_base_table = $target_entity_type
            ->getDataTable() ?: $target_entity_type
            ->getBaseTable();
          $field_name = $field_definition
            ->getName();
          $pseudo_field_name = 'reverse__' . $this->entityType
            ->id() . '__' . $field_name;
          $args = [
            '@label' => $target_entity_type
              ->getSingularLabel(),
            '@field_name' => $field_name,
            '@entity' => $this->entityType
              ->getLabel(),
          ];
          $data[$target_base_table][$pseudo_field_name]['relationship'] = [
            'title' => $this
              ->t('@entity using @field_name', $args),
            'label' => $this
              ->t('@field_name', [
              '@field_name' => $field_name,
            ]),
            'group' => $target_entity_type
              ->getLabel(),
            'help' => $this
              ->t('Relate each @entity with a @field_name set to the @label.', $args),
            'id' => 'civicrm_entity_reverse',
            'base' => $this->entityType
              ->getDataTable() ?: $this->entityType
              ->getBaseTable(),
            'entity_type' => $this->entityType
              ->id(),
            'base field' => $this->entityType
              ->getKey('id'),
            'field_name' => $field_name,
          ];
        }
      }
      else {
        if ($table_mapping
          ->requiresDedicatedTableStorage($field_definition
          ->getFieldStorageDefinition())) {
          $table = $table_mapping
            ->getDedicatedDataTableName($field_definition
            ->getFieldStorageDefinition());
          $data[$table]['table']['group'] = $this->entityType
            ->getLabel();
          $data[$table]['table']['provider'] = $this->entityType
            ->getProvider();
          $data[$table]['table']['join'][$views_base_table] = [
            'left_field' => $base_field,
            'field' => 'entity_id',
            'extra' => [
              [
                'field' => 'deleted',
                'value' => 0,
                'numeric' => TRUE,
              ],
            ],
          ];
        }
      }
      if (($field_metadata = $field_definition
        ->getSetting('civicrm_entity_field_metadata')) && isset($field_metadata['custom_group_id'])) {
        $this
          ->processViewsDataForCustomFields($data, $field_metadata);

        // Remove the predefined custom field property if we are able to
        // retrieve metadata for the field.
        unset($data[$base_table][$field_definition
          ->getName()]);
      }
    }
  }
  $this
    ->processViewsDataForSpecialFields($data, $base_table);
  return $data;
}