You are here

public function Countdown::blockForm in Countdown 8

Implements a block form handler.

Overrides BlockPluginTrait::blockForm

File

src/Plugin/Block/countdown.php, line 102

Class

Countdown
Provides a 'Countdown' Block.

Namespace

Drupal\countdown\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {
  $form = parent::blockForm($form, $form_state);
  $time = time();
  $timestamp = $time;
  $event_name = '';
  $countdown_url = '';
  $countdown_accuracy = '';

  // Retrieve existing configuration for this block.
  $config = $this
    ->getConfiguration();
  if (isset($config['event_name'])) {
    $event_name = $config['event_name'];
    $countdown_url = $config['url'];
    $countdown_accuracy = $config['accuracy'];
    $timestamp = $config['timestamp'];
  }
  $form['countdown_event_name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Event Name'),
    '#default_value' => $event_name,
    '#size' => 30,
    '#maxlength' => 200,
    '#description' => $this
      ->t("Event name you're counting to or from."),
    '#required' => TRUE,
  ];
  $form['countdown_url'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Event URL'),
    '#default_value' => $countdown_url,
    '#size' => 30,
    '#maxlength' => 200,
    '#description' => $this
      ->t('Turn the event description into a link to more
        information about the event.
        Start typing the title of a piece of content to select it.
        You can also enter an internal path such as %add-node or an external URL
        such as %url. Enter %front to link to the front page.', [
      '%front' => '<front>',
      '%add-node' => '/node/add',
      '%url' => 'http://example.com',
    ]),
    '#required' => FALSE,
  ];
  $form['countdown_accuracy'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Accuracy'),
    '#default_value' => $countdown_accuracy,
    '#options' => [
      'd' => $this
        ->t('days'),
      'h' => $this
        ->t('hours'),
      'm' => $this
        ->t('minutes'),
      's' => $this
        ->t('seconds'),
    ],
    '#description' => $this
      ->t('Select the smallest amount of detail to display. For example, selecting "days" will display only days, selecting "hours" will display the number of days and hours.'),
  ];
  $form['target_time'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Target date/time'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#description' => $this
      ->t('Select a date relative to the server time: %s', [
      '%s' => \Drupal::service('date.formatter')
        ->format($time),
    ]),
  ];
  for ($years = [], $i = 1970; $i < 2032; $years[$i] = $i, $i++) {
  }
  $form['target_time']['year'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Year'),
    '#default_value' => (int) date('Y', $timestamp),
    '#options' => $years,
  ];
  unset($years);
  $form['target_time']['month'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Month'),
    '#default_value' => (int) date('n', $timestamp),
    '#options' => [
      1 => $this
        ->t('January'),
      2 => $this
        ->t('February'),
      3 => $this
        ->t('March'),
      4 => $this
        ->t('April'),
      5 => $this
        ->t('May'),
      6 => $this
        ->t('June'),
      7 => $this
        ->t('July'),
      8 => $this
        ->t('August'),
      9 => $this
        ->t('September'),
      10 => $this
        ->t('October'),
      11 => $this
        ->t('November'),
      12 => $this
        ->t('December'),
    ],
  ];
  for ($month_days = [], $i = 1; $i < 32; $month_days[$i] = $i, $i++) {
  }
  $form['target_time']['day'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Day'),
    '#default_value' => (int) date('j', $timestamp),
    '#options' => $month_days,
  ];
  unset($month_days);
  for ($hrs = [], $i = 0; $i < 24; $hrs[] = $i, $i++) {
  }
  $form['target_time']['hour'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Hour'),
    '#default_value' => (int) date('G', $timestamp),
    '#options' => $hrs,
  ];
  unset($hrs);
  for ($mins = [], $i = 0; $i < 60; $mins[] = $i, $i++) {
  }
  $form['target_time']['min'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Minute'),
    '#default_value' => (int) date('i', $timestamp),
    '#options' => $mins,
  ];
  $form['target_time']['sec'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Seconds'),
    '#default_value' => (int) date('s', $timestamp),
    '#options' => $mins,
  ];
  return $form;
}