xandeadx

Kimi K3 (paid)

Jul 26th, 2026
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.53 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\current_date_block\Plugin\Block;
  4.  
  5. use Drupal\Component\Datetime\TimeInterface;
  6. use Drupal\Core\Block\Attribute\Block;
  7. use Drupal\Core\Block\BlockBase;
  8. use Drupal\Core\Datetime\DateFormatterInterface;
  9. use Drupal\Core\Entity\EntityTypeManagerInterface;
  10. use Drupal\Core\Form\FormStateInterface;
  11. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  12. use Drupal\Core\StringTranslation\TranslatableMarkup;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14.  
  15. /**
  16.  * Provides a block that displays the current date.
  17.  */
  18. #[Block(
  19.  id: 'current_date_block',
  20.   admin_label: new TranslatableMarkup('Current date'),
  21.   category: new TranslatableMarkup('System'),
  22. )]
  23. final class CurrentDateBlock extends BlockBase implements ContainerFactoryPluginInterface {
  24.  
  25.   /**
  26.    * Constructs a CurrentDateBlock object.
  27.    *
  28.    * @param array $configuration
  29.    *   A configuration array containing information about the plugin instance.
  30.    * @param string $plugin_id
  31.    *   The plugin ID for the plugin instance.
  32.    * @param mixed $plugin_definition
  33.    *   The plugin implementation definition.
  34.    * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
  35.    *   The date formatter service.
  36.    * @param \Drupal\Component\Datetime\TimeInterface $time
  37.    *   The time service.
  38.    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
  39.    *   The entity type manager service.
  40.    */
  41.   public function __construct(
  42.     array $configuration,
  43.     $plugin_id,
  44.     $plugin_definition,
  45.     protected readonly DateFormatterInterface $dateFormatter,
  46.     protected readonly TimeInterface $time,
  47.     protected readonly EntityTypeManagerInterface $entityTypeManager,
  48.   ) {
  49.     parent::__construct($configuration, $plugin_id, $plugin_definition);
  50.   }
  51.  
  52.   /**
  53.    * {@inheritdoc}
  54.    */
  55.   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
  56.     return new static(
  57.       $configuration,
  58.       $plugin_id,
  59.       $plugin_definition,
  60.       $container->get('date.formatter'),
  61.       $container->get('datetime.time'),
  62.       $container->get('entity_type.manager'),
  63.     );
  64.   }
  65.  
  66.   /**
  67.    * {@inheritdoc}
  68.    */
  69.   public function defaultConfiguration(): array {
  70.     return [
  71.       'date_format' => 'medium',
  72.       'custom_format' => 'd.m.Y',
  73.     ] + parent::defaultConfiguration();
  74.   }
  75.  
  76.   /**
  77.    * {@inheritdoc}
  78.    */
  79.   public function blockForm($form, FormStateInterface $form_state): array {
  80.     $form = parent::blockForm($form, $form_state);
  81.  
  82.     $formats = $this->entityTypeManager->getStorage('date_format')->loadMultiple();
  83.     $options = [];
  84.     foreach ($formats as $format_id => $format) {
  85.       $options[$format_id] = $format->label();
  86.     }
  87.     $options['custom'] = $this->t('Custom format');
  88.  
  89.     $form['date_format'] = [
  90.       '#type' => 'select',
  91.       '#title' => $this->t('Date format'),
  92.       '#options' => $options,
  93.       '#default_value' => $this->configuration['date_format'],
  94.     ];
  95.  
  96.     $form['custom_format'] = [
  97.       '#type' => 'textfield',
  98.       '#title' => $this->t('Custom format'),
  99.       '#description' => $this->t('A date format as accepted by the PHP <a href=":url">date()</a> function. For example: <em>d.m.Y</em>.', [
  100.         ':url' => 'https://www.php.net/manual/datetime.format.php',
  101.       ]),
  102.       '#default_value' => $this->configuration['custom_format'],
  103.       '#states' => [
  104.         'visible' => [
  105.           ':input[name="settings[date_format]"]' => ['value' => 'custom'],
  106.         ],
  107.       ],
  108.     ];
  109.  
  110.     return $form;
  111.   }
  112.  
  113.   /**
  114.    * {@inheritdoc}
  115.    */
  116.   public function blockSubmit($form, FormStateInterface $form_state): void {
  117.     parent::blockSubmit($form, $form_state);
  118.     $this->configuration['date_format'] = $form_state->getValue('date_format');
  119.     $this->configuration['custom_format'] = $form_state->getValue('custom_format');
  120.   }
  121.  
  122.   /**
  123.    * {@inheritdoc}
  124.    */
  125.   public function build(): array {
  126.     $format_type = $this->configuration['date_format'];
  127.  
  128.     $date = $this->dateFormatter->format(
  129.       $this->time->getRequestTime(),
  130.       $format_type,
  131.       $format_type === 'custom' ? $this->configuration['custom_format'] : '',
  132.     );
  133.  
  134.     return [
  135.       'title' => [
  136.         '#type' => 'html_tag',
  137.         '#tag' => 'h3',
  138.         '#value' => $this->t('Current date'),
  139.       ],
  140.       'date' => [
  141.         '#markup' => $date,
  142.       ],
  143.       '#cache' => [
  144.         // The date must always be up to date.
  145.         'max-age' => 0,
  146.       ],
  147.     ];
  148.   }
  149.  
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment