Skip to content

Commit

Permalink
Added “Registration deadline” to date
Browse files Browse the repository at this point in the history
  • Loading branch information
rimi-itk committed Feb 27, 2025
1 parent 9c784d4 commit acd47a6
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ before starting to add changes.

## [Unreleased]

* [PR-69](https://github.com/itk-dev/itk_pretix/pull/69)
Added “Registration deadline” to date

## 1.2.2

- Allow nullabale pretix client
Expand Down
72 changes: 72 additions & 0 deletions itk_pretix.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Installation file for the itk_pretix module.
*/

use Drupal\Core\Entity\Sql\SqlContentEntityStorage;

/**
* Implements hook_schema().
*/
Expand Down Expand Up @@ -93,3 +95,73 @@ function itk_pretix_schema() {
],
];
}

/**
* Implements hook_update_N().
*/
function itk_pretix_update_10101(&$sandbox) {
_itk_pretix_field_type_add_properties(
'pretix_date',
[
'registration_deadline_value' => [
'description' => 'The registration deadline value.',
'type' => 'varchar',
'length' => 20,
],
]
);
}

/**
* Lifted from https://drupal.stackexchange.com/a/295485.
*/
function _itk_pretix_field_type_add_properties(string $field_type, array $columns): void {
$database_schema = \Drupal::database()->schema();

$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();

// First, get all fields of the provided field type.
$field_map = \Drupal::service('entity_field.manager')
->getFieldMapByFieldType($field_type);

// Process per entity type (i.e., node, paragraph) all fields.
foreach ($field_map as $entity_type_id => $fields) {
$entity_type_storage = \Drupal::entityTypeManager()
->getStorage($entity_type_id);

// Database schema can only be updated, if the values are stored in the
// database, of course.
if ($entity_type_storage instanceof SqlContentEntityStorage) {
foreach (array_keys($fields) as $field_name) {
$field_storage_definition = $entity_definition_update_manager->getFieldStorageDefinition($field_name,
$entity_type_id);

// Get the table and column names, according to the field definition.
$table_mapping = $entity_type_storage->getTableMapping([
$field_name => $field_storage_definition,
]);
$table_names = $table_mapping->getDedicatedTableNames();
$table_columns = $table_mapping->getColumnNames($field_name);

// Check if, for the property, the corresponding table exists in the
// database. If not, create the table column (according to the schema
// provided by the field definition).
foreach ($table_names as $table_name) {
$table_exists = $database_schema->tableExists($table_name);
foreach ($columns as $property => $spec) {
$field_exists = $database_schema->fieldExists($table_name,
$table_columns[$property]);

if ($table_exists && !$field_exists) {
$database_schema->addField($table_name, $table_columns[$property],
$spec);
}
}
}

// Reflect any changes made to the field storage definition.
$entity_definition_update_manager->updateFieldStorageDefinition($field_storage_definition);
}
}
}
}
2 changes: 1 addition & 1 deletion src/NodeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private function getPretixDates(NodeInterface $node): ?FieldItemListInterface {

if (NULL !== $items) {
foreach ($items as $item) {
foreach (['time_from', 'time_to'] as $key) {
foreach (['registration_deadline', 'time_from', 'time_to'] as $key) {
if (isset($item->{$key}) && is_string($item->{$key})) {
$item->{$key} = new DrupalDateTime($item->{$key});
}
Expand Down
1 change: 1 addition & 0 deletions src/Plugin/Field/FieldFormatter/PretixDateFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
'entity' => $items->getEntity(),
'location' => $item->location,
'address' => $item->address,
'registration_deadline' => $item->registration_deadline,
'time_from' => $item->time_from,
'time_to' => $item->time_to,
'spots' => $item->spots,
Expand Down
16 changes: 16 additions & 0 deletions src/Plugin/Field/FieldType/PretixDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* @property string uuid
* @property string location
* @property string address
* @property DateTimeComputed registration_deadline
* @property DateTimeComputed time_from
* @property DateTimeComputed time_to
* @property int spots
Expand All @@ -45,6 +46,16 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel
->setLabel(new TranslatableMarkup('Address'))
->setRequired(TRUE);

$properties['registration_deadline_value'] = DataDefinition::create('datetime_iso8601')
->setLabel(t('Registration deadline value'))
->setRequired(TRUE);
$properties['registration_deadline'] = DataDefinition::create('any')
->setLabel(t('Computed registration deadline'))
->setDescription(t('The computed registration deadline DateTime object.'))
->setComputed(TRUE)
->setClass(DateTimeComputed::class)
->setSetting('date source', 'registration_deadline_value');

$properties['time_from_value'] = DataDefinition::create('datetime_iso8601')
->setLabel(t('Time from value'))
->setRequired(TRUE);
Expand Down Expand Up @@ -91,6 +102,11 @@ public static function schema(FieldStorageDefinitionInterface $field_definition)
'type' => 'varchar',
'length' => 255,
],
'registration_deadline_value' => [
'description' => 'The registration deadline value.',
'type' => 'varchar',
'length' => 20,
],
'time_from_value' => [
'description' => 'The time from value.',
'type' => 'varchar',
Expand Down
32 changes: 30 additions & 2 deletions src/Plugin/Field/FieldWidget/PretixDateWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
'#required' => $element['#required'],
];

$element['registration_deadline_value'] = [
'#title' => t('Registration deadline'),
'#type' => 'datetime',
'#default_value' => NULL,
'#date_increment' => 1,
'#date_timezone' => date_default_timezone_get(),
'#required' => $element['#required'],
];

if ($items[$delta]->registration_deadline) {
/** @var \Drupal\Core\Datetime\DrupalDateTime $registration_deadline */
$registration_deadline = $items[$delta]->registration_deadline;
$element['registration_deadline_value']['#default_value'] = $this->createDefaultValue($registration_deadline, $element['registration_deadline_value']['#date_timezone']);
}

$element['time_from_value'] = [
'#title' => t('Start time'),
'#type' => 'datetime',
Expand Down Expand Up @@ -225,6 +240,14 @@ public function massageFormValues(array $values, array $form, FormStateInterface
$user_timezone = new \DateTimeZone(date_default_timezone_get());

foreach ($values as &$item) {
if (!empty($item['registration_deadline_value']) && $item['registration_deadline_value'] instanceof DrupalDateTime) {
/** @var \Drupal\Core\Datetime\DrupalDateTime $time_from */
$registration_deadline = $item['registration_deadline_value'];

// Adjust the date for storage.
$item['registration_deadline_value'] = $registration_deadline->setTimezone($storage_timezone)->format($storage_format);
}

if (!empty($item['time_from_value']) && $item['time_from_value'] instanceof DrupalDateTime) {
/** @var \Drupal\Core\Datetime\DrupalDateTime $time_from */
$time_from = $item['time_from_value'];
Expand Down Expand Up @@ -341,12 +364,17 @@ public function errorElement(
* The complete form structure.
*/
public function validate(array &$element, FormStateInterface $form_state, array &$complete_form) {
$time_from = $element['time_from_value']['#value']['object'];
$time_to = $element['time_to_value']['#value']['object'];
$time_from = $element['time_from_value']['#value']['object'] ?? NULL;
$time_to = $element['time_to_value']['#value']['object'] ?? NULL;

if ($time_from instanceof DrupalDateTime && $time_to instanceof DrupalDateTime && $time_to < $time_from) {
$form_state->setError($element['time_to_value'], $this->t('The end time cannot be before the start time'));
}

$registration_deadline = $element['registration_deadline_value']['#value']['object'] ?? NULL;
if ($registration_deadline instanceof DrupalDateTime && $time_from instanceof DrupalDateTime && $time_from < $registration_deadline) {
$form_state->setError($element['registration_deadline_value'], $this->t('The registration deadline must be before the start time'));
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Pretix/EventHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private function synchronizePretixSubEvent(PretixDate $item, Event $event, NodeI
'active' => TRUE,
'is_public' => TRUE,
'date_admission' => NULL,
'presale_end' => NULL,
'presale_end' => $this->formatDate($item->registration_deadline),
'seating_plan' => NULL,
'seat_category_mapping' => (object) [],
]);
Expand Down

0 comments on commit acd47a6

Please sign in to comment.