class FieldTimerCountdownFormatter in Field Timer 8
Same name and namespace in other branches
- 2.x src/Plugin/Field/FieldFormatter/FieldTimerCountdownFormatter.php \Drupal\field_timer\Plugin\Field\FieldFormatter\FieldTimerCountdownFormatter
Plugin implementation of the 'field_timer_countdown' formatter.
Plugin annotation
@FieldFormatter(
id = "field_timer_countdown",
label = @Translation("jQuery Countdown"),
field_types = {
"datetime"
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Field\FormatterBase implements FormatterInterface, ContainerFactoryPluginInterface
- class \Drupal\field_timer\Plugin\Field\FieldFormatter\FieldTimerJsFormatterBase
- class \Drupal\field_timer\Plugin\Field\FieldFormatter\FieldTimerCountdownFormatterBase
- class \Drupal\field_timer\Plugin\Field\FieldFormatter\FieldTimerCountdownFormatter implements ContainerFactoryPluginInterface
- class \Drupal\field_timer\Plugin\Field\FieldFormatter\FieldTimerCountdownFormatterBase
- class \Drupal\field_timer\Plugin\Field\FieldFormatter\FieldTimerJsFormatterBase
- class \Drupal\Core\Field\FormatterBase implements FormatterInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of FieldTimerCountdownFormatter
File
- src/
Plugin/ Field/ FieldFormatter/ FieldTimerCountdownFormatter.php, line 25
Namespace
Drupal\field_timer\Plugin\Field\FieldFormatterView source
class FieldTimerCountdownFormatter extends FieldTimerCountdownFormatterBase implements ContainerFactoryPluginInterface {
/**
* Provides language.default service.
*
* {@inheritdoc}
*/
const JS_KEY = 'jquery.countdown';
/**
* Provides language.default service.
*
* @var \Drupal\Core\Language\LanguageDefault
*/
protected $languageDefault;
/**
* {@inheritdoc}
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, TimeInterface $time, LanguageDefault $languageDefault) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $time);
$this->languageDefault = $languageDefault;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('datetime.time'), $container
->get('language.default'));
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$settings = [
'use_system_language' => FALSE,
'regional' => 'en',
'format' => 'dHMS',
'layout' => '',
'compact' => 0,
'significant' => 0,
'timeSeparator' => ':',
'padZeroes' => 0,
] + parent::defaultSettings();
return $settings;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
$language = $this
->getLanguage($langcode);
if ($language != 'en') {
$elements['#attached']['library'][] = 'field_timer/' . static::LIBRARY_NAME . '.' . $language;
}
$keys = $this
->getItemKeys($items);
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#markup' => '<span class="field-timer-jquery-countdown" data-field-timer-key="' . $keys[$delta] . '" data-timestamp="' . $this
->getTimestamp($item) . '"></span>',
];
}
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$useSystemLanguageDescription = $this
->t('If this option is checked, it will try to ' . 'use appropriate translation from internal files and fallback to the ' . 'site\'s default language or English if nothing is found. Otherwise it ' . 'provides option \'Region\' to configure which translation to use for ' . 'each language on the site.');
$form['use_system_language'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use system language'),
'#default_value' => $this
->getSetting('use_system_language'),
'#description' => $useSystemLanguageDescription,
'#attributes' => [
'class' => [
'field-timer-use-system-language',
],
],
];
$form['regional'] = [
'#type' => 'select',
'#title' => $this
->t('Language'),
'#default_value' => $this
->getSetting('regional'),
'#options' => $this
->languageOptions(),
'#states' => [
'invisible' => [
'input.field-timer-use-system-language' => [
'checked' => TRUE,
],
],
],
];
$form['format'] = [
'#type' => 'textfield',
'#title' => $this
->t('Format'),
'#default_value' => $this
->getSetting('format'),
'#description' => $this
->t('See <a href=":url" target="_blank">documentation</a> for this parameter.', [
':url' => $this
->getDocumentationLink([
'fragment' => 'format',
]),
]),
];
$form['layout'] = [
'#type' => 'textarea',
'#rows' => 3,
'#cols' => 60,
'#title' => $this
->t('Layout'),
'#default_value' => $this
->getSetting('layout'),
'#description' => $this
->t('See <a href=":url" target="_blank">documentation</a> for this parameter.', [
':url' => $this
->getDocumentationLink([
'fragment' => 'layout',
]),
]),
];
$form['compact'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Display in compact format'),
'#default_value' => $this
->getSetting('compact'),
];
$form['significant'] = [
'#type' => 'select',
'#title' => $this
->t('Granularity'),
'#options' => range(0, 7),
'#default_value' => $this
->getSetting('significant'),
];
$form['timeSeparator'] = [
'#type' => 'textfield',
'#title' => $this
->t('Time separator'),
'#default_value' => $this
->getSetting('timeSeparator'),
];
$form['padZeroes'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Pad with zeroes'),
'#default_value' => $this
->getSetting('padZeroes'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$useSystemLanguage = $this
->getSetting('use_system_language');
$summary[] = $this
->t('Use system language: %use_system_language', [
'%use_system_language' => $useSystemLanguage ? $this
->t('Yes') : $this
->t('No'),
]);
if (!$useSystemLanguage) {
$language = $this
->getSetting('regional');
$summary[] = $this
->t('Language: %language', [
'%language' => $this
->languageOptions()[$language],
]);
}
$summary[] = $this
->t('Format: %format', [
'%format' => $this
->getSetting('format'),
]);
$summary[] = $this
->t('Layout: %layout', [
'%layout' => $this
->getSetting('layout'),
]);
$summary[] = $this
->t('Compact: %compact', [
'%compact' => $this
->getSetting('compact') ? $this
->t('Yes') : $this
->t('No'),
]);
$summary[] = $this
->t('Granularity: %significant', [
'%significant' => $this
->getSetting('significant'),
]);
$summary[] = $this
->t('Time separator: %timeSeparator', [
'%timeSeparator' => $this
->getSetting('timeSeparator'),
]);
$summary[] = $this
->t('Pad with zeroes: %padZeroes', [
'%padZeroes' => $this
->getSetting('padZeroes') ? $this
->t('Yes') : $this
->t('No'),
]);
return $summary;
}
/**
* {@inheritdoc}
*/
protected function preparePluginSettings(FieldItemInterface $item, $langcode) {
$settings = parent::preparePluginSettings($item, $langcode);
unset($settings['use_system_language']);
$settings['regional'] = $this
->getLanguage($langcode);
return $settings;
}
/**
* Gets language to use for jquery.countdown.
*
* @param string $langcode
* Language code.
*
* @return string
* Language code.
*/
protected function getLanguage($langcode) {
// Fallback to English.
$language = 'en';
if ($this
->getSetting('use_system_language')) {
$languages = $this
->languageOptions();
// Try content language.
if (isset($languages[$langcode])) {
$language = $langcode;
}
else {
$defaultLangcode = $this->languageDefault
->get()
->getId();
// Try default language.
if (isset($languages[$defaultLangcode])) {
$language = $defaultLangcode;
}
}
}
else {
$language = $this
->getSetting('regional');
}
return $language;
}
/**
* Gets language options.
*
* @return array
* Array of language options.
*/
protected function languageOptions() {
return [
'sq' => t('Albanian'),
'ar' => t('Arabic'),
'hy' => t('Armenian'),
'bn' => t('Bengali/Bangla'),
'bs' => t('Bosnian'),
'bg' => t('Bulgarian'),
'my' => t('Burmese'),
'ca' => t('Catalan'),
'zh-CN' => t('Chinese/Simplified'),
'zh-TW' => t('Chinese/Traditional'),
'hr' => t('Croatian'),
'cs' => t('Czech'),
'da' => t('Danish'),
'nl' => t('Dutch'),
'et' => t('Estonian'),
'en' => t('English'),
'fa' => t('Farsi/Persian'),
'fi' => t('Finnish'),
'fo' => t('Faroese'),
'fr' => t('French'),
'gl' => t('Galician'),
'de' => t('German'),
'el' => t('Greek'),
'gu' => t('Gujarati'),
'he' => t('Hebrew'),
'hu' => t('Hungarian'),
'id' => t('Indonesian'),
'is' => t('Icelandic'),
'it' => t('Italian'),
'ja' => t('Japanese'),
'kn' => t('Kannada'),
'ko' => t('Korean'),
'lv' => t('Latvian'),
'lt' => t('Lithuanian'),
'mk' => t('Macedonian'),
'ml' => t('Malayalam'),
'ms' => t('Malaysian'),
'nb' => t('Norvegian'),
'pl' => t('Polish'),
'pt-BR' => t('Portuguese/Brazilian'),
'ro' => t('Romanian'),
'ru' => t('Russian'),
'sr' => t('Serbian'),
'sk' => t('Slovak'),
'sl' => t('Slovenian'),
'es' => t('Spanish'),
'sv' => t('Swedish'),
'th' => t('Thai'),
'tr' => t('Turkish'),
'uk' => t('Ukrainian'),
'ur' => t('Urdu'),
'uz' => t('Uzbek'),
'vi' => t('Vietnamese'),
'cy' => t('Welsh'),
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FieldTimerCountdownFormatter:: |
protected | property | Provides language.default service. | |
FieldTimerCountdownFormatter:: |
public static | function |
Creates an instance of the plugin. Overrides FieldTimerCountdownFormatterBase:: |
|
FieldTimerCountdownFormatter:: |
public static | function |
Defines the default settings for this plugin. Overrides FieldTimerCountdownFormatterBase:: |
|
FieldTimerCountdownFormatter:: |
protected | function | Gets language to use for jquery.countdown. | |
FieldTimerCountdownFormatter:: |
constant |
Provides language.default service. Overrides FieldTimerJsFormatterBase:: |
||
FieldTimerCountdownFormatter:: |
protected | function | Gets language options. | |
FieldTimerCountdownFormatter:: |
protected | function |
Prepares array of settings used to initialize jQuery plugin. Overrides FieldTimerCountdownFormatterBase:: |
|
FieldTimerCountdownFormatter:: |
public | function |
Returns a form to configure settings for the formatter. Overrides FieldTimerCountdownFormatterBase:: |
|
FieldTimerCountdownFormatter:: |
public | function |
Returns a short summary for the current formatter settings. Overrides FieldTimerCountdownFormatterBase:: |
|
FieldTimerCountdownFormatter:: |
public | function |
Builds a renderable array for a field value. Overrides FieldTimerJsFormatterBase:: |
|
FieldTimerCountdownFormatter:: |
public | function |
FieldTimerCountdownFormatterBase constructor. Overrides FieldTimerCountdownFormatterBase:: |
|
FieldTimerCountdownFormatterBase:: |
protected | property | ||
FieldTimerCountdownFormatterBase:: |
protected | function | ||
FieldTimerCountdownFormatterBase:: |
constant |
Plugin name used to render timer/countdown widget. Overrides FieldTimerJsFormatterBase:: |
||
FieldTimerCountdownFormatterBase:: |
protected | function | ||
FieldTimerCountdownFormatterBase:: |
constant | Formatter types. | ||
FieldTimerJsFormatterBase:: |
protected | property | Stores set of unique html ids for current items. | |
FieldTimerJsFormatterBase:: |
protected | function | Generates JS settings for the field. | |
FieldTimerJsFormatterBase:: |
protected | function | Generates unique ids for the field items. | |
FieldTimerJsFormatterBase:: |
protected | function | Retrieves timestamp from field item. | |
FormatterBase:: |
protected | property | The field definition. | |
FormatterBase:: |
protected | property | The label display setting. | |
FormatterBase:: |
protected | property |
The formatter settings. Overrides PluginSettingsBase:: |
|
FormatterBase:: |
protected | property | The view mode. | |
FormatterBase:: |
protected | function | Returns the value of a field setting. | |
FormatterBase:: |
protected | function | Returns the array of field settings. | |
FormatterBase:: |
public static | function |
Returns if the formatter can be used for the provided field. Overrides FormatterInterface:: |
14 |
FormatterBase:: |
public | function |
Allows formatters to load information for field values being displayed. Overrides FormatterInterface:: |
2 |
FormatterBase:: |
public | function |
Builds a renderable array for a fully themed field. Overrides FormatterInterface:: |
1 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginSettingsBase:: |
protected | property | Whether default settings have been merged into the current $settings. | |
PluginSettingsBase:: |
protected | property | The plugin settings injected by third party modules. | |
PluginSettingsBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
6 |
PluginSettingsBase:: |
public | function |
Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
protected | function | Merges default settings values into $settings. | |
PluginSettingsBase:: |
public | function |
Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface:: |
3 |
PluginSettingsBase:: |
public | function |
Sets the value of a setting for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the settings for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Unsets a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |