Skip to content

Instantly share code, notes, and snippets.

@mlutfy
Created March 21, 2012 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlutfy/2147910 to your computer and use it in GitHub Desktop.
Save mlutfy/2147910 to your computer and use it in GitHub Desktop.
Show PCP contributions for the logged-in user using Views (Drupal)
/**
* Implements hook_views_query_alter().
*
* HOW TO USE THIS:
* 1- Create a View that displays contributions
* 2- Add a filter to the View for "soft-credit ID = -1". This means that by
* default, no contributions will be shown.
* 3- Enter the correct $view->name for your view in the code below.
* If in doubt, use dsm($view); with the devel module enabled.
*
* A better solution would be to implement a filter in the Views implementation
* of CiviCRM for "The contact ID of the logged-in user" (similar to Drupal's
* filter for the 'currently logged-in user').
*/
function mymodule_views_query_alter(&$view, &$query) {
if ($view->name == 'pcp_donations') {
// get the user's contact_id
global $user;
if ($user->uid) {
civicrm_initialize(TRUE);
require_once 'CRM/Core/BAO/UFMatch.php';
$contact_id = CRM_Core_BAO_UFMatch::getContactId($user->uid);
// allow admins to override using [url to view]/[contact id]
if (user_access('view all contacts') && arg(1)) {
$contact_id = arg(1);
}
if ($contact_id) {
foreach ($query->where as $key => $where) {
foreach ($where['conditions'] as $key2 => $condition) {
if ($condition['field'] == 'civicrm_contribution_soft.contact_id') {
$query->where[$key]['conditions'][$key2]['value'] = $contact_id;
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment