You are here

public function OfficeHoursItem::storageSettingsForm in Office Hours 8

Returns a form for the storage-level settings.

Invoked from \Drupal\field_ui\Form\FieldStorageConfigEditForm to allow administrators to configure storage-level settings.

Field storage might reject settings changes that affect the field storage schema if the storage already has data. When the $has_data parameter is TRUE, the form should not allow changing the settings that take part in the schema() method. It is recommended to set #access to FALSE on the corresponding elements.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the (entire) configuration form.

bool $has_data: TRUE if the field already has data, FALSE if not.

Return value

array The form definition for the field settings.

Overrides FieldItemBase::storageSettingsForm

File

src/Plugin/Field/FieldType/OfficeHoursItem.php, line 98

Class

OfficeHoursItem
Plugin implementation of the 'office_hours' field type.

Namespace

Drupal\office_hours\Plugin\Field\FieldType

Code

public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
  $element = parent::storageSettingsForm($form, $form_state, $has_data);
  $settings = $this
    ->getFieldDefinition()
    ->getFieldStorageDefinition()
    ->getSettings();

  // Get a formatted list of valid hours values.
  $hours = OfficeHoursDateHelper::hours('H', FALSE);
  foreach ($hours as $key => &$hour) {
    if (!empty($hour)) {
      $hrs = OfficeHoursDateHelper::format($hour . '00', 'H:i');
      $ampm = OfficeHoursDateHelper::format($hour . '00', 'g:i a');
      $hour = "{$hrs} ({$ampm})";
    }
  }
  $element['#element_validate'] = [
    [
      static::class,
      'validateOfficeHoursSettings',
    ],
  ];
  $description = $this
    ->t('The maximum number of time slots, that are allowed per day.
      <br/><strong> Warning! Lowering this setting after data has been created
      could result in the loss of data! </strong><br/> Be careful when using
      more then 2 slots per day, since not all external services (like Google
      Places) support this.');
  $element['cardinality_per_day'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Number of time slots per day'),
    '#options' => array_combine(range(1, 12), range(1, 12)),
    '#default_value' => $settings['cardinality_per_day'],
    '#description' => $description,
  ];

  // @todo D8 Align with DateTimeDatelistWidget.
  $element['time_format'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Time notation'),
    '#options' => [
      'G' => $this
        ->t('24 hour time @example', [
        '@example' => '(9:00)',
      ]),
      'H' => $this
        ->t('24 hour time @example', [
        '@example' => '(09:00)',
      ]),
      'g' => $this
        ->t('12 hour time @example', [
        '@example' => '09:00 am)',
      ]),
      'h' => $this
        ->t('12 hour time @example', [
        '@example' => '(09:00 am)',
      ]),
    ],
    '#default_value' => $settings['time_format'],
    '#required' => FALSE,
    '#description' => $this
      ->t('Format of the time in the widget.'),
  ];
  $element['element_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Time element type'),
    '#description' => $this
      ->t('Select the widget type for selecting the time.'),
    '#options' => [
      'office_hours_datelist' => 'Select list',
      'office_hours_datetime' => 'HTML5 time input',
    ],
    '#default_value' => $this
      ->getSetting('element_type'),
  ];

  // @todo D8 Align with DateTimeDatelistWidget.
  $element['increment'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Time increments'),
    '#default_value' => $settings['increment'],
    '#options' => [
      1 => $this
        ->t('1 minute'),
      5 => $this
        ->t('5 minute'),
      15 => $this
        ->t('15 minute'),
      30 => $this
        ->t('30 minute'),
      60 => $this
        ->t('60 minute'),
    ],
    '#required' => FALSE,
    '#description' => $this
      ->t('Restrict the input to fixed fractions of an hour.'),
  ];
  $element['comment'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Allow a comment per time slot'),
    '#required' => FALSE,
    '#default_value' => $settings['comment'],
    '#options' => [
      0 => $this
        ->t('No comments allowed'),
      1 => $this
        ->t('Allow comments (HTML tags possible)'),
      2 => $this
        ->t('Allow translatable comments (no HTML)'),
    ],
  ];
  $element['valhrs'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Validate hours'),
    '#required' => FALSE,
    '#default_value' => $settings['valhrs'],
    '#description' => $this
      ->t('Assure that endhours are later then starthours.
        Please note that this will work as long as both hours are set and
        the opening hours are not through midnight.'),
  ];
  $element['required_start'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Require Start time'),
    '#default_value' => $settings['required_start'],
  ];
  $element['required_end'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Require End time'),
    '#default_value' => $settings['required_end'],
  ];
  $element['limit_start'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Limit hours - from'),
    '#description' => $this
      ->t('Restrict the hours available - select options will start from this hour.'),
    '#default_value' => $settings['limit_start'],
    '#options' => $hours,
  ];
  $element['limit_end'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Limit hours - until'),
    '#description' => $this
      ->t('Restrict the hours available - select options
         will end at this hour. You may leave \'until\' time empty.
         Use \'00:00\' for closing at midnight.'),
    '#default_value' => $settings['limit_end'],
    '#options' => $hours,
  ];
  return $element;
}