You are here

public function OfficeHoursWidgetBase::massageFormValues in Office Hours 8

Massages the form values into the format expected for field values.

Parameters

array $values: The submitted form values produced by the widget.

  • If the widget does not manage multiple values itself, the array holds the values generated by the multiple copies of the $element generated by the formElement() method, keyed by delta.
  • If the widget manages multiple values, the array holds the values of the form element generated by the formElement() method.

array $form: The form structure where field elements are attached to. This might be a full form structure, or a sub-element of a larger form.

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

Return value

array An array of field values, keyed by delta.

Overrides WidgetBase::massageFormValues

2 calls to OfficeHoursWidgetBase::massageFormValues()
OfficeHoursDefaultWidget::massageFormValues in src/Plugin/Field/FieldWidget/OfficeHoursDefaultWidget.php
Massages the form values into the format expected for field values.
OfficeHoursListWidget::massageFormValues in src/Plugin/Field/FieldWidget/OfficeHoursListWidget.php
This function repairs the anomaly we mentioned before.
2 methods override OfficeHoursWidgetBase::massageFormValues()
OfficeHoursDefaultWidget::massageFormValues in src/Plugin/Field/FieldWidget/OfficeHoursDefaultWidget.php
Massages the form values into the format expected for field values.
OfficeHoursListWidget::massageFormValues in src/Plugin/Field/FieldWidget/OfficeHoursListWidget.php
This function repairs the anomaly we mentioned before.

File

src/Plugin/Field/FieldWidget/OfficeHoursWidgetBase.php, line 39

Class

OfficeHoursWidgetBase
Base class for the 'office_hours_*' widgets.

Namespace

Drupal\office_hours\Plugin\Field\FieldWidget

Code

public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {

  // N.B. The $values are already reformatted in the subWidgets.
  foreach ($values as $key => &$item) {

    // Note: below could better be done in OfficeHoursItemList::filter().
    // However, then we have below error "value '' is not allowed".
    // Or "This value should be of the correct primitive type".
    if (OfficeHoursDatetime::isEmpty($item)) {
      unset($values[$key]);
      continue;
    }

    // Get hours. Result can be NULL, '', 0000, or a proper time.
    $start = OfficeHoursDatetime::get($item['starthours'], 'Hi');
    $end = OfficeHoursDatetime::get($item['endhours'], 'Hi');

    // Cast the time to integer, to avoid core's error
    // "This value should be of the correct primitive type."
    // This is needed for e.g., 0000 and 0030.
    $item['starthours'] = isset($start) ? (int) $start : -1;
    $item['endhours'] = isset($end) ? (int) $end : -1;

    // Allow Empty time field with comment (#2070145).
    // In principle, this is prohibited by the database: value '' is not
    // allowed. The format is int(11).
    // Would changing the format to 'string' help?
    // Perhaps, but using '-1' (saved as '-001') works, too.
    if (!empty($item['comment'])) {
      $item['starthours'] = empty($start) ? -1 : $item['starthours'];
      $item['endhours'] = empty($end) ? -1 : $item['endhours'];
    }
  }
  return $values;
}