xandeadx

Fable 5 (paid)

Jul 26th, 2026
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.66 KB | None | 0 0
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace Drupal\current_date\Plugin\Block;
  6.  
  7. use Drupal\Core\Block\Attribute\Block;
  8. use Drupal\Core\Block\BlockBase;
  9. use Drupal\Core\Datetime\DrupalDateTime;
  10. use Drupal\Core\Form\FormStateInterface;
  11. use Drupal\Core\StringTranslation\TranslatableMarkup;
  12.  
  13. /**
  14.  * Provides a block that displays the current date.
  15.  */
  16. #[Block(
  17.  id: 'current_date_block',
  18.   admin_label: new TranslatableMarkup('Current date'),
  19.   category: new TranslatableMarkup('Custom'),
  20. )]
  21. final class CurrentDateBlock extends BlockBase {
  22.  
  23.   /**
  24.    * {@inheritdoc}
  25.    */
  26.   public function defaultConfiguration(): array {
  27.     return [
  28.       'date_format' => 'd.m.Y',
  29.     ] + parent::defaultConfiguration();
  30.   }
  31.  
  32.   /**
  33.    * {@inheritdoc}
  34.    */
  35.   public function blockForm($form, FormStateInterface $form_state): array {
  36.     $form['date_format'] = [
  37.       '#type' => 'textfield',
  38.       '#title' => $this->t('Date format'),
  39.       '#description' => $this->t('A PHP date format string. Example: %example.', [
  40.         '%example' => 'd.m.Y H:i',
  41.       ]),
  42.       '#default_value' => $this->configuration['date_format'],
  43.       '#required' => TRUE,
  44.     ];
  45.  
  46.     return $form;
  47.   }
  48.  
  49.   /**
  50.    * {@inheritdoc}
  51.    */
  52.   public function blockSubmit($form, FormStateInterface $form_state): void {
  53.     $this->configuration['date_format'] = $form_state->getValue('date_format');
  54.   }
  55.  
  56.   /**
  57.    * {@inheritdoc}
  58.    */
  59.   public function build(): array {
  60.     $formatted = (new DrupalDateTime('now'))->format($this->configuration['date_format']);
  61.  
  62.     return [
  63.       '#markup' => $formatted,
  64.       '#cache' => [
  65.         'max-age' => 0,
  66.       ],
  67.     ];
  68.   }
  69.  
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment