array( 'label' => t('Countdown Timer Field'), 'description' => t('Countdown Timer Field.'), 'default_widget' => 'field_countdown_widget', 'default_formatter' => 'field_countdown_default', ), ); } /** * Implements hook_field_widget_info(). */ function field_countdown_field_widget_info() { return array( 'field_countdown_widget' => array( 'label' => t('Countdown Timer'), 'field types' => array('field_countdown'), ), ); } /** * Implements hook_field_widget_form(). */ function field_countdown_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { $base = $element; $settings = $instance['settings']; $element += array( '#type' => 'fieldset', ); $element['#attached']['css'] = array( drupal_get_path('module', 'field_countdown') . '/css/field_countdown.css', ); $default_date = NULL; if (isset($items[$delta]['countdown_timer'])) { if (is_numeric($items[$delta]['countdown_timer'])) { $default_date = date('Y-m-d H:i', $items[$delta]['countdown_timer']); } else { $default_date = $items[$delta]['countdown_timer']; } } $element['countdown_timer'] = array( '#type' => 'date_popup', '#title' => check_plain($element['#title']), '#date_format' => 'Y-m-d H:i', '#date_year_range' => '-3:+3', '#description' => check_plain($element['#description']), '#default_value' => $default_date, '#attributes' => array('class' => array('countdown_timer_field')), '#required' => isset($element['#required']) ? $element['#required'] : FALSE, ); return $element; } /** * Implements hook_field_widget_settings_form(). */ function field_countdown_field_widget_settings_form($field, $instance) { $widget = $instance['widget']; $settings = $widget['settings']; $form['font_size'] = array( '#type' => 'textfield', '#title' => t('Font Size'), '#description' => t('Size of the timer will depend upon this value.'), '#default_value' => isset($settings['font_size']) ? $settings['font_size'] : NULL, '#required' => TRUE, '#element_validate' => array('_element_validate_integer_positive'), ); return $form; } /** * Implements hook_field_validate(). */ function field_countdown_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) { foreach ($items as $delta => $value) { if (!empty($value['countdown_timer'])) { if (!is_array($value['countdown_timer'])) { $entered_time = strtotime($value['countdown_timer']); if ($entered_time <= time()) { $errors[$field['field_name']][$langcode][$delta][] = array( 'error' => 'value', 'message' => t("The date and time should greater than current date and time."), ); } } } } } /** * Implements hook_field_is_empty(). */ function field_countdown_field_is_empty($item, $field) { return FALSE; } /** * Implements hook_field_presave(). */ function field_countdown_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) { foreach ($items as $delta => $value) { _field_countdown_process($items[$delta], $field, $entity, $delta); } } /** * Prepares the item data for storage. */ function _field_countdown_process(&$item, $field, $entity, $delta = 0) { $nid = NULL; if ($item['countdown_timer']) { if (!is_numeric($item['countdown_timer'])) { $item['countdown_timer'] = strtotime(check_plain($item['countdown_timer'])); } } } /** * Implements hook_field_formatter_info(). */ function field_countdown_field_formatter_info() { return array( 'field_countdown_default' => array( 'label' => t('jQuery Countdown Timer without text timer'), 'field types' => array('field_countdown'), 'multiple values' => FIELD_BEHAVIOR_DEFAULT, ), 'field_countdown_with_text_timer' => array( 'label' => t('jQuery Countdown Timer with text timer'), 'field types' => array('field_countdown'), 'multiple values' => FIELD_BEHAVIOR_DEFAULT, ), 'field_countdown_plain' => array( 'label' => t('Date and time as string'), 'field types' => array('field_countdown'), 'multiple values' => FIELD_BEHAVIOR_DEFAULT, ), 'field_countdown_integer' => array( 'label' => t('Unix time stamp'), 'field types' => array('field_countdown'), 'multiple values' => FIELD_BEHAVIOR_DEFAULT, ), ); } /** * Implements hook_theme(). */ function field_countdown_theme() { return array( 'field_countdown_timer' => array( 'variables' => array(), ), ); } /** * Implements hook_field_formatter_view(). */ function field_countdown_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { $element = array(); $countdown_field_count = &drupal_static(__FUNCTION__); if (empty($countdown_field_count)) { $countdown_field_count = 0; } switch ($display['type']) { case 'field_countdown_default': foreach ($items as $delta => $item) { if (time() < $item['countdown_timer']) { $element[$delta]['#markup'] = _field_countdown_display_timer( check_plain($item['countdown_timer']), $instance['widget']['settings']['font_size'], $countdown_field_count, FALSE); $countdown_field_count++; } } break; case 'field_countdown_with_text_timer': foreach ($items as $delta => $item) { if (time() < $item['countdown_timer']) { $element[$delta]['#markup'] = _field_countdown_display_timer( check_plain($item['countdown_timer']), $instance['widget']['settings']['font_size'], $countdown_field_count, TRUE); $countdown_field_count++; } } break; case 'field_countdown_plain': foreach ($items as $delta => $item) { $element[$delta]['#markup'] = date('Y-m-d H:i', check_plain( $item['countdown_timer']) ); } break; case 'field_countdown_integer': foreach ($items as $delta => $item) { $element[$delta]['#markup'] = check_plain($item['countdown_timer']); } break; } return $element; } /** * Creates the jquery countdown timer. */ function _field_countdown_display_timer($time, $font_size, $countdown_field_count, $text_timer = FALSE) { $path = libraries_get_path('jquery-countdown'); drupal_add_js($path . '/assets/countdown/jquery.countdown.js', array( 'type' => 'file', 'scope' => 'footer', ) ); $suffix = str_replace(array('.', ' '), array('-', '-'), microtime()); $settings = array( 'coundownSetting' . $countdown_field_count => array( 'coundownSetting_suffix' => $suffix, 'coundownSetting_time' => $time, ), ); drupal_add_js(array('field_countdown' => $settings), 'setting'); drupal_add_js(drupal_get_path('module', 'field_countdown') . '/field_countdown.js', array( 'scope' => 'footer', 'weight' => 5, )); drupal_add_css($path . '/assets/countdown/jquery.countdown.css'); drupal_add_css( '.countdownHolder {font-size: ' . $font_size . 'px}', 'inline' ); $content = theme('field_countdown_timer', array( 'text_timer' => $text_timer, 'suffix' => $suffix, ) ); return $content; } /** * Returns HTML for the timer container. */ function theme_field_countdown_timer($variables) { $output = ''; $output .= "