You are here

function _field_timer_jquery_countdown_formatter in Field Timer 7

Help function.

Build renderable array and load necessary files for jQuery Countdown formatter.

1 call to _field_timer_jquery_countdown_formatter()
field_timer_field_formatter_view in ./field_timer.module
Implements hook_field_formatter_view().

File

includes/field_timer.inc, line 97
Contains some help functions for formatters and settings forms.

Code

function _field_timer_jquery_countdown_formatter($items, $settings, $entity_type, $entity, $instance) {
  $info = entity_get_info($entity_type);
  $id_key = 'id_' . $entity->{$info['entity keys']['id']};
  $field_name = $instance['field_name'];
  $js_settings = array();
  $elements = array();

  // Add description, expiry text and url.
  $instance_settings = $instance['settings']['jquery-countdown'];
  $data = array(
    $entity_type => $entity,
  );
  $instance_settings['expiryUrl'] = token_replace($instance_settings['expiryUrl'], $data);
  $instance_settings['expiryText'] = token_replace($instance_settings['expiryText'], $data);
  $instance_settings['description'] = token_replace($instance_settings['description'], $data);
  foreach ($items as $delta => $item) {
    $elements[$delta] = array(
      '#type' => 'markup',
      '#markup' => '<span id="jquery-countdown-' . $entity_type . '-' . $id_key . '-' . $instance['field_name'] . '-' . $delta . '" class="jquery-countdown"></span>',
    );
    $js_settings[$entity_type][$id_key][$field_name][$delta]['timestamp'] = $item['timestamp'];
    $js_settings[$entity_type][$id_key][$field_name][$delta]['options'] = array_merge($settings, $instance_settings);
    $js_settings[$entity_type][$id_key][$field_name][$delta]['plugin'] = 'jquery.countdown';
    if ($settings['type'] == 'timer' || $settings['type'] == 'auto' && $item['timestamp'] <= REQUEST_TIME) {
      $js_settings[$entity_type][$id_key][$field_name][$delta]['options']['until'] = FALSE;
      $js_settings[$entity_type][$id_key][$field_name][$delta]['options']['since'] = TRUE;
    }
    elseif ($settings['type'] == 'countdown' || $settings['type'] == 'auto' && $item['timestamp'] > REQUEST_TIME) {
      $js_settings[$entity_type][$id_key][$field_name][$delta]['options']['until'] = TRUE;
      $js_settings[$entity_type][$id_key][$field_name][$delta]['options']['since'] = FALSE;
    }
  }

  // Load library files.
  $library = libraries_load('jquery.countdown');
  $library_loaded = $library && !empty($library['loaded']);
  $js_name = 'jquery.countdown.js';
  $js_name_min = 'jquery.countdown.min.js';
  $js_plugin_name = 'jquery.plugin.js';
  $js_plugin_name_min = 'jquery.plugin.min.js';
  $base_path = 'sites/all/libraries/jquery.countdown';
  if (!$library_loaded) {
    drupal_set_message(t("Can't load jQuery Countdown library. Please download !url plugin and extract it to @path, so @js, @js_min, @plugin and @plugin_min can be found at @full_path and @full_path_min.", array(
      '!url' => l(t('jQuery Countdown'), 'http://keith-wood.name/countdown.html', array(
        'absolute' => TRUE,
      )),
      '@path' => $base_path,
      '@js' => $js_name,
      '@js_min' => $js_name_min,
      '@plugin' => $js_plugin_name,
      '@plugin_min' => $js_plugin_name_min,
      '@full_path' => $base_path . '/' . $js_name,
      '@full_path_min' => $base_path . '/' . $js_name_min,
    )), 'error');
    return array();
  }

  // Add necessary js and css files.
  drupal_add_js(array(
    'field_timer' => $js_settings,
  ), 'setting');
  $path = libraries_get_path('jquery.countdown');
  if (file_exists($path . '/jquery.countdown-' . $settings['regional'] . '.js')) {
    drupal_add_js($path . '/jquery.countdown-' . $settings['regional'] . '.js');
  }
  drupal_add_js(drupal_get_path('module', 'field_timer') . '/js/field_timer.js', 'file');
  drupal_add_css(drupal_get_path('module', 'field_timer') . '/css/field_timer.css');
  return $elements;
}