Newer
Older
use Drupal\Core\Database\Database;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CivicrmEntityViewsData extends EntityViewsData {
/**
* The CiviCRM API.
*
* @var \Drupal\civicrm_entity\CiviCrmApiInterface
*/
protected $civicrmApi;
/**
* {@inheritdoc}
*/
public function __construct(EntityTypeInterface $entity_type, SqlEntityStorageInterface $storage_controller, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, TranslationInterface $translation_manager, EntityFieldManagerInterface $entity_field_manager = NULL, CiviCrmApiInterface $civicrm_api) {
parent::__construct($entity_type, $storage_controller, $entity_type_manager, $module_handler, $translation_manager, $entity_field_manager);
$this->civicrmApi = $civicrm_api;
$this->civicrmApi->civicrmInitialize();
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('entity_type.manager')->getStorage($entity_type->id()),
$container->get('entity_type.manager'),
$container->get('module_handler'),
$container->get('string_translation'),
$container->get('entity_field.manager'),
$container->get('civicrm_entity.api')
);
}
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(),
// If we're in a test site, use the civicrm_test database connection.
'database' => drupal_valid_test_ua() ? 'civicrm_test' : 'civicrm',
// Specify our own views query plugin, to support ensuring the CiviCRM
// database connection exists in Drupal.
// @see \Drupal\views\Plugin\views\display\DisplayPluginBase::getPlugin
'query_id' => 'civicrm_views_query',
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
];
$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.

Luke Stewart
committed
$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');

