Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupal\date_display\Plugin\Block;
- use Drupal\Core\Block\Attribute\Block;
- use Drupal\Core\Block\BlockBase;
- use Drupal\Core\Form\FormStateInterface;
- use Drupal\Core\StringTranslation\TranslatableMarkup;
- use Drupal\Core\DateTime\DateTimeFormatter;
- /**
- * Provides a Date Display block.
- */
- #[Block(
- id: "date_display",
- admin_label: new TranslatableMarkup("Date Display"),
- category: new TranslatableMarkup("Date"),
- )]
- class DateDisplayBlock extends BlockBase {
- /**
- * {@inheritdoc}
- */
- public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
- $form = parent::buildConfigurationForm($form, $form_state);
- // Title setting
- $form['title'] = [
- '#type' => 'textfield',
- '#title' => $this->t('Title'),
- '#description' => $this->t('Optional title to display above the date'),
- '#default_value' => $this->getSetting('title'),
- ];
- // Date format setting
- $form['format'] = [
- '#type' => 'textfield',
- '#title' => $this->t('Date format'),
- '#description' => $this->t('Enter a date format string (see <a href="https://www.php.net/manual/en/datetime.format.php">PHP date format</a>). Default is F j, Y (e.g., March 1, 2024).'),
- '#default_value' => $this->getSetting('format'),
- '#placeholder' => $this->t('F j, Y'),
- ];
- // Show full date setting
- $form['show_full_date'] = [
- '#type' => 'checkbox',
- '#title' => $this->t('Show full date (customization)'),
- '#description' => $this->t('When enabled, allows custom date customization for different display options'),
- '#default_value' => $this->getSetting('show_full_date') ?? false,
- ];
- return $form;
- }
- /**
- * {@inheritdoc}
- */
- public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
- parent::submitConfigurationForm($form, $form_state);
- $values = $form_state->getValues();
- $this->configuration['title'] = $values['title'];
- $this->configuration['format'] = $values['format'];
- $this->configuration['show_full_date'] = $values['show_full_date'] ?? false;
- }
- /**
- * {@inheritdoc}
- */
- public function defaultConfiguration() {
- return [
- 'title' => '',
- 'format' => 'F j, Y',
- 'show_full_date' => false,
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function build() {
- $build = [];
- // Add title if configured
- if (!empty($this->getSetting('title'))) {
- $build['title'] = [
- '#type' => 'html_tag',
- '#tag' => 'h2',
- '#attributes' => ['class' => ['date-display-title']],
- '#value' => $this->getSetting('title'),
- ];
- }
- // Get current date
- $dateTimeFormatter = $this->getDateFormatter();
- $current_date = $dateTimeFormatter->format(time(), $this->getSetting('format'))
- ?: $dateTimeFormatter->format(time(), 'F j, Y');
- // Add date display
- $build['date'] = [
- '#type' => 'markup',
- '#markup' => '<div class="date-display-value">' . $current_date . '</div>',
- '#allowed_tags' => ['div'],
- ];
- // Add additional formatting options if enabled
- if ($this->getSetting('show_full_date')) {
- $build['additional_dates'] = [
- '#type' => 'container',
- '#attributes' => ['class' => ['date-display-additional']],
- '#children' => [
- $this->renderShortDate($dateTimeFormatter),
- $this->renderLongDate($dateTimeFormatter),
- ],
- ];
- }
- return $build;
- }
- /**
- * Render a short version of the date.
- */
- protected function renderShortDate(DateTimeFormatter $dateTimeFormatter) {
- $short_format = 'M j, Y';
- $current_date = $dateTimeFormatter->format(time(), $short_format);
- return [
- '#type' => 'html_tag',
- '#tag' => 'span',
- '#attributes' => ['class' => ['date-display-short']],
- '#value' => $this->t('Short: @date', ['@date' => $current_date]),
- ];
- }
- /**
- * Render a long version of the date.
- */
- protected function renderLongDate(DateTimeFormatter $dateTimeFormatter) {
- $long_format = 'l, F j, Y - g:i A';
- $current_date = $dateTimeFormatter->format(time(), $long_format);
- return [
- '#type' => 'html_tag',
- '#tag' => 'span',
- '#attributes' => ['class' => ['date-display-long']],
- '#value' => $this->t('Full: @date', ['@date' => $current_date]),
- ];
- }
- }
Advertisement