Created
June 10, 2014 20:16
-
-
Save bdombro/6312465947009055f72b to your computer and use it in GitHub Desktop.
Flexslider used on river pages
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
<?php | |
function relation_get_related_entities($entity_type, $entity_id, $relation_type = NULL, $r_index = NULL) { | |
$query = relation_query($entity_type, $entity_id, $r_index); | |
if ($relation_type) { | |
$query->propertyCondition('relation_type', $relation_type); | |
} | |
$results = $query->execute(); | |
$relations = array(); | |
foreach($results as $result) { | |
if (empty($result)) { | |
return FALSE; | |
} | |
$relation = relation_load($result->rid); | |
$request_key = $entity_type . ':' . $entity_id; | |
$entities = $relation->endpoints[LANGUAGE_NONE]; | |
$first_entity_key = $entities[0]['entity_type'] . ':' . $entities[0]['entity_id']; | |
if (isset($r_index)) { | |
$request_key = $request_key . ':' . $r_index; | |
$first_entity_key = $first_entity_key . ':' . $entities[0]['r_index']; | |
} | |
if ($request_key == $first_entity_key) { | |
$other_endpoints = entity_load($entities[1]['entity_type'], array($entities[1]['entity_id'])); | |
foreach($other_endpoints as $endpoint) | |
$relations[] = $endpoint; | |
} | |
else { | |
$other_endpoints = entity_load($entities[0]['entity_type'], array($entities[0]['entity_id'])); | |
foreach($other_endpoints as $endpoint) | |
$relations[] = $endpoint; | |
} | |
} | |
return $relations; | |
} | |
function generate_image_style($file_uri) { | |
$original_path = file_uri_target($file_uri); | |
$style_path = image_style_path('large2', $file_uri); | |
if (!file_exists($style_path)) { | |
image_style_create_derivative(image_style_load('large2'), $file_uri, $style_path); | |
} | |
if (file_exists($style_path)) | |
return image_style_url('large2', $file_uri); | |
else | |
return file_create_url($file_uri); | |
} | |
$nid_filter = isset($_REQUEST['nid']) ? $_REQUEST['nid'] : null; | |
if($nid_filter) { | |
$nodes_to_check = array( (array) node_load($nid_filter)); | |
if ($nodes_to_check[0]['type'] == 'river_section') { | |
$features = relation_get_related_entities('node', $nid_filter, $relation_type = 'has_feature'); | |
foreach($features as $feature) | |
$nodes_to_check[] = (array) $feature; | |
} | |
else if ($nodes_to_check[0]['type'] == 'river') { | |
$nodes_to_check[] = (array) relation_get_related_entity('node', $nid_filter, $relation_type = 'has_river_section'); | |
$sections = relation_get_related_entities('node', $nid_filter, $relation_type = 'has_river_section'); | |
foreach($sections as $section) { | |
$nodes_to_check[] = (array) $section; | |
$features = relation_get_related_entities('node', $section->nid, $relation_type = 'has_feature'); | |
foreach($features as $feature) | |
$nodes_to_check[] = (array) $feature; | |
} | |
} | |
// print "<pre>"; | |
// print_r($result); | |
// print "</pre>"; | |
$photos = array(); | |
foreach($nodes_to_check as $node) { | |
if( isset($node['field_photos']) ) { | |
// print_r($node['field_photos'][und]); | |
foreach($node['field_photos'][und] as $photo) { | |
if(!in_array($photo, $photos)) | |
$photos[] = $photo; | |
} | |
} | |
} | |
if(count($photos)) { ?> | |
<div id="river_photo_gallery_slider" class="slider" style="width: 100%;"> | |
<div class="flexslider"> | |
<div class="loading">Loading...</div> | |
<ul class="slides hidden"> | |
<?php foreach($photos as $photo) { ?> | |
<li> | |
<div class="img_container"> | |
<a href="/file/<?php print $photo['fid']; ?>"> | |
<img alt="<?php print $photo['field_caption'][und][0]['value']; ?>" | |
src="<?php print generate_image_style($photo['uri']); ?>" > | |
</a> | |
</div> | |
</li> | |
<?php } ?> | |
</ul> | |
</div> | |
</div> | |
<?php } | |
} | |
?> | |
<!-- this following is added to template.tpl instead | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.2.0/jquery.flexslider-min.js"></script> | |
drupal_add_js('https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.2.0/jquery.flexslider-min.js', 'external'); | |
--> | |
<script> | |
jQuery(document).ready(function(){ | |
jQuery('.flexslider').flexslider({ | |
animation: "slide", | |
start: function(slider){ | |
jQuery('.flexslider .loading').hide(); | |
jQuery('.slides').removeClass('hidden'); | |
} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment