Skip to content

Commit

Permalink
Update event entities to use Drupal datetime fields (#46)
Browse files Browse the repository at this point in the history
* Update event entities to use Drupal datetime fields

* Event start and end dates now use UTC timezone

* Event start and end dates now are a single datetime range field

* Add dependency on module “Fullcalendar library”

* Add fullcalendar_library to makefile

* Don't fail build on php 7.2

* Change timezone to UTC

* Remove bat_fullcalendar.install

* Migrate event's dates on the new field

* Use short array syntax

* Event start and end dates now are a single datetime range field
  • Loading branch information
acrollet authored Feb 15, 2018
1 parent 1c1de35 commit 64a3f9a
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 327 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ php:
- 7.1
- 7.2

matrix:
allow_failures:
- php: 7.2

addons:
chrome: stable

Expand Down
1 change: 1 addition & 0 deletions modules/bat_event/bat_event.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ package: bat

dependencies:
- bat_unit
- datetime_range
36 changes: 36 additions & 0 deletions modules/bat_event/bat_event.install
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* the entity types.
*/

use Drupal\Core\Database\Database;

/**
* Implements hook_requirements().
*/
Expand Down Expand Up @@ -34,3 +36,37 @@ function bat_event_requirements($phase) {
];
}
}

/**
* Migrate event's dates on the new field.
*/
function bat_event_update_8001() {
$events = db_select('event', 'e')
->fields('e', [])
->condition('start', '', '<>')
->execute()
->fetchAll();

foreach ($events as $event) {
$event_object = bat_event_load($event->id);

$start_date = new DateTime();
$start_date->setTimestamp($event->start);

$end_date = new DateTime();
$end_date->setTimestamp($event->end);

$event_object->setStartDate($start_date);
$event_object->setEndDate($end_date);

$event_object->save();
}
}

/**
* Drop old "start" and "end" columns on event table.
*/
function bat_event_update_8002() {
Database::getConnection()->schema()->dropField('event', 'start');
Database::getConnection()->schema()->dropField('event', 'end');
}
54 changes: 54 additions & 0 deletions modules/bat_event/bat_event.module
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,60 @@ function bat_event_type_add_event_state_reference($entity) {
}
}

/**
* Create "Event Dates" field.
*/
function bat_event_type_add_event_dates_field($entity) {
$entity_info = \Drupal::entityTypeManager()->getDefinition($entity->target_entity_type);
$field_name = 'event_dates';

$field_storage = FieldStorageConfig::loadByName('bat_event', $field_name);
$field = FieldConfig::loadByName('bat_event', $entity->id(), $field_name);

if (empty($field_storage)) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'bat_event',
'type' => 'daterange',
'cardinality' => 1,
'locked' => 1,
'settings' => [
'datetime_type' => 'datetime',
],
]);
$field_storage->save();
}

if (empty($field)) {
$field = FieldConfig::create([
'field_storage' => $field_storage,
'entity_type' => 'bat_event',
'label' => 'Event Dates',
'bundle' => $entity->id(),
'required' => FALSE,
'settings' => [
'datetime_type' => 'datetime',
],
]);
$field->save();

$form_display = \Drupal::entityTypeManager()->getStorage('entity_form_display')->load('bat_event.' . $entity->id() . '.default');
if (!$form_display) {
$form_display = EntityFormDisplay::create([
'targetEntityType' => 'bat_event',
'bundle' => $entity->id(),
'mode' => 'default',
'status' => TRUE,
]);
}
$form_display->setComponent($field_name, [
'type' => 'daterange_default',
'weight' => 1,
]);
$form_display->save();
}
}

/**
* Create fields of type 'Entity Reference' to reference the target entity.
*
Expand Down
54 changes: 16 additions & 38 deletions modules/bat_event/src/Entity/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,30 +145,38 @@ public function setUnit(UnitInterface $unit) {
* {@inheritdoc}
*/
public function getStartDate() {
$date = new \DateTime();
return $date->setTimestamp($this->get('start')->value);
$date = new \DateTime($this->get('event_dates')->value);
return $date;
}

