You are here

public function DateRecurBasicFormatter::settingsForm in Recurring Dates Field 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/DateRecurBasicFormatter.php \Drupal\date_recur\Plugin\Field\FieldFormatter\DateRecurBasicFormatter::settingsForm()
  2. 3.0.x src/Plugin/Field/FieldFormatter/DateRecurBasicFormatter.php \Drupal\date_recur\Plugin\Field\FieldFormatter\DateRecurBasicFormatter::settingsForm()
  3. 3.1.x src/Plugin/Field/FieldFormatter/DateRecurBasicFormatter.php \Drupal\date_recur\Plugin\Field\FieldFormatter\DateRecurBasicFormatter::settingsForm()

Returns a form to configure settings for the formatter.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the formatter. The field_ui module takes care of handling submitted form values.

Parameters

array $form: The form where the settings form is being included in.

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

Return value

array The form elements for the formatter settings.

Overrides DateRangeDefaultFormatter::settingsForm

File

src/Plugin/Field/FieldFormatter/DateRecurBasicFormatter.php, line 151

Class

DateRecurBasicFormatter
Basic recurring date formatter.

Namespace

Drupal\date_recur\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) : array {
  $form = parent::settingsForm($form, $form_state);
  $originalFormatType = $form['format_type'];
  unset($form['format_type']);

  // Redefine format type to change the natural order of form fields.
  $form['format_type'] = $originalFormatType;
  $form['format_type']['#title'] = $this
    ->t('Non-Repeating Date format');
  $form['format_type']['#description'] = $this
    ->t('Date format used for field values without repeat rules.');
  $form['occurrence_format_type'] = $originalFormatType;
  $form['occurrence_format_type']['#title'] = $this
    ->t('Start and end date format');
  $form['occurrence_format_type']['#default_value'] = $this
    ->getSetting('occurrence_format_type');
  $form['occurrence_format_type']['#description'] = $this
    ->t('Date format used for field values with repeat rules.');
  $form['same_end_date_format_type'] = $originalFormatType;
  $form['same_end_date_format_type']['#title'] = $this
    ->t('Same day end date format');
  $form['same_end_date_format_type']['#description'] = $this
    ->t('Date format used for end date if field value has repeat rule. Used only if occurs on same calendar day as start date.');
  $form['same_end_date_format_type']['#default_value'] = $this
    ->getSetting('same_end_date_format_type');

  // Redefine separator to change the natural order of form fields.
  $originalSeparator = $form['separator'];
  unset($form['separator']);
  $form['separator'] = $originalSeparator;

  // Change the width of the field if not already set. (Not set by default)
  $form['separator']['#size'] = $form['separator']['#size'] ?? 5;

  // Redefine timezone to change the natural order of form fields.
  $originalTimezoneOverride = $form['timezone_override'];
  unset($form['timezone_override']);
  $form['timezone_override'] = $originalTimezoneOverride;
  $form['timezone_override']['#empty_option'] = $this
    ->t('Use current user timezone');
  $form['timezone_override']['#description'] = $this
    ->t('Change the timezone used for displaying dates (not recommended).');
  $interpreterOptions = array_map(function (DateRecurInterpreterInterface $interpreter) : string {
    return $interpreter
      ->label() ?? (string) $this
      ->t('- Missing label -');
  }, $this->dateRecurInterpreterStorage
    ->loadMultiple());
  $form['interpreter'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Recurring date interpreter'),
    '#description' => $this
      ->t('Choose a plugin for converting rules into a human readable description.'),
    '#default_value' => $this
      ->getSetting('interpreter'),
    '#options' => $interpreterOptions,
    '#required' => FALSE,
    '#empty_option' => $this
      ->t('- Do not show interpreted rule -'),
  ];
  $form['occurrences'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'container-inline',
      ],
    ],
    '#tree' => FALSE,
  ];
  $form['occurrences']['show_next'] = [
    '#field_prefix' => $this
      ->t('Show maximum of'),
    '#field_suffix' => $this
      ->t('occurrences'),
    '#type' => 'number',
    '#min' => 0,
    '#default_value' => $this
      ->getSetting('show_next'),
    '#attributes' => [
      'size' => 4,
    ],
    '#element_validate' => [
      [
        static::class,
        'validateSettingsShowNext',
      ],
    ],
  ];
  $form['occurrences']['count_per_item'] = [
    '#type' => 'select',
    '#options' => [
      static::COUNT_PER_ITEM_ITEM => $this
        ->t('per field item'),
      static::COUNT_PER_ITEM_ALL => $this
        ->t('across all field items'),
    ],
    '#default_value' => $this
      ->getSetting('count_per_item') ? static::COUNT_PER_ITEM_ITEM : static::COUNT_PER_ITEM_ALL,
    '#element_validate' => [
      [
        static::class,
        'validateSettingsCountPerItem',
      ],
    ],
  ];
  return $form;
}