Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- namespace Drupal\current_date\Plugin\Block;
- use Drupal\Core\Block\Attribute\Block;
- use Drupal\Core\Block\BlockBase;
- use Drupal\Core\Datetime\DateFormatterInterface;
- use Drupal\Core\Form\FormStateInterface;
- use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
- use Drupal\Core\StringTranslation\TranslatableMarkup;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- /**
- * Provides a block that displays the current date.
- */
- #[Block(
- id: 'current_date_block',
- admin_label: new TranslatableMarkup('Current date'),
- category: new TranslatableMarkup('Custom'),
- )]
- class CurrentDateBlock extends BlockBase implements ContainerFactoryPluginInterface {
- /**
- * The available predefined date format types.
- */
- protected const FORMAT_TYPES = [
- 'short' => 'Short',
- 'medium' => 'Medium',
- 'long' => 'Long',
- 'custom' => 'Custom',
- ];
- public function __construct(
- array $configuration,
- $plugin_id,
- $plugin_definition,
- protected DateFormatterInterface $dateFormatter,
- ) {
- parent::__construct($configuration, $plugin_id, $plugin_definition);
- }
- /**
- * {@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_type' => 'medium',
- 'custom_format' => 'd.m.Y',
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function blockForm($form, FormStateInterface $form_state): array {
- $config = $this->getConfiguration();
- $form['date_format_type'] = [
- '#type' => 'select',
- '#title' => $this->t('Date format'),
- '#description' => $this->t('Choose a predefined date format or "Custom" to enter your own PHP date format string.'),
- '#options' => array_map([$this, 't'], self::FORMAT_TYPES),
- '#default_value' => $config['date_format_type'],
- ];
- $form['custom_format'] = [
- '#type' => 'textfield',
- '#title' => $this->t('Custom date format'),
- '#description' => $this->t('A PHP date format string (e.g. <em>d.m.Y</em>, <em>Y-m-d H:i</em>). Used only when "Custom" format is selected. See the <a href=":url">PHP manual</a> for available placeholders.', [':url' => 'https://www.php.net/manual/datetime.format.php']),
- '#default_value' => $config['custom_format'],
- '#states' => [
- 'visible' => [
- ':input[name="settings[date_format_type]"]' => ['value' => 'custom'],
- ],
- ],
- ];
- return $form;
- }
- /**
- * {@inheritdoc}
- */
- public function blockSubmit($form, FormStateInterface $form_state): void {
- $this->configuration['date_format_type'] = $form_state->getValue('date_format_type');
- $this->configuration['custom_format'] = $form_state->getValue('custom_format');
- }
- /**
- * {@inheritdoc}
- */
- public function blockValidate($form, FormStateInterface $form_state): void {
- if ($form_state->getValue('date_format_type') === 'custom' && trim((string) $form_state->getValue('custom_format')) === '') {
- $form_state->setErrorByName('custom_format', $this->t('A custom date format string is required when "Custom" format is selected.'));
- }
- }
- /**
- * {@inheritdoc}
- */
- public function build(): array {
- $config = $this->getConfiguration();
- $type = $config['date_format_type'];
- $format = $type === 'custom' ? $config['custom_format'] : '';
- $formatted = $this->dateFormatter->format(
- \Drupal::time()->getRequestTime(),
- $type === 'custom' ? 'custom' : $type,
- $format,
- );
- return [
- '#theme' => 'current_date',
- '#date' => $formatted,
- '#cache' => [
- 'max-age' => 0,
- ],
- ];
- }
- }
Advertisement