Created
March 21, 2012 14:55
-
-
Save mlutfy/2147910 to your computer and use it in GitHub Desktop.
Show PCP contributions for the logged-in user using Views (Drupal)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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