From 741b920a20f7370421520eece92265490846e745 Mon Sep 17 00:00:00 2001 From: anemirovsky Date: Fri, 16 Nov 2018 10:51:57 -0800 Subject: [PATCH] Add a Views Relationship handler for relating a Contact to their Contributions. The existing Views integration allows for pulling in Contributions related to a Contact via indirect methods, like from a related Participant or Membership record. Doing so does not allow Contribution Custom Fields to be used in the View, though. This change adds an explicit Relationship between Contacts and Contribution which, when added to a View, allows for pulling in Custom Field data for Contributions. --- civicrm.info | 1 + ...er_relationship_contacts_contributions.inc | 53 +++++++++++++++++++ modules/views/components/civicrm.core.inc | 14 +++++ 3 files changed, 68 insertions(+) create mode 100644 modules/views/civicrm/civicrm_handler_relationship_contacts_contributions.inc diff --git a/civicrm.info b/civicrm.info index a146ec862..daf7a9543 100644 --- a/civicrm.info +++ b/civicrm.info @@ -89,6 +89,7 @@ files[] = modules/views/civicrm/views_handler_argument_civicrm_year_month.inc files[] = modules/views/civicrm/civicrm_handler_relationship.inc files[] = modules/views/civicrm/civicrm_handler_relationship_relationship.inc files[] = modules/views/civicrm/civicrm_handler_relationship_contact2users.inc +files[] = modules/views/civicrm/civicrm_handler_relationship_contacts_contributions.inc files[] = modules/views/civicrm/civicrm_handler_relationship_memberships_contributions.inc files[] = modules/views/civicrm/civicrm_handler_relationship_location.inc files[] = modules/views/civicrm/civicrm_handler_relationship_mail.inc diff --git a/modules/views/civicrm/civicrm_handler_relationship_contacts_contributions.inc b/modules/views/civicrm/civicrm_handler_relationship_contacts_contributions.inc new file mode 100644 index 000000000..79fd6bb74 --- /dev/null +++ b/modules/views/civicrm/civicrm_handler_relationship_contacts_contributions.inc @@ -0,0 +1,53 @@ +options['required']) ? 'LEFT' : 'INNER'; + $this->ensure_my_table(); + $join = new views_join(); + $join->definition = array( + 'table' => 'civicrm_contribution', + 'field' => 'contact_id', + 'left_table' => $this->table_alias, + 'left_field' => 'id', + 'type' => $join_type, + ); + $join->construct(); + $join->adjusted = TRUE; + $alias = $join->definition['table'] . '_' . $join->definition['left_table']; + $this->alias = $this->query->add_relationship($alias, $join, 'civicrm_contribution', $this->relationship); + } + +} diff --git a/modules/views/components/civicrm.core.inc b/modules/views/components/civicrm.core.inc index b82d8cfde..beac713e0 100644 --- a/modules/views/components/civicrm.core.inc +++ b/modules/views/components/civicrm.core.inc @@ -2454,6 +2454,20 @@ function _civicrm_core_data(&$data, $enabled) { ), ); + // Link to Contribution table + if (isset($enabled['CiviContribute'])) { + $data['civicrm_contact']['contribution_id'] = array( + 'title' => t('Contribution Records'), + 'help' => 'Contributions records for this Contact', + 'relationship' => array( + 'base' => 'civicrm_contribution', + 'base field' => 'id', + 'handler' => 'civicrm_handler_relationship_contacts_contributions', + 'label' => t('Contact -> Contribution'), + ), + ); + } + //---------------------------------------------------------------- // CIVICRM Relationships are here with all the connections, base tabling it up. //----------------------------------------------------------------