class Countdown in Countdown 8
Provides a 'Countdown' Block.
Plugin annotation
@Block(
id = "countdown_block",
admin_label = @Translation("Countdown block"),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Component\Plugin\ContextAwarePluginBase implements ContextAwarePluginInterface
- class \Drupal\Core\Plugin\ContextAwarePluginBase implements CacheableDependencyInterface, ContextAwarePluginInterface uses DependencySerializationTrait, StringTranslationTrait, TypedDataTrait
- class \Drupal\Core\Block\BlockBase implements BlockPluginInterface, PluginWithFormsInterface, PreviewFallbackInterface uses BlockPluginTrait, ContextAwarePluginAssignmentTrait
- class \Drupal\countdown\Plugin\Block\Countdown
- class \Drupal\Core\Block\BlockBase implements BlockPluginInterface, PluginWithFormsInterface, PreviewFallbackInterface uses BlockPluginTrait, ContextAwarePluginAssignmentTrait
- class \Drupal\Core\Plugin\ContextAwarePluginBase implements CacheableDependencyInterface, ContextAwarePluginInterface uses DependencySerializationTrait, StringTranslationTrait, TypedDataTrait
- class \Drupal\Component\Plugin\ContextAwarePluginBase implements ContextAwarePluginInterface
Expanded class hierarchy of Countdown
1 string reference to 'Countdown'
File
- src/
Plugin/ Block/ countdown.php, line 19
Namespace
Drupal\countdown\Plugin\BlockView source
class Countdown extends BlockBase {
/**
* Implements a block render.
*/
public function build() {
$config = $this
->getConfiguration();
$time = time();
$event_name = isset($config['event_name']) ? $config['event_name'] : '';
$url = isset($config['url']) ? $config['url'] : '';
if ($url != "") {
if (UrlHelper::isExternal($url)) {
$event_name = Link::fromTextAndUrl($event_name, Url::fromUri($url, []))
->toString();
}
else {
if ($url == "<front>") {
$url = "/";
}
$event_name = Link::fromTextAndUrl($event_name, Url::fromUri('internal:/' . ltrim($url, '/'), []))
->toString();
}
}
$accuracy = isset($config['accuracy']) ? $config['accuracy'] : '';
$countdown_timestamp = isset($config['timestamp']) ? $config['timestamp'] : $time;
$difference = $countdown_timestamp - $time;
if ($difference < 0) {
$passed = 1;
$difference = abs($difference);
}
else {
$passed = 0;
}
if ($passed) {
$event_name = $this
->t(' since @event_name.', [
'@event_name' => $event_name,
]);
}
else {
$event_name = $this
->t(' until @event_name.', [
'@event_name' => $event_name,
]);
}
$days_left = floor($difference / 60 / 60 / 24);
$hrs_left = floor(($difference - $days_left * 60 * 60 * 24) / 60 / 60);
$min_left = floor(($difference - $days_left * 60 * 60 * 24 - $hrs_left * 60 * 60) / 60);
$secs_left = floor($difference - $days_left * 60 * 60 * 24 - $hrs_left * 60 * 60 - $min_left * 60);
$days_left = $this
->formatPlural($days_left, '1 day', '@count days');
if ($accuracy == 'h' || $accuracy == 'm' || $accuracy == 's') {
$hrs_left = $this
->formatPlural($hrs_left, ', 1 hour', ', @count hours');
}
if ($accuracy == 'm' || $accuracy == 's') {
$min_left = $this
->formatPlural($min_left, ', 1 minute', ', @count minutes');
}
if ($accuracy == 's') {
$secs_left = $this
->formatPlural($secs_left, ', 1 second', ', @count seconds');
}
$build = [
'#theme' => 'countdown',
'#cache' => [
'max-age' => 0,
],
'#accuracy' => $accuracy,
'#countdown_url' => $url,
'#countdown_event_name' => $event_name,
'#days_left' => $days_left,
'#hrs_left' => $hrs_left,
'#min_left' => $min_left,
'#secs_left' => $secs_left,
'#attached' => [
'library' => 'countdown/countdownblock',
'drupalSettings' => [
'countdown' => [
'countdownblock' => [
'accuracy' => $accuracy,
],
],
],
],
];
return $build;
}
/**
* Implements a block form handler.
*/
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$time = time();
$timestamp = $time;
$event_name = '';
$countdown_url = '';
$countdown_accuracy = '';
// Retrieve existing configuration for this block.
$config = $this
->getConfiguration();
if (isset($config['event_name'])) {
$event_name = $config['event_name'];
$countdown_url = $config['url'];
$countdown_accuracy = $config['accuracy'];
$timestamp = $config['timestamp'];
}
$form['countdown_event_name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Event Name'),
'#default_value' => $event_name,
'#size' => 30,
'#maxlength' => 200,
'#description' => $this
->t("Event name you're counting to or from."),
'#required' => TRUE,
];
$form['countdown_url'] = [
'#type' => 'textfield',
'#title' => $this
->t('Event URL'),
'#default_value' => $countdown_url,
'#size' => 30,
'#maxlength' => 200,
'#description' => $this
->t('Turn the event description into a link to more
information about the event.
Start typing the title of a piece of content to select it.
You can also enter an internal path such as %add-node or an external URL
such as %url. Enter %front to link to the front page.', [
'%front' => '<front>',
'%add-node' => '/node/add',
'%url' => 'http://example.com',
]),
'#required' => FALSE,
];
$form['countdown_accuracy'] = [
'#type' => 'radios',
'#title' => $this
->t('Accuracy'),
'#default_value' => $countdown_accuracy,
'#options' => [
'd' => $this
->t('days'),
'h' => $this
->t('hours'),
'm' => $this
->t('minutes'),
's' => $this
->t('seconds'),
],
'#description' => $this
->t('Select the smallest amount of detail to display. For example, selecting "days" will display only days, selecting "hours" will display the number of days and hours.'),
];
$form['target_time'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Target date/time'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#description' => $this
->t('Select a date relative to the server time: %s', [
'%s' => \Drupal::service('date.formatter')
->format($time),
]),
];
for ($years = [], $i = 1970; $i < 2032; $years[$i] = $i, $i++) {
}
$form['target_time']['year'] = [
'#type' => 'select',
'#title' => $this
->t('Year'),
'#default_value' => (int) date('Y', $timestamp),
'#options' => $years,
];
unset($years);
$form['target_time']['month'] = [
'#type' => 'select',
'#title' => $this
->t('Month'),
'#default_value' => (int) date('n', $timestamp),
'#options' => [
1 => $this
->t('January'),
2 => $this
->t('February'),
3 => $this
->t('March'),
4 => $this
->t('April'),
5 => $this
->t('May'),
6 => $this
->t('June'),
7 => $this
->t('July'),
8 => $this
->t('August'),
9 => $this
->t('September'),
10 => $this
->t('October'),
11 => $this
->t('November'),
12 => $this
->t('December'),
],
];
for ($month_days = [], $i = 1; $i < 32; $month_days[$i] = $i, $i++) {
}
$form['target_time']['day'] = [
'#type' => 'select',
'#title' => $this
->t('Day'),
'#default_value' => (int) date('j', $timestamp),
'#options' => $month_days,
];
unset($month_days);
for ($hrs = [], $i = 0; $i < 24; $hrs[] = $i, $i++) {
}
$form['target_time']['hour'] = [
'#type' => 'select',
'#title' => $this
->t('Hour'),
'#default_value' => (int) date('G', $timestamp),
'#options' => $hrs,
];
unset($hrs);
for ($mins = [], $i = 0; $i < 60; $mins[] = $i, $i++) {
}
$form['target_time']['min'] = [
'#type' => 'select',
'#title' => $this
->t('Minute'),
'#default_value' => (int) date('i', $timestamp),
'#options' => $mins,
];
$form['target_time']['sec'] = [
'#type' => 'select',
'#title' => $this
->t('Seconds'),
'#default_value' => (int) date('s', $timestamp),
'#options' => $mins,
];
return $form;
}
/**
* Implements a block submit handler.
*
* Save configuration into system.
*/
public function blockSubmit($form, FormStateInterface $form_state) {
// Save our custom settings when the form is submitted.
$this
->setConfigurationValue('event_name', $form_state
->getValue('countdown_event_name'));
$this
->setConfigurationValue('url', $form_state
->getValue('countdown_url'));
$this
->setConfigurationValue('accuracy', $form_state
->getValue('countdown_accuracy'));
$timestamp = $form_state
->getValue('target_time');
$countdown_timestamp = mktime((int) $timestamp['hour'], (int) $timestamp['min'], (int) $timestamp['sec'], (int) $timestamp['month'], (int) $timestamp['day'], (int) $timestamp['year']);
$this
->setConfigurationValue('timestamp', $countdown_timestamp);
}
/**
* Implements form validation.
*
* The validateForm method is the default method called to validate input on
* a form.
*
* @param array $form
* The render array of the currently built form.
* @param FormStateInterface $form_state
* Object describing the current state of the form.
*/
public function blockValidate($form, FormStateInterface $form_state) {
$url = $form_state
->getValue('countdown_url');
if ($url != "" || $url == "<front>") {
if (!UrlHelper::isExternal($url) && !(strpos($url, '/') === 0)) {
$form_state
->setErrorByName('countdown_url', $this
->t('Event URL is not vaild. Entered paths should start with /'));
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BlockPluginInterface:: |
constant | Indicates the block label (title) should be displayed to end users. | ||
BlockPluginTrait:: |
protected | property | The transliteration service. | |
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
protected | function | Returns generic default configuration for block plugins. | |
BlockPluginTrait:: |
protected | function | Indicates whether the block should be shown. | 16 |
BlockPluginTrait:: |
public | function | Creates a generic configuration form for all block types. Individual block plugins can add elements to this form by overriding BlockBase::blockForm(). Most block plugins should not override this method unless they need to alter the generic form elements. | 2 |
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
public | function | 19 | |
BlockPluginTrait:: |
public | function | 1 | |
BlockPluginTrait:: |
public | function | 1 | |
BlockPluginTrait:: |
public | function | 3 | |
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
public | function | Sets the transliteration service. | |
BlockPluginTrait:: |
public | function | Most block plugins should not override this method. To add submission handling for a specific block type, override BlockBase::blockSubmit(). | |
BlockPluginTrait:: |
protected | function | Wraps the transliteration service. | |
BlockPluginTrait:: |
public | function | Most block plugins should not override this method. To add validation for a specific block type, override BlockBase::blockValidate(). | 1 |
BlockPluginTrait:: |
public | function | 22 | |
ContextAwarePluginAssignmentTrait:: |
protected | function | Builds a form element for assigning a context to a given slot. | |
ContextAwarePluginAssignmentTrait:: |
protected | function | Wraps the context handler. | |
ContextAwarePluginBase:: |
protected | property | The data objects representing the context of this plugin. | |
ContextAwarePluginBase:: |
private | property | Data objects representing the contexts passed in the plugin configuration. | |
ContextAwarePluginBase:: |
protected | function |
Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
The cache contexts associated with this object. Overrides CacheableDependencyInterface:: |
9 |
ContextAwarePluginBase:: |
public | function |
The maximum age for which this object may be cached. Overrides CacheableDependencyInterface:: |
7 |
ContextAwarePluginBase:: |
public | function |
The cache tags associated with this object. Overrides CacheableDependencyInterface:: |
4 |
ContextAwarePluginBase:: |
public | function |
This code is identical to the Component in order to pick up a different
Context class. Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Gets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Gets the defined contexts. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Gets the value for a defined context. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Gets the values for all defined contexts. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Set a context on this plugin. Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Sets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Sets the value for a defined context. Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Validates the set values for the defined contexts. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function | Implements magic __get() method. | |
Countdown:: |
public | function |
Implements a block form handler. Overrides BlockPluginTrait:: |
|
Countdown:: |
public | function |
Implements a block submit handler. Overrides BlockPluginTrait:: |
|
Countdown:: |
public | function |
Implements form validation. Overrides BlockPluginTrait:: |
|
Countdown:: |
public | function |
Implements a block render. Overrides BlockPluginInterface:: |
|
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 | |
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. | |
PluginWithFormsTrait:: |
public | function | ||
PluginWithFormsTrait:: |
public | function | ||
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. | |
TypedDataTrait:: |
protected | property | The typed data manager used for creating the data types. | |
TypedDataTrait:: |
public | function | Gets the typed data manager. | 2 |
TypedDataTrait:: |
public | function | Sets the typed data manager. | 2 |