type == 'timeline' && $view_mode == 'full') { if (isset($node->nid)) { $node->content['timeline']['#markup'] = timelinejs($node->nid); } else { $node->content['timeline']['#markup'] = t('Timeline will be rendered just after saving it.'); } $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, ); $items['widgets/timeline'] = array( 'page callback' => 'timelinejs_widget', '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( 'module_path' => drupal_get_path('module', 'timelinejs'), 'nid' => NULL, 'width' => NULL, 'height' => NULL, 'source' => NULL, 'start_at_end' => 'false', 'hash_bookmark' => 'true', 'css' => NULL, 'js' => '/' . timelinejs_library_path() . '/compiled/js/timeline.js', 'font' => NULL, 'lang' => NULL, 'lang_js' => NULL, ), ), 'timelinejs_embed' => array( 'template' => 'timelinejs_embed', 'variables' => array( 'nid' => NULL, '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', 'date_format'), ), 'timelinejs_print' => array( 'template' => 'timelinejs_print', 'variables' => array('headline', 'date', 'text', 'media', 'events'), ), 'timelinejs_print_event' => array( 'template' => 'timelinejs_print_event', 'variables' => array('event', 'date_format'), ), ); } /** * Determine the library path. */ function timelinejs_library_path() { // We have to use "timeline" instead of "timelinejs" because of // this issue: https://github.com/VeriteCo/TimelineJS/issues/403 return libraries_get_path('timeline'); } /** * 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); if (isset($timeline->field_date_format['und'][0]['value'])) { $date_format = check_plain($timeline->field_date_format['und'][0]['value']); } else { $date_format = '%Y,%m,%d'; } 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, 'date_format' => $date_format)); 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) { header('Content-type: application/json; charset=utf-8', true); print theme('timelinejs_json', timelinejs_fetch_items($nid)); // Avoid themable output. exit; } /** * Menu callback. */ function timelinejs_widget($nid = NULL, $width = '960px', $height = '500px') { // Check parameters. if ($nid == NULL) { print t('Page not found'); exit; } else { $node = node_load($nid); if ($node == FALSE) { print t('Page not found'); exit; } } // Format output and include timeline. $output = ''; $output .= ''; $output .= '
'; $output .= '