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\BlockBase;
- use Drupal\Core\Block\Attribute\Block;
- use Drupal\Core\StringTranslation\TranslatableMarkup;
- use Drupal\Core\Form\FormStateInterface;
- use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Drupal\Core\Datetime\DateFormatterInterface;
- use Drupal\Core\Datetime\Entity\DateFormat;
- /**
- * Блок текущей даты с настройкой формата.
- */
- #[Block(
- id: 'current_date_block',
- admin_label: new TranslatableMarkup('Текущая дата'),
- category: new TranslatableMarkup('Custom')
- )]
- class CurrentDateBlock extends BlockBase implements ContainerFactoryPluginInterface {
- /**
- * Сервис форматирования дат.
- */
- protected DateFormatterInterface $dateFormatter;
- /**
- * {@inheritdoc}
- */
- public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter) {
- parent::__construct($configuration, $plugin_id, $plugin_definition);
- $this->dateFormatter = $date_formatter;
- }
- /**
- * {@inheritdoc}
- */
- public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
- return new static(
- $configuration,
- $plugin_id,
- $plugin_definition,
- $container->get('date.formatter')
- );
- }
- /**
- * {@inheritdoc}
- */
- public function defaultConfiguration(): array {
- return [
- 'date_format' => 'short',
- 'label_display' => TRUE,
- ] + parent::defaultConfiguration();
- }
- /**
- * {@inheritdoc}
- */
- public function blockForm($form, FormStateInterface $form_state): array {
- $form = parent::blockForm($form, $form_state);
- // Получаем все доступные форматы дат сайта.
- $date_formats = DateFormat::loadMultiple();
- $options = [];
- foreach ($date_formats as $machine_name => $format) {
- $options[$machine_name] = $this->t('@label: @pattern', [
- '@label' => $format->label(),
- '@pattern' => \Drupal::service('date.formatter')->format(\Drupal::time()->getRequestTime(), $machine_name),
- ]);
- }
- $form['date_format'] = [
- '#type' => 'select',
- '#title' => $this->t('Формат даты'),
- '#description' => $this->t('Выберите формат отображения даты из настроек сайта.'),
- '#options' => $options,
- '#default_value' => $this->configuration['date_format'],
- '#required' => TRUE,
- ];
- return $form;
- }
- /**
- * {@inheritdoc}
- */
- public function blockSubmit($form, FormStateInterface $form_state): void {
- parent::blockSubmit($form, $form_state);
- $this->configuration['date_format'] = $form_state->getValue('date_format');
- }
- /**
- * {@inheritdoc}
- */
- public function build(): array {
- $timestamp = \Drupal::time()->getRequestTime();
- $format = $this->configuration['date_format'];
- return [
- '#type' => 'container',
- '#attributes' => ['class' => ['current-date-block']],
- 'date' => [
- '#type' => 'html_tag',
- '#tag' => 'time',
- '#attributes' => [
- 'datetime' => $this->dateFormatter->format($timestamp, 'html_datetime'),
- 'class' => ['current-date'],
- ],
- '#value' => $this->dateFormatter->format($timestamp, $format),
- ],
- '#cache' => [
- 'contexts' => ['timezone'],
- 'max-age' => 60,
- ],
- ];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment