Skip to content

Commit

Permalink
Issue #1: Make end date optional
Browse files Browse the repository at this point in the history
  • Loading branch information
webflo committed Apr 18, 2016
1 parent 15258bf commit 9634d24
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 30 deletions.
46 changes: 37 additions & 9 deletions src/Plugin/Field/FieldType/DateCombo.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;

/**
Expand All @@ -28,18 +29,41 @@
class DateCombo extends FieldItemBase {

/**
** {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
* {@inheritdoc}
*/
public static function defaultStorageSettings() {
$settings = parent::defaultStorageSettings();
$settings['require_enddate'] = FALSE;
return $settings;
}

/**
* @inheritDoc
*/
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = parent::storageSettingsForm($form, $form_state, $has_data);

$element['require_enddate'] = array(
'#type' => 'checkbox',
'#title' => t('Require an end date'),
'#default_value' => $this->getSetting('require_enddate'),
'#disabled' => $has_data,
);

return $element;
}

/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('datetime_iso8601')
->setLabel(t('Start Date value'))
->setRequired(TRUE);

$properties['value2'] = DataDefinition::create('datetime_iso8601')
->setLabel(t('End Date value'))
->setRequired(TRUE);

->setRequired($field_definition->getSetting('require_enddate'));

$properties['date'] = DataDefinition::create('any')
->setLabel(t('Computed start date'))
Expand All @@ -55,12 +79,12 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel
->setClass('\Drupal\datetime\DateTimeComputed')
->setSetting('date source', 'value2');

return $properties;
return $properties;
}

/**
** {@inheritdoc}
*/
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return array(
'columns' => array(
Expand Down Expand Up @@ -101,7 +125,10 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
public function isEmpty() {
$value = $this->get('value')->getValue();
$value2 = $this->get('value2')->getValue();
return $value === NULL || $value === '' || $value2 === NULL || $value2 === '';
if ($this->getSetting('require_enddate')) {
return $value === NULL || $value === '' || $value2 === NULL || $value2 === '';
}
return $value === NULL || $value === '';
}

/**
Expand All @@ -117,4 +144,5 @@ public function onChange($property_name, $notify = TRUE) {
}
parent::onChange($property_name, $notify);
}

}
46 changes: 25 additions & 21 deletions src/Plugin/Field/FieldWidget/DateComboDefaultWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
'#date_time_format' => $time_format,
'#date_time_element' => $time_type,
'#date_time_callbacks' => array(),

);
$element['value2']['#required'] = $this->fieldDefinition->getSetting('require_enddate');

if ($items[$delta]->date2) {
$date = $items[$delta]->date2;
Expand Down Expand Up @@ -130,30 +132,32 @@ public function massageFormValues(array $values, array $form, FormStateInterface
// DrupalDateTime object at this point. We need to convert it back to the
// storage timezone and format.
foreach ($values as &$item) {
if (!empty($item['value']) && !empty($item['value2']) && $item['value'] instanceof DrupalDateTime && $item['value2'] instanceof DrupalDateTime) {
$date = $item['value'];
$date2 = $item['value2'];
switch ($this->getFieldSetting('datetime_type')) {
case DateTimeItem::DATETIME_TYPE_DATE:
// If this is a date-only field, set it to the default time so the
// timezone conversion can be reversed.
datetime_date_default_time($date);
datetime_date_default_time($date2);
$format = DATETIME_DATE_STORAGE_FORMAT;
break;

default:
$format = DATETIME_DATETIME_STORAGE_FORMAT;
break;
}
// Adjust the date for storage.
$date->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
$date2->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
$item['value'] = $date->format($format);
$item['value2'] = $date2->format($format);
if (isset($item['value']) && $item['value'] instanceof DrupalDateTime) {
$item['value'] = $this->massageDateValue($item['value']);
}
if (isset($item['value2']) && $item['value2'] instanceof DrupalDateTime) {
$item['value2'] = $this->massageDateValue($item['value2']);
}
}
return $values;
}

protected function massageDateValue(DrupalDateTime $date) {
switch ($this->getFieldSetting('datetime_type')) {
case DateTimeItem::DATETIME_TYPE_DATE:
// If this is a date-only field, set it to the default time so the
// timezone conversion can be reversed.
datetime_date_default_time($date);
$format = DATETIME_DATE_STORAGE_FORMAT;
break;

default:
$format = DATETIME_DATETIME_STORAGE_FORMAT;
break;
}
// Adjust the date for storage.
$date->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
return $date->format($format);
}

}

0 comments on commit 9634d24

Please sign in to comment.