Skip to content

Instantly share code, notes, and snippets.

@jeremycaldwell
Created October 22, 2015 04:15
Show Gist options
  • Save jeremycaldwell/7287d4649143cca46397 to your computer and use it in GitHub Desktop.
Save jeremycaldwell/7287d4649143cca46397 to your computer and use it in GitHub Desktop.
<?php
/**
* Implements hook_field_formatter_info().
*/
function aspphotogallery_field_formatter_info() {
return array(
'aspphotogallery_default' => array(
'label' => t('ASP Photo Gallery'),
'field types' => array('image'),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function aspphotogallery_field_formatter_view($obj_type, $object, $field, $instance, $langcode, $items, $display) {
$element = array();
// Build variables array for formatter.
$variables = array(
'#obj_type' => $obj_type,
'#object' => $object,
'#field' => $field,
'#instance' => $instance,
'#langcode' => $langcode,
'#items' => $items,
'#display' => $display,
);
if (function_exists($function = "{$display['module']}_field_formatter_{$display['type']}")) {
$element[0] = array(
'#markup' => $function($variables),
);
}
return $element;
}
/**
* Field Formatter.
*/
function aspphotogallery_field_formatter_aspphotogallery_default($variables) {
// Load the currently logged in user.
global $user;
// Load custom JS only on non-admin pages.
if (arg(0) != 'admin') {
// ShareThis JS.
sharethis_include_js();
// Photo gallery JS.
drupal_add_js(file_create_url(drupal_get_path('module', 'aspphotogallery') . '/js/ASPPhotoGallery.js', array('preprocess' => FALSE)));
}
// Entity.
$entity = $variables['#object'];
// Get list of Exhibit IDs.
$imageReferenceID = $entity->field_photogallery_image['und'];
// Columns.
$columns = $entity->field_photogallery_columns['und'][0]['value'];
// Display type.
$displayType = $entity->field_photogallery_display_type['und'][0]['value'];
// Loop through each Exhibit entity.
foreach ($imageReferenceID as $key => $val) {
// Get File ID from array.
$fileID = $imageReferenceID[$key]['fid'];
// Load File entity.
$fileEntity = entity_load_single('file', $fileID);
// Person.
// Get People ID from array.
$peopleID = $fileEntity->field_photographer_reference['und'][0]['target_id'];
// Load People entity.
$peopleEntity = entity_load_single('people', $peopleID);
// Person name.
$nameArray = $peopleEntity->field_name['und'][0];
// First, Middle, Last name.
(!empty($nameArray['given']))
? $firstName = $nameArray['given'] . ' '
: $firstName = NULL;
(!empty($nameArray['middle']))
? $middleName = $nameArray['middle'] . ' '
: $middleName = NULL;
(!empty($nameArray['family']))
? $lastName = $nameArray['family']
: $lastName = NULL;
// Person name combined.
$peopleName = $firstName . $middleName . $lastName;
// Caption.
$imageCaption = $fileEntity->field_caption[und][0]['value'];
// Copyright.
if (!empty($fileEntity->field_credit[und][0]['value'])) {
$imageCredit = $fileEntity->field_credit[und][0]['value'];
} elseif (!empty($peopleName)) {
$imageCredit = $peopleName;
} else {
$imageCredit = '';
}
// Share URL
$url = $GLOBALS['base_url'] . '/' . drupal_get_path_alias();
$urlShare = $url . '?aspPhotoGallery=' . $fileID;
// Image - Preview.
$imagePreviewFID = $fileEntity->field_grid_preview['und'][0]['fid'];
$imagePreviewURI = file_load($imagePreviewFID)->uri;
$imagePreview = theme('image', array('path' => $imagePreviewURI));
// Image - Large.
$imageLargeURI = file_load($fileID)->uri;
$imageLarge = file_create_url($imageLargeURI);
// Image - Carousel.
$imageCarousel = theme('image', array('path' => $imageLargeURI));
// Display name.
if (!empty($fileEntity->field_display_name[und][0]['value'])) {
$displayName = $fileEntity->field_display_name[und][0]['value'];
}
else {
$displayName = '';
}
// Image attributes.
$imageAttributes =
array(
'html' => TRUE,
'attributes' =>
array(
'id' => 'aspPhotoGallery-' . $fileID,
'class' => 'aspPhotoGallery',
'rel' => 'aspPhotoGallery',
'data-title' => $displayName,
'data-photographer' => $imageCredit,
'data-caption' => $imageCaption,
'data-share' => '<h2 class="title">' . t('Share') . '</h2><div class="ShareThis"><div class="sharethis-wrapper"><span st_url="' . $urlShare . '" st_title="' . $peopleName . '" class="st_facebook_custom" displaytext="facebook"></span><span st_url="' . $urlShare . '" st_title="' . $peopleName . '" class="st_twitter_custom" displaytext="twitter"></span><span st_url="' . $urlShare . '" st_title="' . $peopleName . '" class="st_googleplus_custom" displaytext="googleplus"></span><span st_url="' . $urlShare . '" st_title="' . $peopleName . '" class="st_email_custom" displaytext="email"></span></div></div>'
)
);
// Display name with markup.
if (!empty($fileEntity->field_display_name[und][0]['value'])) {
$displayNameMarkup = '<h3>' . l($displayName, $imageLarge, $imageAttributes) . '</h3>';
}
else {
$displayNameMarkup = '';
}
// Display type of "Carousel".
if ($displayType == '0') {
$listitem =
'<li class="item">' .
l($imageCarousel, $imageLarge, $imageAttributes) . $displayNameMarkup . $imageCredit .
'</li>';
// Output.
$output[] = $listitem;
$aspPhotoGallery_output =
'<div class="ThumbnailSlides">
<div class="ThumbnailSlides-carousel aspect-564x380">
<div class="ThumbnailSlides-content">
<ul class="slides">' .
implode('', $output) .
'</ul>
</div>
</div>
</div>';
}
// Display type of "Grid".
elseif ($displayType == '1') {
// Build markup.
if (!empty($imagePreviewFID)) {
$listitem =
'<li class="col col-custom-' . $columns . '">' .
l($imagePreview, $imageLarge, $imageAttributes) .
'</li>';
}
else if (in_array('administrator', $user->roles)) {
$listitem =
'<li class="col col-custom-' . $columns . ' center">' .
l('Add grid preview image', 'file/'. $fileID . '/edit',
array(
'attributes' =>
array (
'class' => 'btn btn-primary',
),
'query' =>
array (
'destination' => $_GET['q'],
),
)
) .
'</li>';
} else {
$listitem = '';
}
// Output.
$output[] = $listitem;
$aspPhotoGallery_output =
'<div class="views-bootstrap-grid-plugin-style">
<ul class="row-custom-' . $columns . '">' .
implode('', $output) .
'</ul>
</div>';
}
}
return $aspPhotoGallery_output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment