Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
pcp_views_handlers.info:
name = PCP Views Field Handlers -- Additional
description = Adds some CiviCRM PCP Views handlers
core = 7.x
package = Custom
version = "7.x-1.0"
dependencies[] = civicrm
dependencies[] = views
dependencies[] = imagecache_external
files[] = views/pcp_views_handlers_handler_alternate_image_handler_field.inc
pcp_views_handlers.module:
/**
* Implements hook_views_api().
*/
function pcp_views_handlers_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'pcp_views_handlers') . '/views',
);
}
/**
* Implements hook_views_data_alter().
*
* @param $data
*/
function pcp_views_handlers_views_data_alter(&$data){
$data['civicrm_pcp']['skvare_pcp_image'] = array (
'title' => 'PCP Image - Custom',
'help' => t('Render the PCP image via custom field handler'),
'field' => array(
'handler' => 'pcp_views_handlers_handler_alternate_image_handler_field',
'click sortable' => FALSE,
),
);
}
views/pcp_views_handlers_handler_alternate_image_handler_field.inc:
/**
* Alternate image handler for PCP images
*
*/
class pcp_views_handlers_handler_alternate_image_handler_field extends views_handler_field {
function construct() {
parent::construct();
$this->additional_fields['id'] = 'id';
}
function option_definition() {
$options = parent::option_definition();
$options['image_style'] = array('default' => '');
return $options;
}
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
if (module_exists('image')) {
$styles = image_styles();
$style_options = array('' => t('Default'));
foreach ($styles as $style) {
$style_options[$style['name']] = $style['name'];
}
$form['image_style'] = array(
'#title' => t('Image style'),
'#description' => t('Drupal Image Style'),
'#type' => 'select',
'#options' => $style_options,
'#default_value' => $this->options['image_style'],
);
}
}
function query() {
$this->ensure_my_table();
$this->add_additional_fields();
}
function render($values) {
$output = '';
$pcp_id = $values->id;
if(!civicrm_initialize(TRUE)){
return FALSE;
}
$file_array = CRM_Core_BAO_File::getEntityFile('civicrm_pcp', $pcp_id);
if(is_array($file_array) && count($file_array)) {
$keys = array_keys($file_array);
$file_id = $keys[0];
$image_path = '/civicrm/file?reset=1&id=' . $file_id . '&eid=' . $pcp_id;
if ($this->options['image_style']) {
global $base_url;
$output = theme('imagecache_external', array(
'path' => $base_url . $image_path,
'style_name'=> $this->options['image_style'],
));
}
else {
$output = '<img class="pcp-image" src="' . $image_path . '" />';
}
}
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.