EndTimestampWidget.php in Commerce Recurring Framework 8
File
src/Plugin/Field/FieldWidget/EndTimestampWidget.php
View source
<?php
namespace Drupal\commerce_recurring\Plugin\Field\FieldWidget;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Datetime\Plugin\Field\FieldWidget\TimestampDatetimeWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
class EndTimestampWidget extends TimestampDatetimeWidget {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$element['has_value'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Provide an end date'),
'#default_value' => !empty($element['value']['#default_value']),
'#access' => empty($element['value']['#default_value']),
];
$element['value']['#weight'] = 10;
$element['value']['#description'] = '';
$field_name = $this->fieldDefinition
->getName();
$element['container']['#type'] = 'container';
$element['container'] = [
'#type' => 'container',
'#states' => [
'visible' => [
':input[name="' . $field_name . '[' . $delta . '][has_value]"]' => [
'checked' => TRUE,
],
],
],
];
$element['container']['value'] = $element['value'];
unset($element['value']);
return $element;
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
foreach ($values as &$item) {
$item['value'] = $item['container']['value'];
unset($item['has_value']);
unset($item['container']);
if (empty($item['value'])) {
continue;
}
if ($item['value'] instanceof DrupalDateTime) {
$item['value'] = $item['value']
->getTimestamp();
}
elseif (isset($item['value']['object']) && $item['value']['object'] instanceof DrupalDateTime) {
$item['value'] = $item['value']['object']
->getTimestamp();
}
}
return $values;
}
}