Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- namespace Drupal\current_date_block\Plugin\Block;
- use Drupal\Core\Block\Attribute\Block;
- use Drupal\Core\Block\BlockBase;
- use Drupal\Core\Form\FormStateInterface;
- use Drupal\Core\StringTranslation\TranslatableMarkup;
- /**
- * Provides a current date block with configurable format.
- */
- #[Block(
- id: 'current_date_block',
- admin_label: new TranslatableMarkup('Current Date Block'),
- category: new TranslatableMarkup('Custom'),
- )]
- final class CurrentDateBlock extends BlockBase {
- /**
- * {@inheritdoc}
- */
- public function defaultConfiguration(): array {
- return [
- 'block_label' => $this->t('Current Date'),
- 'date_format' => 'long',
- 'custom_date_format' => '',
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function blockForm($form, FormStateInterface $form_state): array {
- $form['block_label'] = [
- '#type' => 'textfield',
- '#title' => $this->t('Block title'),
- '#default_value' => $this->configuration['block_label'],
- '#required' => TRUE,
- '#description' => $this->t('The title displayed above the date.'),
- ];
- $form['date_format'] = [
- '#type' => 'select',
- '#title' => $this->t('Date format'),
- '#default_value' => $this->configuration['date_format'],
- '#options' => [
- 'short' => $this->t('Short (e.g. @format)', ['@format' => date('m/d/Y')]),
- 'medium' => $this->t('Medium (e.g. @format)', ['@format' => date('D, m/d/Y')]),
- 'long' => $this->t('Long (e.g. @format)', ['@format' => date('l, F j, Y')]),
- 'custom' => $this->t('Custom'),
- ],
- '#description' => $this->t('Select a predefined date format or choose custom.'),
- ];
- $form['custom_date_format'] = [
- '#type' => 'textfield',
- '#title' => $this->t('Custom date format'),
- '#default_value' => $this->configuration['custom_date_format'],
- '#description' => $this->t('A PHP date format string, e.g. <code>Y-m-d</code>, <code>d.m.Y H:i</code>. See <a href=":url">PHP date()</a> documentation.', [':url' => 'https://www.php.net/manual/datetime.format.php']),
- '#states' => [
- 'visible' => [
- ':input[name="settings[date_format]"]' => ['value' => 'custom'],
- ],
- 'required' => [
- ':input[name="settings[date_format]"]' => ['value' => 'custom'],
- ],
- ],
- ];
- return $form;
- }
- /**
- * {@inheritdoc}
- */
- public function blockValidate($form, FormStateInterface $form_state): void {
- if ($form_state->getValue('date_format') === 'custom') {
- $custom_format = $form_state->getValue('custom_date_format');
- if (empty($custom_format)) {
- $form_state->setErrorByName('custom_date_format', $this->t('Custom date format is required when "Custom" is selected.'));
- }
- }
- }
- /**
- * {@inheritdoc}
- */
- public function blockSubmit($form, FormStateInterface $form_state): void {
- $this->configuration['block_label'] = $form_state->getValue('block_label');
- $this->configuration['date_format'] = $form_state->getValue('date_format');
- $this->configuration['custom_date_format'] = $form_state->getValue('custom_date_format');
- }
- /**
- * {@inheritdoc}
- */
- public function build(): array {
- $label = $this->configuration['block_label'];
- $format_choice = $this->configuration['date_format'];
- $format_map = [
- 'short' => 'm/d/Y',
- 'medium' => 'D, m/d/Y',
- 'long' => 'l, F j, Y',
- ];
- $php_format = $format_choice === 'custom'
- ? $this->configuration['custom_date_format']
- : ($format_map[$format_choice] ?? 'l, F j, Y');
- $current_date = \Drupal::service('date.formatter')->format(
- \Drupal::time()->getRequestTime(),
- 'custom',
- $php_format,
- );
- return [
- '#theme' => 'current_date_block',
- '#label' => $label,
- '#date' => $current_date,
- '#cache' => [
- 'contexts' => ['timezone'],
- 'max-age' => 86400,
- ],
- ];
- }
- }
Advertisement