xandeadx

Kimi 2.6 Fast

Jul 26th, 2026
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.64 KB | None | 0 0
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace Drupal\current_date_block\Plugin\Block;
  6.  
  7. use Drupal\Core\Block\BlockBase;
  8. use Drupal\Core\Block\Attribute\Block;
  9. use Drupal\Core\StringTranslation\TranslatableMarkup;
  10. use Drupal\Core\Form\FormStateInterface;
  11. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Drupal\Core\Datetime\DateFormatterInterface;
  14. use Drupal\Core\Datetime\Entity\DateFormat;
  15.  
  16. /**
  17.  * Блок текущей даты с настройкой формата.
  18.  */
  19. #[Block(
  20.  id: 'current_date_block',
  21.   admin_label: new TranslatableMarkup('Текущая дата'),
  22.   category: new TranslatableMarkup('Custom')
  23. )]
  24. class CurrentDateBlock extends BlockBase implements ContainerFactoryPluginInterface {
  25.  
  26.   /**
  27.    * Сервис форматирования дат.
  28.    */
  29.   protected DateFormatterInterface $dateFormatter;
  30.  
  31.   /**
  32.    * {@inheritdoc}
  33.    */
  34.   public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter) {
  35.     parent::__construct($configuration, $plugin_id, $plugin_definition);
  36.     $this->dateFormatter = $date_formatter;
  37.   }
  38.  
  39.   /**
  40.    * {@inheritdoc}
  41.    */
  42.   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
  43.     return new static(
  44.       $configuration,
  45.       $plugin_id,
  46.       $plugin_definition,
  47.       $container->get('date.formatter')
  48.     );
  49.   }
  50.  
  51.   /**
  52.    * {@inheritdoc}
  53.    */
  54.   public function defaultConfiguration(): array {
  55.     return [
  56.       'date_format' => 'short',
  57.       'label_display' => TRUE,
  58.     ] + parent::defaultConfiguration();
  59.   }
  60.  
  61.   /**
  62.    * {@inheritdoc}
  63.    */
  64.   public function blockForm($form, FormStateInterface $form_state): array {
  65.     $form = parent::blockForm($form, $form_state);
  66.  
  67.     // Получаем все доступные форматы дат сайта.
  68.     $date_formats = DateFormat::loadMultiple();
  69.     $options = [];
  70.     foreach ($date_formats as $machine_name => $format) {
  71.       $options[$machine_name] = $this->t('@label: @pattern', [
  72.         '@label' => $format->label(),
  73.         '@pattern' => \Drupal::service('date.formatter')->format(\Drupal::time()->getRequestTime(), $machine_name),
  74.       ]);
  75.     }
  76.  
  77.     $form['date_format'] = [
  78.       '#type' => 'select',
  79.       '#title' => $this->t('Формат даты'),
  80.       '#description' => $this->t('Выберите формат отображения даты из настроек сайта.'),
  81.       '#options' => $options,
  82.       '#default_value' => $this->configuration['date_format'],
  83.       '#required' => TRUE,
  84.     ];
  85.  
  86.     return $form;
  87.   }
  88.  
  89.   /**
  90.    * {@inheritdoc}
  91.    */
  92.   public function blockSubmit($form, FormStateInterface $form_state): void {
  93.     parent::blockSubmit($form, $form_state);
  94.     $this->configuration['date_format'] = $form_state->getValue('date_format');
  95.   }
  96.  
  97.   /**
  98.    * {@inheritdoc}
  99.    */
  100.   public function build(): array {
  101.     $timestamp = \Drupal::time()->getRequestTime();
  102.     $format = $this->configuration['date_format'];
  103.  
  104.     return [
  105.       '#type' => 'container',
  106.       '#attributes' => ['class' => ['current-date-block']],
  107.       'date' => [
  108.         '#type' => 'html_tag',
  109.         '#tag' => 'time',
  110.         '#attributes' => [
  111.           'datetime' => $this->dateFormatter->format($timestamp, 'html_datetime'),
  112.           'class' => ['current-date'],
  113.         ],
  114.         '#value' => $this->dateFormatter->format($timestamp, $format),
  115.       ],
  116.       '#cache' => [
  117.         'contexts' => ['timezone'],
  118.         'max-age' => 60,
  119.       ],
  120.     ];
  121.   }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment