type == 'timeline' && $view_mode == 'full') { $node->content['timeline']['#markup'] = timelinejs($node->nid); $node->content['timeline']['#weight'] = -1; } } /** * Implements hook_menu() */ function timelinejs_menu() { $items['timelinejs/json'] = array( 'page callback' => 'timelinejs_json', 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); $items['timeline'] = array( 'page callback' => 'timelinejs', 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_theme() */ function timelinejs_theme($existing, $type, $theme, $path) { return array( 'timelinejs' => array( 'template' => 'timelinejs', 'variables' => array( 'width' => NULL, 'height' => NULL, 'source' => NULL, 'start_at_end' => 'false', 'hash_bookmark' => 'true', 'css' => NULL, 'js' => '/sites/all/libraries/timelinejs/compiled/js/timeline.js', 'lang' => NULL, ), ), 'timelinejs_json' => array( 'template' => 'timelinejs_json', 'variables' => array('headline', 'date', 'text', 'media', 'events'), ), 'timelinejs_json_event' => array( 'template' => 'timelinejs_json_event', 'variables' => array('event'), ), 'timelinejs_print' => array( 'template' => 'timelinejs_print', 'variables' => array('headline', 'date', 'text', 'media', 'events'), ), 'timelinejs_print_event' => array( 'template' => 'timelinejs_print_event', 'variables' => array('event'), ), ); } /** * Get a score nid from a timeline nid. */ function timelinejs_load_events($nid) { $query = db_select('field_data_field_timeline', 't'); // Basic query. $query ->fields('t', array('entity_id')) ->condition('t.bundle', 'event') ->condition('t.field_timeline_target_id', $nid, '='); $result = $query->execute(); return $result->fetchAll(); } /** * Load all events from a timeline. */ function timelinejs_fetch_items($nid, $format = 'json') { // Sanitization and basic data. $nid = (int) $nid; $timeline = node_load($nid); $nodes = timelinejs_load_events($nid); foreach ($nodes as $node) { $items[] = node_load($node->entity_id); } if (empty($items)) { $items[0] = new stdClass(); $items[0]->field_date['und'][0]['value'] = NULL; $items[0]->title = t('No items'); $items[0]->body['und'][0]['value'] = t('Currently there are no items in this timeline.'); } foreach ($items as $event) { if (!isset($event->body['und'])) { $event->body['und'] = NULL; } $output[] = theme('timelinejs_'. $format .'_event', array('event' => $event)); if ($format == 'json') { $events = implode(',', $output); } else { $events = implode($output); } } $text = $media = NULL; $headline = $timeline->title; $date = NULL; return array( 'headline' => $headline, 'date' => $date, 'text' => $text, 'media' => $media, 'events' => $events, ); } /** * Menu callback. */ function timelinejs_json($nid) { print theme('timelinejs_json', timelinejs_fetch_items($nid)); # Avoid themable output. exit; } /** * Menu callback. */ function timelinejs($nid, $width = '960px', $height = '500px') { global $language; global $theme; global $base_url; $theme_path = drupal_get_path('theme', $theme); $library_path = drupal_get_path('libraries', 'timelinejs'); $css = (file_exists($theme_path .'/timeline.css')) ? '/'. $theme_path .'/timeline.css' : '/'. $library_path .'/compiled/css/timeline.css'; return theme('timelinejs', array( 'width' => $width, 'height' => $height, 'source' => $base_url .'/timelinejs/json/'. (int) $nid, 'css' => $css, 'lang' => '/sites/all/libraries/timelinejs/compiled/js/locale/'. $language->language .'.js', )); } /** * Menu callback. */ function timelinejs_print($nid) { return theme('timelinejs_print', timelinejs_fetch_items($nid, 'print')); }