From 710b7c9131acf38572287b7147084891fd456e0d Mon Sep 17 00:00:00 2001 From: puresyntax71 <34715246+puresyntax71@users.noreply.github.com> Date: Wed, 13 May 2020 12:23:54 +0800 Subject: [PATCH] Add relationship between contact, uf_match, and users. --- src/CivicrmEntityViewsData.php | 44 ++++++++++++ .../views/relationship/CiviCrmContactUser.php | 71 +++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 src/Plugin/views/relationship/CiviCrmContactUser.php diff --git a/src/CivicrmEntityViewsData.php b/src/CivicrmEntityViewsData.php index 51db2018..3e161907 100644 --- a/src/CivicrmEntityViewsData.php +++ b/src/CivicrmEntityViewsData.php @@ -112,6 +112,9 @@ public function getViewsData() { } } } + + $this->processViewsDataForSpecialFields($data, $base_table); + return $data; } @@ -199,4 +202,45 @@ protected function processViewsDataForListInteger($table, FieldDefinitionInterfa $views_field['argument']['field_name'] = $field_definition->getName(); } + /** + * 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_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'), + ], + ]; + + break; + } + } + } diff --git a/src/Plugin/views/relationship/CiviCrmContactUser.php b/src/Plugin/views/relationship/CiviCrmContactUser.php new file mode 100644 index 00000000..588898bb --- /dev/null +++ b/src/Plugin/views/relationship/CiviCrmContactUser.php @@ -0,0 +1,71 @@ +ensureMyTable(); + + $views_data = Views::viewsData()->get($this->table); + $left_field = $views_data['table']['base']['field']; + + // Add a join to the civicrm_uf_match table with the matching contact_id. + $first = [ + 'left_table' => $this->tableAlias, + 'left_field' => $left_field, + 'table' => 'civicrm_uf_match', + 'field' => $this->definition['first field'], + 'adjusted' => TRUE, + ]; + + if (!empty($this->options['required'])) { + $first['type'] = 'INNER'; + } + + $first_join = Views::pluginManager('join')->createInstance('standard', $first); + $first_alias = $this->query->addTable('civicrm_uf_match', $this->relationship, $first_join); + + // Relate the first join to the base table defined. + $second = [ + 'left_table' => $first_alias, + 'left_field' => $this->definition['second field'], + 'table' => $this->definition['base'], + 'field' => $this->definition['base field'], + 'adjusted' => TRUE, + ]; + + if (!empty($this->options['required'])) { + $second['type'] = 'INNER'; + } + + $second_join = Views::pluginManager('join')->createInstance('standard', $second); + $second_join->adjusted = TRUE; + + $alias = $this->definition['base'] . '_civicrm_uf_match'; + + $this->alias = $this->query->addRelationship($alias, $second_join, $this->definition['base'], $this->relationship); + } + +}