/**
* {@inheritdoc}
*/
public function getEndDate() {
$date = new \DateTime();
return $date->setTimestamp($this->get('end')->value);
$date = new \DateTime($this->get('event_dates')->end_value);
return $date;
}

/**
* {@inheritdoc}
*/
public function setStartDate(\DateTime $date) {
$this->set('start', $date->getTimestamp());
$value = [
'value' => $date->format('Y-m-d\TH:i:00'),
'end_value' => $this->getEndDate()->format('Y-m-d\TH:i:00'),
];
$this->set('event_dates', $value);
}

/**
* {@inheritdoc}
*/
public function setEndDate(\DateTime $date) {
$this->set('end', $date->getTimestamp());
$value = [
'value' => $this->getStartDate()->format('Y-m-d\TH:i:00'),
'end_value' => $date->format('Y-m-d\TH:i:00'),
];
$this->set('event_dates', $value);
}

/**
Expand Down Expand Up @@ -203,7 +211,7 @@ public function save() {

$this->batStoreSave($unit,
$entity_original->getStartDate(),
$entity_original->getEndDate(),
$entity_original->getEndDate()->sub(new \DateInterval('PT1M')),
$event_type->id(),
$event_type->getEventGranularity(),
$unit->getDefaultValue(),
Expand Down Expand Up @@ -250,7 +258,7 @@ public function save() {

$this->batStoreSave($unit,
$this->getStartDate(),
$this->getEndDate(),
$this->getEndDate()->sub(new \DateInterval('PT1M')),
$event_type->id(),
$event_type->getEventGranularity(),
$event_value,
Expand Down Expand Up @@ -311,36 +319,6 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));

$fields['start'] = BaseFieldDefinition::create('created')
->setLabel(t('Start Date'))
->setDescription(t('The time that this event starts.'))
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'timestamp',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE)
->setRequired(TRUE);

$fields['end'] = BaseFieldDefinition::create('created')
->setLabel(t('End Date'))
->setDescription(t('The time that this event ends.'))
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'timestamp',
'weight' => 1,
])
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 1,
])
->setDisplayConfigurable('form', TRUE)
->setRequired(TRUE);

$fields['type'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Type'))
->setDescription(t('The event type.'))
Expand Down
3 changes: 3 additions & 0 deletions modules/bat_event/src/Entity/EventType.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public function save() {
// Create all tables necessary for this Event Type.
bat_event_create_event_type_schema($this->id());

// Create a field of type "Date range" for event dates.
bat_event_type_add_event_dates_field($this);

// Create a field of type 'Entity Reference' to reference a Bat Unit.
bat_event_type_add_target_entity_field($this);

Expand Down
24 changes: 0 additions & 24 deletions modules/bat_event/src/Entity/EventViewsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,6 @@ public function getViewsData() {
'help' => $this->t('The Event ID.'),
];

$data['event']['start_date'] = [
'title' => $this->t('Start Date'),
'help' => $this->t("A event's start date."),
'field' => [
'float' => TRUE,
'id' => 'bat_event_handler_date_field',
'click sortable' => TRUE,
],
'filter' => [
'id' => 'bat_event_handler_date_filter',
],
];
$data['event']['end_date'] = [
'title' => $this->t('End Date'),
'help' => $this->t("A event's end date."),
'field' => [
'float' => TRUE,
'id' => 'bat_event_handler_date_field',
'click sortable' => TRUE,
],
'filter' => [
'id' => 'bat_event_handler_date_filter',
],
];
$data['event']['type']['field'] = [
'title' => $this->t('Event Type'),
'help' => $this->t('The event type label.'),
Expand Down
36 changes: 16 additions & 20 deletions modules/bat_event/src/Entity/Form/EventForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,23 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$form['created']['#group'] = 'author';
}

if ($entity->isNew()) {
$form['start']['widget'][0]['value']['#default_value'] = '';
$form['end']['widget'][0]['value']['#default_value'] = '';
if ($event_type->getEventGranularity() == 'bat_daily') {
$form['event_dates']['widget'][0]['value']['#date_time_element'] = 'none';
$form['event_dates']['widget'][0]['end_value']['#date_time_element'] = 'none';
}
else {
$form['end']['widget'][0]['value']['#default_value']->add(new \DateInterval('PT1M'));
$form['event_dates']['widget'][0]['value']['#date_increment'] = 60;
$form['event_dates']['widget'][0]['end_value']['#date_increment'] = 60;
}

unset($form['start']['widget'][0]['value']['#description']);
unset($form['end']['widget'][0]['value']['#description']);
$form['event_dates']['widget'][0]['value']['#date_timezone'] = 'UTC';
$form['event_dates']['widget'][0]['end_value']['#date_timezone'] = 'UTC';

if ($event_type->getEventGranularity() == 'bat_daily') {
$form['start']['widget'][0]['value']['#date_time_element'] = 'none';
$form['end']['widget'][0]['value']['#date_time_element'] = 'none';
if (isset($form['event_dates']['widget'][0]['value']['#default_value'])) {
$form['event_dates']['widget'][0]['value']['#default_value']->setTimezone(new \DateTimeZone('UTC'));
}
else {
$form['start']['widget'][0]['value']['#date_increment'] = 60;
$form['end']['widget'][0]['value']['#date_increment'] = 60;
if (isset($form['event_dates']['widget'][0]['end_value']['#default_value'])) {
$form['event_dates']['widget'][0]['end_value']['#default_value']->setTimezone(new \DateTimeZone('UTC'));
}

if (\Drupal::request()->query->get(MainContentViewSubscriber::WRAPPER_FORMAT) == 'drupal_ajax') {
Expand All @@ -130,12 +129,12 @@ public function validateForm(array &$form, FormStateInterface $form_state) {

$values = $form_state->getValues();

$start_date = new \DateTime($values['start'][0]['value']->format('Y-m-d H:i:s'));
$end_date = new \DateTime($values['end'][0]['value']->format('Y-m-d H:i:s'));
$start_date = new \DateTime($values['event_dates'][0]['value']->format('Y-m-d H:i:s'));
$end_date = new \DateTime($values['event_dates'][0]['end_value']->format('Y-m-d H:i:s'));

// The end date must be greater or equal than start date.
if ($end_date < $start_date) {
$form_state->setErrorByName('end', t('End date must be on or after the start date.'));
$form_state->setErrorByName('event_dates', t('End date must be on or after the start date.'));
}

$event_type = bat_event_type_load($this->entity->bundle());
Expand Down Expand Up @@ -183,17 +182,14 @@ public function save(array $form, FormStateInterface $form_state) {
$event = $this->entity;
$event_type = bat_event_type_load($event->bundle());

$end_date = $event->getEndDate();
if ($event_type->getEventGranularity() == 'bat_daily') {
$start_date = $event->getStartDate()->setTime(0, 0);
$event->setStartDate($start_date);

$end_date->setTime(0, 0);
$end_date = $event->getEndDate()->setTime(0, 0);
$event->setEndDate($end_date);
}

$end_date->sub(new \DateInterval('PT1M'));
$event->setEndDate($end_date);

$status = $event->save();

switch ($status) {
Expand Down
6 changes: 3 additions & 3 deletions modules/bat_event/src/EventListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ public function buildHeader() {
],
'start_date' => [
'data' => $this->t('Start Date'),
'field' => 'start',
'field' => 'event_dates',
'specifier' => 'start',
'class' => [RESPONSIVE_PRIORITY_LOW],
],
'end_date' => [
'data' => $this->t('End Date'),
'field' => 'end',
'field' => 'event_dates',
'specifier' => 'end',
'class' => [RESPONSIVE_PRIORITY_LOW],
],
Expand All @@ -112,7 +112,7 @@ public function buildRow(EntityInterface $entity) {

$row['id'] = $entity->id();
$row['start_date'] = $entity->getStartDate()->format($date_format);
$row['end_date'] = $entity->getEndDate()->add(new \DateInterval('PT1M'))->format($date_format);
$row['end_date'] = $entity->getEndDate()->format($date_format);
$row['type'] = bat_event_type_load($entity->bundle())->label();
return $row + parent::buildRow($entity);
}
Expand Down
Loading

0 comments on commit 64a3f9a

Please sign in to comment.