Luke Stewart
committed
$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;
'@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 table' => $target_base_table,
'field field' => $field_name,
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
elseif ($field_definition->getType() === 'datetime') {
$arguments = [
'year' => $this->t('Date in the form of YYYY.'),
'month' => $this->t('Date in the form of MM (01 - 12).'),
'day' => $this->t('Date in the form of DD (01 - 31).'),
'week' => $this->t('Date in the form of WW (01 - 53).'),
'year_month' => $this->t('Date in the form of YYYYMM.'),
'full_date' => $this->t('Date in the form of CCYYMMDD.'),
];
foreach ($arguments as $argument_type => $help_text) {
$data[$base_table][$field_definition->getName() . '_' . $argument_type] = [
'title' => t('@label (@argument)', [
'@label' => $field_definition->getLabel(),
'@argument' => $argument_type,
]),
'help' => $help_text,
'argument' => [
'field' => $field_definition->getName(),
'id' => 'datetime_' . $argument_type,
'entity_type' => $field_definition->getTargetEntityTypeId(),
'field_name' => $field_definition->getName(),
],
];
}
}
}
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;
}
public function getViewsTableForEntityType(EntityTypeInterface $entity_type) {
// CiviCRM Entity tables are `civicrm_*`
return $entity_type->id();
}
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
* Provides Views integration for any datetime-based fields.
*
* This does not provide arguments, as that required an alter against the
* entire Views data array, which is not possible here.
*
* @param string $table
* The table the language field is added to.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field definition.
* @param array $views_field
* The views field data.
* @param string $field_column_name
* The field column being processed.
*
* @see datetime_type_field_views_data_helper()
*/
protected function processViewsDataForDatetime($table, FieldDefinitionInterface $field_definition, array &$views_field, $field_column_name) {
// Set the 'datetime' filter type.
$views_field['filter']['id'] = 'datetime';
$views_field['filter']['field_name'] = $field_definition->getName();
// Set the 'datetime' argument type.
$views_field['argument']['id'] = 'datetime';
$views_field['argument']['field_name'] = $field_definition->getName();
// Set the 'datetime' sort handler.
$views_field['sort']['id'] = 'datetime';
$views_field['sort']['field_name'] = $field_definition->getName();
}
/**
* Provides Views integration for list_string fields.
*
* This does not provide arguments, as that required an alter against the
* entire Views data array, which is not possible here.
*
* @param string $table
* The table the language field is added to.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field definition.
* @param array $views_field
* The views field data.
* @param string $field_column_name
* The field column being processed.
*
* @see options_field_views_data()
*/
protected function processViewsDataForListString($table, FieldDefinitionInterface $field_definition, array &$views_field, $field_column_name) {
$views_field['filter']['id'] = 'list_field';
$views_field['filter']['field_name'] = $field_definition->getName();
$views_field['argument']['id'] = 'string_list_field';
$views_field['argument']['field_name'] = $field_definition->getName();
}
/**
* Provides Views integration for list_integer fields.
*
* This does not provide arguments, as that required an alter against the
* entire Views data array, which is not possible here.
*
* @param string $table
* The table the language field is added to.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field definition.
* @param array $views_field
* The views field data.
* @param string $field_column_name
* The field column being processed.
*
* @see options_field_views_data()
*/
protected function processViewsDataForListInteger($table, FieldDefinitionInterface $field_definition, array &$views_field, $field_column_name) {
$views_field['filter']['id'] = 'list_field';
$views_field['filter']['field_name'] = $field_definition->getName();
$views_field['argument']['id'] = 'number_list_field';
$views_field['argument']['field_name'] = $field_definition->getName();
}
/**
* {@inheritdoc}
*/
protected function processViewsDataForEntityReference($table, FieldDefinitionInterface $field_definition, array &$views_field, $field_column_name) {
parent::processViewsDataForEntityReference($table, $field_definition, $views_field, $field_column_name);
if (!empty($field_definition->getItemDefinition()->getSetting('allowed_values_function'))) {
$this->processViewsDataForListString($table, $field_definition, $views_field, $field_column_name);
}
}
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* Add views integration for custom fields.
*
* @param array $views_field
* Array of fields from ::getViewsData().
* @param array $field_metadata
* An array of field metadata.
*/
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'],
];
}
/**
* Add views integration for fields that require special handling.
*
* @param array $views_field
* Array of fields from ::getViewsData().
* @param string $base_table
* The base table, most likely the CiviCRM entity type.
*/
protected function processViewsDataForSpecialFields(array &$views_field, $base_table) {
switch ($base_table) {
case 'civicrm_activity':
$views_field[$base_table]['source_contact_id']['filter'] = [
'id' => 'civicrm_entity_civicrm_activity_contact_record',
];
$views_field[$base_table]['assignee_id']['filter'] = [
'id' => 'civicrm_entity_civicrm_activity_contact_record',
];
$views_field[$base_table]['target_id']['filter'] = [
'id' => 'civicrm_entity_civicrm_activity_contact_record',
];
$views_field[$base_table]['attachments'] = [
'title' => $this->t('Attachments'),
'help' => $this->t('Attachments for the CiviCRM activity.'),
'field' => [
'id' => 'civicrm_entity_activity_attachments',
'field' => 'id',
'click sortable' => FALSE,
],
];
$views_field[$base_table]['activity_end_datetime'] = [
'title' => $this->t('Activity End Date'),
'help' => $this->t('The calculated end date and time, for calendar integrations.'),
'field' => [
'id' => 'civicrm_entity_activity_end_datetime',
'field_name' => 'activity_end_datetime',
'click sortable' => FALSE,
],
];
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
$views_field[$base_table]['contact'] = [
'title' => $this->t('Contact'),
'help' => $this->t('Relate CiviCRM contact to CiviCRM activity.'),
'relationship' => [
'id' => 'civicrm_entity_activity_contact',
'base' => 'civicrm_contact',
'base field' => 'id',
'first field' => 'activity_id',
'second field' => 'contact_id',
'label' => $this->t('Contact'),
],
];
$views_field['civicrm_contact']['activity'] = [
'title' => $this->t('Activity'),
'help' => $this->t('Relate CiviCRM activity to CiviCRM contact.'),
'relationship' => [
'id' => 'civicrm_entity_activity_contact',
'base' => 'civicrm_activity',
'base field' => 'id',
'first field' => 'contact_id',
'second field' => 'activity_id',
'label' => $this->t('Activity'),
],
];
unset(
$views_field['civicrm_contact']['reverse__civicrm_activity__assignee_id'],
$views_field['civicrm_contact']['reverse__civicrm_activity__target_id'],
$views_field['civicrm_contact']['reverse__civicrm_activity__source_contact_id'],
$views_field[$base_table]['assignee_id']['relationship'],
$views_field[$base_table]['target_id']['relationship'],
$views_field[$base_table]['source_contact_id']['relationship']
);
case 'civicrm_contact':
$views_field['civicrm_contact']['user'] = [
'title' => $this->t('User related to the CiviCRM contact'),
'help' => $this->t('Relate user to the CiviCRM contact.'),
'relationship' => [
'base' => 'users_field_data',
'base field' => 'uid',
'first field' => 'contact_id',
'second field' => 'uf_id',
'id' => 'civicrm_entity_civicrm_contact_user',
'label' => $this->t('User'),
],
];
$views_field['users_field_data']['civicrm_contact'] = [
'title' => $this->t('CiviCRM contact related to the user'),
'help' => $this->t('Relate CiviCRM contact to the user.'),
'relationship' => [
'base' => 'civicrm_contact',
'base field' => 'id',
'first field' => 'uf_id',
'second field' => 'contact_id',
'id' => 'civicrm_entity_civicrm_contact_user',
'label' => $this->t('CiviCRM contact'),
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
],
];
$views_field['civicrm_contact']['civicrm_tag'] = [
'title' => $this->t('CiviCRM tag related to the CiviCRM contact'),
'help' => $this->t('Relate CiviCRM tag to the CiviCRM contact.'),
'relationship' => [
'base' => 'civicrm_tag',
'base field' => 'id',
'table' => 'civicrm_entity_tag',
'first field' => 'entity_id',
'second field' => 'tag_id',
'id' => 'civicrm_entity_civicrm_bridge',
'label' => $this->t('CiviCRM tag'),
'extra' => [
['field' => 'entity_table', 'value' => 'civicrm_contact'],
],
],
];
$views_field['civicrm_tag']['civicrm_contact'] = [
'title' => $this->t('CiviCRM contact related to the CiviCRM tag'),
'help' => $this->t('Relate CiviCRM contact to the CiviCRM tag.'),
'relationship' => [
'base' => 'civicrm_contact',
'base field' => 'id',
'table' => 'civicrm_entity_tag',
'first field' => 'tag_id',
'second field' => 'entity_id',
'id' => 'civicrm_entity_civicrm_bridge',
'label' => $this->t('CiviCRM contact'),
'extra' => [
['field' => 'entity_table', 'value' => 'civicrm_contact'],
],
if (isset($views_field['civicrm_contact']['contact_sub_type'])) {
$views_field['civicrm_contact']['contact_sub_type']['filter']['id'] = 'civicrm_entity_in_operator';
$views_field['civicrm_contact']['contact_sub_type']['filter']['options callback'] = '\CRM_Contact_DAO_Contact::buildOptions';
$views_field['civicrm_contact']['contact_sub_type']['filter']['options arguments'] = 'contact_sub_type';
$views_field['civicrm_contact']['contact_sub_type']['filter']['multi'] = TRUE;
}
case 'civicrm_phone':
if (isset($views_field['civicrm_contact']['reverse__civicrm_phone__contact_id']['relationship'])) {
$views_field['civicrm_contact']['reverse__civicrm_phone__contact_id']['relationship']['id'] = 'civicrm_entity_reverse_location';
$views_field['civicrm_contact']['reverse__civicrm_phone__contact_id']['relationship']['label'] = $this->t('Phone');
}
break;
if (isset($views_field['civicrm_contact']['reverse__civicrm_address__contact_id']['relationship'])) {
$views_field['civicrm_contact']['reverse__civicrm_address__contact_id']['relationship']['id'] = 'civicrm_entity_reverse_location';
$views_field['civicrm_contact']['reverse__civicrm_address__contact_id']['relationship']['label'] = $this->t('Address');
}
$views_field[$base_table]['proximity'] = [
'title' => $this->t('Proximity'),
'help' => $this->t('Search for addresses by proxmity.'),
'filter' => ['id' => 'civicrm_entity_civicrm_address_proximity'],
];
case 'civicrm_email':
if (isset($views_field['civicrm_contact']['reverse__civicrm_email__contact_id']['relationship'])) {
$views_field['civicrm_contact']['reverse__civicrm_email__contact_id']['relationship']['id'] = 'civicrm_entity_reverse_location';
$views_field['civicrm_contact']['reverse__civicrm_email__contact_id']['relationship']['label'] = $this->t('Email');
}
case 'civicrm_group':
$views_field['civicrm_group']['civicrm_contact'] = [
'title' => $this->t('CiviCRM contact related to the CiviCRM group'),
'help' => $this->t('Relate CiviCRM contact to the CiviCRM group.'),
'relationship' => [
'base' => 'civicrm_contact',
'base field' => 'id',
'first field' => 'group_id',
'second field' => 'contact_id',
'id' => 'civicrm_entity_civicrm_group_contact',
'label' => $this->t('CiviCRM contact'),
'extra' => [
['field' => 'status', 'value' => 'Added'],
],
],
];
$views_field['civicrm_contact']['civicrm_group'] = [
'title' => $this->t('CiviCRM group related to the CiviCRM contact'),
'help' => $this->t('Relate CiviCRM group to the CiviCRM contact.'),
'relationship' => [
'base' => 'civicrm_group',
'base field' => 'id',
'first field' => 'contact_id',
'second field' => 'group_id',
'id' => 'civicrm_entity_civicrm_group_contact',
'label' => $this->t('CiviCRM group'),
'extra' => [
['field' => 'status', 'value' => 'Added'],
],
case 'civicrm_relationship':
if (isset($views_field['civicrm_contact']['reverse__civicrm_relationship__contact_id_a']['relationship'])) {
$views_field['civicrm_contact']['reverse__civicrm_relationship__contact_id_a']['relationship']['id'] = 'civicrm_entity_civicrm_relationship';
}
if (isset($views_field['civicrm_contact']['reverse__civicrm_relationship__contact_id_b']['relationship'])) {
$views_field['civicrm_contact']['reverse__civicrm_relationship__contact_id_b']['relationship']['id'] = 'civicrm_entity_civicrm_relationship';
}
break;
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
case 'civicrm_case':
$views_field['civicrm_case']['civicrm_activity'] = [
'title' => $this->t('CiviCRM activity related to the CiviCRM case'),
'help' => $this->t('Relate CiviCRM activity to the CiviCRM case.'),
'relationship' => [
'base' => 'civicrm_activity',
'base field' => 'id',
'table' => 'civicrm_case_activity',
'first field' => 'case_id',
'second field' => 'activity_id',
'id' => 'civicrm_entity_civicrm_case_activity',
'label' => $this->t('CiviCRM activity'),
],
];
$views_field['civicrm_activity']['civicrm_case'] = [
'title' => $this->t('CiviCRM case related to the CiviCRM activity'),
'help' => $this->t('Relate CiviCRM case to the CiviCRM activity.'),
'relationship' => [
'base' => 'civicrm_case',
'base field' => 'id',
'table' => 'civicrm_case_activity',
'first field' => 'activity_id',
'second field' => 'case_id',
'id' => 'civicrm_entity_civicrm_case_activity',
'label' => $this->t('CiviCRM case'),
],
];
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
$views_field['civicrm_case']['civicrm_contact'] = [
'title' => $this->t('CiviCRM contact related to the CiviCRM case'),
'help' => $this->t('Relate CiviCRM contact to the CiviCRM case.'),
'relationship' => [
'base' => 'civicrm_contact',
'base field' => 'id',
'table' => 'civicrm_case_contact',
'first field' => 'case_id',
'second field' => 'contact_id',
'id' => 'civicrm_entity_civicrm_case_contact',
'label' => $this->t('CiviCRM contact'),
],
];
$views_field['civicrm_contact']['civicrm_case'] = [
'title' => $this->t('CiviCRM case related to the CiviCRM contact'),
'help' => $this->t('Relate CiviCRM case to the CiviCRM contact.'),
'relationship' => [
'base' => 'civicrm_case',
'base field' => 'id',
'table' => 'civicrm_case_contact',
'first field' => 'contact_id',
'second field' => 'case_id',
'id' => 'civicrm_entity_civicrm_case_contact',
'label' => $this->t('CiviCRM case'),
],
];
unset($views_field['civicrm_case']['medium_id']);
unset($views_field['civicrm_case']['contact_id']['relationship']);
break;
case 'civicrm_line_item':
$views_field['civicrm_line_item']['entity_id'] = [
'title' => $this->t('CiviCRM membership'),
'help' => $this->t('Relate CiviCRM entity to the line item entity for membership.'),
'relationship' => [
'id' => 'standard',
'base field' => 'id',
'base' => 'civicrm_membership',
'label' => $this->t('Membership')
],
];
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
case 'civicrm_price_set':
$views_field['civicrm_price_set_entity']['table'] = [
'group' => $this->t('CiviCRM price set entity'),
'base' => [
'field' => 'id',
'title' => $this->t('CiviCRM price set entity'),
'help' => $this->t('View displays CiviCRM Price Set to Entity mapping info.'),
],
];
$views_field['civicrm_price_set_entity']['price_set_id'] = [
'title' => $this->t('Price set'),
'help' => $this->t('Price set for this entity.'),
'relationship' => [
'id' => 'standard',
'base' => 'civicrm_price_set',
'base field' => 'id',
'label' => $this->t('Price set'),
],
];
$views_field['civicrm_event']['table']['join']['civicrm_price_set_entity'] = [
'left_field' => 'entity_id',
'field' => 'id',
];
$views_field['civicrm_price_set_entity']['table']['join']['civicrm_event'] = [
'left_field' => 'id',
'field' => 'entity_id',
];
break;
case 'civicrm_participant':
$views_field[$base_table]['transferred_to_contact_id']['help'] = $this->t('FK to "Transferred to" Contact ID');
break;
}
/**
* Get the views field handler.
*
* @param array $field_metadata
* An array of field metadata.
*
* @return array
* An array containing the corresponding values for the 'field' key.
*/
protected function getViewsFieldPlugin(array $field_metadata) {
switch ($field_metadata['data_type']) {
case 'File':
return ['id' => 'civicrm_entity_custom_file'];
}
return ['id' => 'civicrm_entity_custom_field'];
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
}
/**
* Get the views filter handler.
*
* @param array $field_metadata
* An array of field metadata.
*
* @return array
* An array containing the corresponding values for the 'sort' key.
*/
protected function getViewsSortPlugin(array $field_metadata) {
$type = \CRM_Utils_Array::value($field_metadata['data_type'], \CRM_Core_BAO_CustomField::dataToType());
switch ($type) {
case \CRM_Utils_Type::T_DATE:
case \CRM_Utils_Type::T_TIMESTAMP:
return ['id' => 'date'];
default:
return ['id' => 'standard'];
}
}
/**
* Get the views sort handler.
*
* @param array $field_metadata
* An array of field metadata.
*
* @return array
* An array containing the corresponding values for the 'filter' key.
*/
protected function getViewsFilterPlugin(array $field_metadata) {
switch ($field_metadata['data_type']) {
case 'Country':
$filter = [
'id' => 'civicrm_entity_in_operator',
'options callback' => 'CRM_Core_PseudoConstant::country',
];
if ($field_metadata['html_type'] === 'Multi-Select Country') {
$filter['multi'] = TRUE;
}
return $filter;
case 'StateProvince':
$filter = [
'id' => 'civicrm_entity_in_operator',
'options callback' => 'CRM_Core_PseudoConstant::stateProvince',
];
if ($field_metadata['html_type'] === 'Multi-Select State/Province') {
$filter['multi'] = TRUE;
}
return $filter;
case 'ContactReference':
return [
'id' => 'civicrm_entity_contact_reference',
'allow empty' => TRUE,
'multi' => TRUE,
];
}
$type = !empty($field_metadata['pseudoconstant']) ? 'pseudoconstant' :
\CRM_Utils_Array::value($field_metadata['data_type'], \CRM_Core_BAO_CustomField::dataToType());
switch ($type) {
case \CRM_Utils_Type::T_BOOLEAN:
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
case \CRM_Utils_Type::T_INT:
case \CRM_Utils_Type::T_FLOAT:
case \CRM_Utils_Type::T_MONEY:
return ['id' => 'numeric'];
case \CRM_Utils_Type::T_ENUM:
case \CRM_Utils_Type::T_STRING:
case \CRM_Utils_Type::T_TEXT:
case \CRM_Utils_Type::T_LONGTEXT:
case \CRM_Utils_Type::T_URL:
case \CRM_Utils_Type::T_EMAIL:
return ['id' => 'string'];
case \CRM_Utils_Type::T_DATE:
case \CRM_Utils_Type::T_TIMESTAMP:
return ['id' => 'civicrm_entity_date'];
case 'pseudoconstant':
if ($class_name = SupportedEntities::getEntityTypeDaoClass($field_metadata['entity_type'])) {
$filter = [
'id' => 'civicrm_entity_in_operator',
'options callback' => "{$class_name}::buildOptions",
'options arguments' => $field_metadata['name'],
];
if (in_array($field_metadata['html_type'], ['Multi-Select', 'CheckBox'])) {
$filter['multi'] = TRUE;
}
return $filter;
}
break;
default:
return ['id' => 'standard'];
}
}
/**
* Get the views argument handler.
*
* @param array $field_metadata
* An array of field metadata.
*
* @return array
* An array containing the corresponding values for the 'argument' key.
*/
protected function getViewsArgumentPlugin(array $field_metadata) {
$type = \CRM_Utils_Array::value($field_metadata['data_type'], \CRM_Core_BAO_CustomField::dataToType());
switch ($type) {
case \CRM_Utils_Type::T_INT:
case \CRM_Utils_Type::T_FLOAT:
case \CRM_Utils_Type::T_MONEY:
return ['id' => 'numeric'];
case \CRM_Utils_Type::T_ENUM:
case \CRM_Utils_Type::T_STRING:
case \CRM_Utils_Type::T_TEXT:
case \CRM_Utils_Type::T_LONGTEXT:
case \CRM_Utils_Type::T_URL:
case \CRM_Utils_Type::T_EMAIL:
return ['id' => 'string'];
case \CRM_Utils_Type::T_DATE:
case \CRM_Utils_Type::T_TIMESTAMP:
return ['id' => 'civicrm_entity_date'];
default:
return ['id' => 'standard'];
}
}
/**
* Get the views argument handler.
*
* @param array $field_metadata
* An array of field metadata.
*
* @return array
* An array containing the corresponding values for the 'argument' key.
*/
protected function getViewsRelationshipPlugin(array $field_metadata) {
switch ($field_metadata['data_type']) {
case 'ContactReference':
return [
'id' => 'standard',
'base' => 'civicrm_contact',
'base field' => 'id',
'label' => $this->t('@label', ['@label' => $field_metadata['label']]),
'join_id' => isset($field_metadata['serialize']) && $field_metadata['serialize'] ? 'civicrm_entity_contact_reference' : 'standard',
];
default:
return [];
}
}
/**
* {@inheritdoc}
*/
protected function processViewsDataForTextLong($table, FieldDefinitionInterface $field_definition, array &$views_field, $field_column_name) {
if ($field_column_name === 'value') {
$views_field['real field'] = $field_definition->getName();
}
if ($field_column_name === 'format') {