View source
<?php
namespace Drupal\office_hours\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\office_hours\OfficeHoursDateHelper;
class OfficeHoursDefaultWidget extends OfficeHoursWidgetBase {
protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
$field_cardinality = $this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality();
if ($field_cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
$this->fieldDefinition
->getFieldStorageDefinition()
->setCardinality($this
->getFieldSetting('cardinality_per_day') * 7);
}
$elements = parent::formMultipleElements($items, $form, $form_state);
$elements['#cardinality_multiple'] = FALSE;
unset($elements[0]['_weight']);
return $elements;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
if ($delta > 0) {
return [];
}
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$indexed_items = array_fill_keys(range(0, 6), []);
foreach ($items as $index => $item) {
$value_of_item = $item
->getValue();
if ($item && !empty($value_of_item)) {
$indexed_items[(int) $value_of_item['day']][] = $item;
}
}
$elements = [];
$days = OfficeHoursDateHelper::weekDaysOrdered(range(0, 6));
$day_names = OfficeHoursDateHelper::weekDays(FALSE);
$cardinality = $this
->getFieldSetting('cardinality_per_day');
foreach ($days as $day) {
for ($day_delta = 0; $day_delta < $cardinality; $day_delta++) {
$default_value = [
'day' => $day,
'day_delta' => $day_delta,
];
$default_value = isset($indexed_items[$day][$day_delta]) ? $indexed_items[$day][$day_delta]
->getValue() + $default_value : $default_value;
$elements[] = [
'#day' => $day,
'#daydelta' => $day_delta,
'#dayname' => $day_names[$day],
'#type' => 'office_hours_slot',
'#default_value' => $default_value,
'#field_settings' => $element['#field_settings'],
'#date_element_type' => $this
->getSetting('date_element_type'),
];
}
}
$header = [
'title' => $this
->t($element['#title']),
'from' => $this
->t('From', [], [
'context' => 'A point in time',
]),
'to' => $this
->t('To', [], [
'context' => 'A point in time',
]),
];
if ($element['#field_settings']['comment']) {
$header['comment'] = $this
->t('Comment');
}
$header['operations'] = $this
->t('Operations');
$element['value'] = [
'#type' => 'office_hours_table',
'#header' => $header,
'#tableselect' => FALSE,
'#tabledrag' => FALSE,
] + $element['value'] + $elements;
$element['#type'] = 'details';
$element['#open'] = TRUE;
$element['value']['#header']['title'] = NULL;
return $element;
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
$multiple_values = $this
->getPluginDefinition()['multiple_values'];
if ($multiple_values == 'FALSE') {
$values = $values['value'];
}
elseif ($multiple_values == 'TRUE') {
$values = reset($values)['value'];
}
$values = parent::massageFormValues($values, $form, $form_state);
return $values;
}
}