You are here

public function SmartDateFormatForm::buildForm in Smart Date 8

Same name and namespace in other branches
  1. 8.2 src/Form/SmartDateFormatForm.php \Drupal\smart_date\Form\SmartDateFormatForm::buildForm()
  2. 3.x src/Form/SmartDateFormatForm.php \Drupal\smart_date\Form\SmartDateFormatForm::buildForm()
  3. 3.0.x src/Form/SmartDateFormatForm.php \Drupal\smart_date\Form\SmartDateFormatForm::buildForm()
  4. 3.1.x src/Form/SmartDateFormatForm.php \Drupal\smart_date\Form\SmartDateFormatForm::buildForm()
  5. 3.2.x src/Form/SmartDateFormatForm.php \Drupal\smart_date\Form\SmartDateFormatForm::buildForm()
  6. 3.3.x src/Form/SmartDateFormatForm.php \Drupal\smart_date\Form\SmartDateFormatForm::buildForm()
  7. 3.4.x src/Form/SmartDateFormatForm.php \Drupal\smart_date\Form\SmartDateFormatForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/SmartDateFormatForm.php, line 74

Class

SmartDateFormatForm
Form controller for smart date format edit forms.

Namespace

Drupal\smart_date\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $entity = $this->entity;

  // Populate defaults.
  if ($entity
    ->isNew()) {
    $options = [
      'date_format' => 'D, M j Y',
      'time_format' => 'g:ia',
      'time_hour_format' => 'ga',
      'separator' => ' - ',
      'join' => ', ',
      'allday_label' => 'All day',
      'date_first' => '1',
      'ampm_reduce' => '1',
    ];
  }
  else {

    // populate from the entity
    $options = $entity
      ->getOptions();
  }
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#default_value' => $entity
      ->label(),
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $entity
      ->id(),
    '#disabled' => !$entity
      ->isNew(),
    '#maxlength' => 64,
    '#description' => $this
      ->t('A unique name for this item. It must only contain lowercase letters, numbers, and underscores.'),
    '#machine_name' => [
      'exists' => [
        $this,
        'exists',
      ],
    ],
  ];
  $form['date_format'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('PHP Date Format'),
    '#description' => $this
      ->t('The PHP date code to use for formatting dates.'),
    '#default_value' => $options['date_format'],
    '#size' => 20,
  ];
  $form['time_format'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('PHP Time Format'),
    '#description' => $this
      ->t('The PHP date code to use for formatting times.'),
    '#default_value' => $options['time_format'],
    '#size' => 20,
  ];
  $form['time_hour_format'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('PHP Time Format - on the hour'),
    '#description' => $this
      ->t('The PHP date code to use for formatting times that fall on the hour. Examples might be 2pm or 14h. Leave this blank to always use the standard format specified above.'),
    '#default_value' => $options['time_hour_format'],
    '#size' => 20,
  ];
  $form['allday_label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('All Day Label'),
    '#description' => $this
      ->t('What to output when an event has been set to run all day. Leave blank to only show the date.'),
    '#default_value' => $options['allday_label'],
    '#size' => 20,
  ];
  $form['separator'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Time separator'),
    '#description' => $this
      ->t('The string to separate the start and end times. Include spaces before and after if those are desired.'),
    '#default_value' => $options['separator'],
    '#size' => 10,
  ];
  $form['join'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Date/time join'),
    '#description' => $this
      ->t('The characters that will be used to join dates and their associated times.'),
    '#default_value' => $options['join'],
    '#size' => 10,
  ];
  $form['date_first'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('First part shown'),
    '#description' => $this
      ->t('Specify whether the time or date should be shown first.'),
    '#default_value' => $options['date_first'],
    '#options' => [
      '1' => $this
        ->t('Date'),
      '0' => $this
        ->t('Time'),
    ],
  ];
  $form['ampm_reduce'] = [
    '#type' => 'checkbox',
    '#return_value' => '1',
    '#title' => $this
      ->t('Reduce output duplication'),
    '#description' => $this
      ->t("Don't show am/pm in the start time if it's the same as the value for the end time, in the same day. Note that this is recommended by the Associated Press style guide. Will also reduce duplicate output of month and year in date ranges, when appropriate."),
    '#default_value' => $options['ampm_reduce'],
  ];
  $form['langcode'] = [
    '#type' => 'language_select',
    '#title' => t('Language'),
    '#languages' => LanguageInterface::STATE_ALL,
    '#default_value' => $entity
      ->language()
      ->getId(),
  ];
  return $form;
}