Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupal\current_date\Plugin\Block;
- use Drupal\Core\Block\Attribute\Block;
- use Drupal\Core\Block\BlockBase;
- use Drupal\Core\Cache\Cache;
- 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"),
- )]
- class CurrentDateBlock extends BlockBase implements ContainerFactoryPluginInterface {
- /**
- * Constructs a CurrentDateBlock instance.
- */
- public function __construct(
- array $configuration,
- $plugin_id,
- $plugin_definition,
- protected DateFormatterInterface $dateFormatter,
- protected EntityTypeManagerInterface $entityTypeManager,
- ) {
- parent::__construct($configuration, $plugin_id, $plugin_definition);
- }
- /**
- * {@inheritdoc}
- */
- public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
- return new static(
- $configuration,
- $plugin_id,
- $plugin_definition,
- $container->get('date.formatter'),
- $container->get('entity_type.manager'),
- );
- }
- /**
- * {@inheritdoc}
- */
- public function defaultConfiguration() {
- return [
- 'date_format' => 'long',
- 'label_display' => '',
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function blockForm($form, FormStateInterface $form_state) {
- $form['date_format'] = [
- '#type' => 'select',
- '#title' => $this->t('Date format'),
- '#description' => $this->t('Select the format in which to display the date.'),
- '#default_value' => $this->configuration['date_format'],
- '#options' => $this->getDateFormatOptions(),
- ];
- return $form;
- }
- /**
- * {@inheritdoc}
- */
- public function blockSubmit($form, FormStateInterface $form_state) {
- $this->configuration['date_format'] = $form_state->getValue('date_format');
- }
- /**
- * {@inheritdoc}
- */
- public function build() {
- $formatted_date = $this->dateFormatter->format(
- time(),
- $this->configuration['date_format'],
- );
- return [
- '#markup' => $formatted_date,
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function getCacheContexts() {
- return Cache::mergeContexts(
- parent::getCacheContexts(),
- ['timezone', 'languages:language_interface'],
- );
- }
- /**
- * {@inheritdoc}
- */
- public function getCacheMaxAge() {
- return 0;
- }
- /**
- * Returns available date format options.
- *
- * @return array
- * An associative array of date format ID => label.
- */
- protected function getDateFormatOptions() {
- $formats = $this->entityTypeManager->getStorage('date_format')->loadMultiple();
- $options = [];
- foreach ($formats as $id => $format) {
- $options[$id] = $format->label();
- }
- return $options;
- }
- }
Advertisement