Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupal\date_block\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\DateFormatterInterface;
- use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- /**
- * Provides a 'Current Date' block.
- */
- #[Block(
- id: "date_block",
- admin_label: new TranslatableMarkup("Current Date"),
- category: new TranslatableMarkup("Custom")
- )]
- class DateBlock extends BlockBase implements ContainerFactoryPluginInterface {
- /**
- * The date formatter service.
- *
- * @var \Drupal\Core\Datetime\DateFormatterInterface
- */
- protected $dateFormatter;
- /**
- * Constructs a DateBlock 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 $date_formatter
- * The date formatter service.
- */
- 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) {
- return new static(
- $configuration,
- $plugin_id,
- $plugin_definition,
- $container->get('date.formatter')
- );
- }
- /**
- * {@inheritdoc}
- */
- public function defaultConfiguration() {
- return [
- 'date_format' => 'short',
- 'custom_date_format' => '',
- 'label_display' => TRUE,
- ] + parent::defaultConfiguration();
- }
- /**
- * {@inheritdoc}
- */
- public function blockForm($form, FormStateInterface $form_state) {
- $formats = $this->dateFormatter->getAllFormats();
- $form['date_format'] = [
- '#type' => 'select',
- '#title' => $this->t('Date format'),
- '#description' => $this->t('Select the date format to use for displaying the current date.'),
- '#default_value' => $this->configuration['date_format'],
- '#options' => $formats,
- '#empty_option' => $this->t('- Custom -'),
- '#empty_value' => 'custom',
- ];
- $form['custom_date_format'] = [
- '#type' => 'textfield',
- '#title' => $this->t('Custom date format'),
- '#description' => $this->t('Enter a custom PHP date format string. See <a href=":url" target="_blank">PHP date() function</a> for format options.', [':url' => 'https://www.php.net/manual/en/function.date.php']),
- '#default_value' => $this->configuration['custom_date_format'],
- '#states' => [
- 'visible' => [
- ':input[name="date_format"]' => ['value' => 'custom'],
- ],
- ],
- ];
- return $form;
- }
- /**
- * {@inheritdoc}
- */
- public function blockSubmit($form, FormStateInterface $form_state) {
- $this->configuration['date_format'] = $form_state->getValue('date_format');
- $this->configuration['custom_date_format'] = $form_state->getValue('custom_date_format');
- }
- /**
- * {@inheritdoc}
- */
- public function build() {
- $current_time = \Drupal::time()->getRequestTime();
- if ($this->configuration['date_format'] === 'custom' && !empty($this->configuration['custom_date_format'])) {
- $date_string = $this->dateFormatter->format($current_time, 'custom', $this->configuration['custom_date_format']);
- }
- else {
- $date_string = $this->dateFormatter->format($current_time, $this->configuration['date_format']);
- }
- return [
- '#markup' => $date_string,
- '#cache' => [
- 'max-age' => 0,
- ],
- ];
- }
- }
Advertisement