abstract class field_timer_text_base in Field Timer 7.2
Base class for field_timer_text formatter.
Hierarchy
- class \field_timer_formatter_base implements field_timer_formatter_interface
- class \field_timer_text_base
Expanded class hierarchy of field_timer_text_base
File
- includes/
field_timer_text.inc, line 11 - Contains help classes to perform field_timer_text formatter related actions.
View source
abstract class field_timer_text_base extends field_timer_formatter_base {
/**
* @inheritdoc
*/
public function enableFormatter() {
return TRUE;
}
/**
* @inheritdoc
*/
public final function getFormatterDefinition() {
return array(
'label' => t('Text'),
'description' => t('Simple text timer and countdown.'),
);
}
/**
* @inheritdoc
*/
public function getDefaultSettings() {
return array(
'type' => 'auto',
'granularity_type' => 'auto',
'granularity_auto' => 2,
'granularity_manual' => array(
'day' => 'day',
),
);
}
/**
* @inheritdoc
*/
public function getSettingsSummary($settings, $field) {
$summary = array();
$types = $this
->formatterTypes();
$granularity_options = $this
->granularitySettingsSummary($settings['granularity_type'], $settings['granularity_auto'], $settings['granularity_manual']);
$summary[] = t('Type: %type', array(
'%type' => $types[$settings['type']],
));
$summary[] = t('Granularity: %granularity_type (%granularity)', $granularity_options);
return $summary;
}
/**
* @inheritdoc
*/
public function getSettingsForm($settings, $field) {
$settings_form = array();
$settings_form['type'] = array(
'#type' => 'select',
'#title' => t('Type'),
'#options' => $this
->formatterTypes(),
'#default_value' => $settings['type'],
);
$settings_form['granularity_type'] = array(
'#type' => 'select',
'#title' => t('Granularity'),
'#options' => $this
->granularityTypes(),
'#default_value' => $settings['granularity_type'],
'#attributes' => array(
'class' => array(
'field-timer-text-granularity-type',
),
),
);
$settings_form['granularity_auto'] = array(
'#type' => 'select',
'#options' => $this
->formatterGranularityAuto(),
'#default_value' => $settings['granularity_auto'],
'#states' => array(
'visible' => array(
'select.field-timer-text-granularity-type' => array(
'value' => 'auto',
),
),
),
);
$settings_form['granularity_manual'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#options' => $this
->formatterGranularityManual(),
'#default_value' => $settings['granularity_manual'],
'#states' => array(
'visible' => array(
'select.field-timer-text-granularity-type' => array(
'value' => 'manual',
),
),
),
);
return $settings_form;
}
public function renderItems($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$elements = array();
$settings = $display['settings'];
$granularity_option = $settings['granularity_' . $settings['granularity_type']];
foreach ($items as $delta => $item) {
$elements[$delta] = array(
'#type' => 'markup',
'#markup' => $this
->renderItem($settings['type'], $settings['granularity_type'], $granularity_option, $this
->getTimestamp($item, $field)),
);
}
return $elements;
}
/**
* @inheritdoc
*/
public function getFormatterName() {
return 'field_timer_text';
}
/**
* Render field item.
*/
protected function renderItem($type, $granularity_type, $granularity_option, $timestamp) {
$time = '';
// Check what we should display - timer or countdown.
switch ($type) {
case 'auto':
$interval = REQUEST_TIME - $timestamp;
$sign = $interval > 0 ? '+' : '-';
$time = $sign . $this
->formatInterval(abs($interval), $granularity_type, $granularity_option);
break;
case 'timer':
$time = $this
->formatInterval(REQUEST_TIME - $timestamp, $granularity_type, $granularity_option);
break;
case 'countdown':
$time = $this
->formatInterval($timestamp - REQUEST_TIME, $granularity_type, $granularity_option);
break;
}
return $time;
}
/**
* Formats interval according to granularity settings.
*/
protected function formatInterval($interval, $granularity_type, $granularity_option) {
$result = '';
switch ($granularity_type) {
case 'auto':
$result = format_interval($interval, $granularity_option);
break;
case 'manual':
// @see function format_interval
$units = array(
'1 year|@count years' => 31536000,
'1 month|@count months' => 2592000,
'1 week|@count weeks' => 604800,
'1 day|@count days' => 86400,
'1 hour|@count hours' => 3600,
'1 min|@count min' => 60,
'1 sec|@count sec' => 1,
);
$granularity_options = array_keys($this
->formatterGranularityManual());
$output = '';
foreach ($units as $key => $value) {
if (isset($granularity_option[array_pop($granularity_options)])) {
$key = explode('|', $key);
if ($interval >= $value) {
$output .= ($output ? ' ' : '') . format_plural(floor($interval / $value), $key[0], $key[1]);
$interval %= $value;
}
}
}
$result = $output ? $output : (isset($key) ? format_plural(0, $key[0], $key[1]) : format_interval($interval));
break;
}
return $result;
}
protected function granularitySettingsSummary($granularity_type, $granularity_auto, $granularity_manual) {
$granularity_types = $this
->granularityTypes();
switch ($granularity_type) {
case 'auto':
$granularity = $granularity_auto;
break;
case 'manual':
$granularity = array();
$granularity_manual_options = $this
->formatterGranularityManual();
foreach ($granularity_manual as $key => $value) {
if ($value) {
$granularity[] = $granularity_manual_options[$key];
}
}
$granularity = implode(', ', $granularity);
break;
}
return array(
'%granularity_type' => $granularity_types[$granularity_type],
'%granularity' => $granularity,
);
}
protected function formatterTypes() {
return array(
'auto' => t('Auto'),
'timer' => t('Timer'),
'countdown' => t('Countdown'),
);
}
protected function granularityTypes() {
return array(
'auto' => t('Auto'),
'manual' => t('Manual'),
);
}
protected function formatterGranularityManual() {
return array(
'second' => t('Second'),
'minute' => t('Minute'),
'hour' => t('Hour'),
'day' => t('Day'),
'week' => t('Week'),
'month' => t('Month'),
'year' => t('Year'),
);
}
protected function formatterGranularityAuto() {
$options = range(0, 7);
unset($options[0]);
return $options;
}
}