Newer
Older
<?php
namespace Drupal\flipclock\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Form\FormStateInterface;
/**
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
* @Block(
* id = "flipclock_block",
* admin_label = @Translation("Clock"),
* )
*/
class FlipClockBlock extends BlockBase implements BlockPluginInterface {
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$config = $this->getConfiguration();
$date = new DrupalDateTime($config['date']);
$date->setTimezone(new \DateTimeZone(drupal_get_user_timezone()));
$form['date'] = [
'#type' => 'datetime',
'#title' => $this->t('Date'),
'#date_increment' => 1,
'#date_timezone' => drupal_get_user_timezone(),
'#description' => $this->t('The date you want to initialize the clock with.'),
'#required' => TRUE,
'#default_value' => isset($config['date']) ? $date : NULL,
];
$form['clock_face'] = [
'#type' => 'select',
'#title' => $this->t('Clock'),
'#options' => [
'HourlyCounter' => $this->t('Hourly Counter'),
'MinuteCounter' => $this->t('Minute Counter'),
'DailyCounter' => $this->t('Daily Counter'),
'TwelveHourClock' => $this->t('12hr Clock'),
'TwentyFourHourClock' => $this->t('24hr Clock'),
],
'#description' => $this->t('This is the name of the clock that is used to build the clock display.'),
'#required' => TRUE,
'#default_value' => $config['clock_face'],
];
$form['auto_play'] = [
'#type' => 'checkbox',
'#title' => $this->t('Auto play'),
'#description' => $this->t('Indicate if the clock should automatically add the play class to start the animation.'),
'#default_value' => isset($config['auto_play']) ? $config['auto_play'] : TRUE,
];
$form['auto_start'] = [
'#type' => 'checkbox',
'#title' => $this->t('Auto start'),
'#description' => $this->t('Indicate if the clock should start automatically.'),
'#default_value' => isset($config['auto_start']) ? $config['auto_start'] : TRUE,
];
$form['countdown'] = [
'#type' => 'checkbox',
'#title' => $this->t('Countdown'),
'#description' => $this->t('Indicate if the clock will count down instead of up.'),
'#default_value' => $config['countdown'],
];
$form['show_seconds'] = [
'#type' => 'checkbox',
'#title' => $this->t('Show seconds'),
'#description' => $this->t('Indicate if the clock should include a seconds display.'),
'#default_value' => isset($config['show_seconds']) ? $config['show_seconds'] : TRUE,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Datetime\DrupalDateTime $date */
$date = $form_state->getValue('date');
$this->setConfigurationValue('date', $date->format(DATETIME_DATETIME_STORAGE_FORMAT));
$this->setConfigurationValue('clock_face', $form_state->getValue('clock_face'));
$this->setConfigurationValue('auto_play', $form_state->getValue('auto_play'));
$this->setConfigurationValue('auto_start', $form_state->getValue('auto_start'));
$this->setConfigurationValue('countdown', $form_state->getValue('countdown'));
$this->setConfigurationValue('show_seconds', $form_state->getValue('show_seconds'));
}
/**
* {@inheritdoc}
*/
public function build() {
$id = 'flipclock-' . uniqid();
$instance = [
'timestamp' => (int) strtotime($this->configuration['date']),
'options' => [
'clockFace' => $this->configuration['clock_face'],
'autoPlay' => ($this->configuration['auto_play']) ? TRUE : FALSE,
'autoStart' => ($this->configuration['auto_start']) ? TRUE : FALSE,
'countdown' => ($this->configuration['countdown']) ? TRUE : FALSE,
'showSeconds' => ($this->configuration['show_seconds']) ? TRUE : FALSE,
],
];
$build = [
'#theme' => 'flipclock',
'#id' => $id,
'#attached' => [
'library' => ['flipclock/flipclock.load'],
],
];
$build['#attached']['drupalSettings']['flipClock']['instances'][$id] = $instance;
return $build;
}