Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupal\current_date_block\Plugin\Block;
- use Drupal\Component\Datetime\TimeInterface;
- use Drupal\Core\Block\Attribute\Block;
- use Drupal\Core\Block\BlockBase;
- use Drupal\Core\Datetime\DateFormatterInterface;
- use Drupal\Core\Entity\EntityTypeManagerInterface;
- 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('System'),
- )]
- final class CurrentDateBlock extends BlockBase implements ContainerFactoryPluginInterface {
- /**
- * Constructs a CurrentDateBlock object.
- *
- * @param array $configuration
- * A configuration array containing information about the plugin instance.
- * @param string $plugin_id
- * The plugin ID for the plugin instance.
- * @param mixed $plugin_definition
- * The plugin implementation definition.
- * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
- * The date formatter service.
- * @param \Drupal\Component\Datetime\TimeInterface $time
- * The time service.
- * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
- * The entity type manager service.
- */
- public function __construct(
- array $configuration,
- $plugin_id,
- $plugin_definition,
- protected readonly DateFormatterInterface $dateFormatter,
- protected readonly TimeInterface $time,
- protected readonly EntityTypeManagerInterface $entityTypeManager,
- ) {
- 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'),
- $container->get('datetime.time'),
- $container->get('entity_type.manager'),
- );
- }
- /**
- * {@inheritdoc}
- */
- public function defaultConfiguration(): array {
- return [
- 'date_format' => 'medium',
- 'custom_format' => 'd.m.Y',
- ] + parent::defaultConfiguration();
- }
- /**
- * {@inheritdoc}
- */
- public function blockForm($form, FormStateInterface $form_state): array {
- $form = parent::blockForm($form, $form_state);
- $formats = $this->entityTypeManager->getStorage('date_format')->loadMultiple();
- $options = [];
- foreach ($formats as $format_id => $format) {
- $options[$format_id] = $format->label();
- }
- $options['custom'] = $this->t('Custom format');
- $form['date_format'] = [
- '#type' => 'select',
- '#title' => $this->t('Date format'),
- '#options' => $options,
- '#default_value' => $this->configuration['date_format'],
- ];
- $form['custom_format'] = [
- '#type' => 'textfield',
- '#title' => $this->t('Custom format'),
- '#description' => $this->t('A date format as accepted by the PHP <a href=":url">date()</a> function. For example: <em>d.m.Y</em>.', [
- ':url' => 'https://www.php.net/manual/datetime.format.php',
- ]),
- '#default_value' => $this->configuration['custom_format'],
- '#states' => [
- 'visible' => [
- ':input[name="settings[date_format]"]' => ['value' => 'custom'],
- ],
- ],
- ];
- 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');
- $this->configuration['custom_format'] = $form_state->getValue('custom_format');
- }
- /**
- * {@inheritdoc}
- */
- public function build(): array {
- $format_type = $this->configuration['date_format'];
- $date = $this->dateFormatter->format(
- $this->time->getRequestTime(),
- $format_type,
- $format_type === 'custom' ? $this->configuration['custom_format'] : '',
- );
- return [
- 'title' => [
- '#type' => 'html_tag',
- '#tag' => 'h3',
- '#value' => $this->t('Current date'),
- ],
- 'date' => [
- '#markup' => $date,
- ],
- '#cache' => [
- // The date must always be up to date.
- 'max-age' => 0,
- ],
- ];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment