You are here

protected function CivicrmEntityViewsData::processViewsDataForCustomFields in CiviCRM Entity 8.3

Add views integration for custom fields.

Parameters

array $views_field: Array of fields from ::getViewsData().

array $field_metadata: An array of field metadata.

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

File

src/CivicrmEntityViewsData.php, line 254

Class

CivicrmEntityViewsData

Namespace

Drupal\civicrm_entity

Code

protected function processViewsDataForCustomFields(array &$views_field, array $field_metadata) {
  $field_metadata = [
    'pseudoconstant' => $field_metadata['option_group_id'] ?? NULL,
    'entity_type' => SupportedEntities::getEntityType($field_metadata['extends']),
    'name' => "custom_{$field_metadata['id']}",
  ] + $field_metadata;
  $views_field[$field_metadata['table_name']]['table'] = [
    'group' => $this
      ->t('CiviCRM custom: @title', [
      '@title' => $field_metadata['title'],
    ]),
    'entity type' => $field_metadata['entity_type'],
    'entity revision' => FALSE,
    // Add automatic relationships so that custom fields from CiviCRM entities
    // are included when they are the base tables.
    'join' => [
      $field_metadata['entity_type'] => [
        'left_field' => 'id',
        'field' => 'entity_id',
      ],
    ],
  ];
  $views_field[$field_metadata['table_name']][$field_metadata['column_name']] = [
    'title' => $field_metadata['label'],
    'help' => $this
      ->t('@message', [
      '@message' => empty($field_metadata['help_post']) ? 'Custom data field.' : $field_metadata['help_post'],
    ]),
    'field' => $this
      ->getViewsFieldPlugin($field_metadata),
    'argument' => $this
      ->getViewsArgumentPlugin($field_metadata),
    'filter' => $this
      ->getViewsFilterPlugin($field_metadata),
    'sort' => $this
      ->getViewsSortPlugin($field_metadata),
    'relationship' => $this
      ->getViewsRelationshipPlugin($field_metadata),
    'entity field' => $field_metadata['name'],
  ];
}