Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix when using date recur field type #4

Open
wants to merge 3 commits into
base: 8.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 43 additions & 16 deletions src/Plugin/views/style/Ical.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\date_recur\Plugin\views\field\DateRecurDate;
use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\Core\Url;
use Drupal\views_ical\ViewsIcalHelperInterface;
Expand Down Expand Up @@ -150,24 +151,10 @@ public function render() {
trigger_error('Drupal\views_ical\Plugin\views\style\Ical: Missing row plugin', E_WARNING);
return [];
}
/** @var \Drupal\Core\Field\FieldDefinitionInterface[] $field_storage_definitions */
$field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($this->view->field[$this->options['date_field']]->definition['entity_type']);
$date_field_definition = $field_storage_definitions[$this->view->field[$this->options['date_field']]->definition['field_name']];
/** @var string $date_field_type */
$date_field_type = $date_field_definition->getType();
$date_field_type = $this->getDateFieldType();

$events = [];
$user_timezone = \drupal_get_user_timezone();

// Make sure the events are made as per the configuration in view.
/** @var string $timezone_override */
$timezone_override = $this->view->field[$this->options['date_field']]->options['settings']['timezone_override'];
if ($timezone_override) {
$timezone = new \DateTimeZone($timezone_override);
}
else {
$timezone = new \DateTimeZone($user_timezone);
}
$timezone = $this->getTimezone();

foreach ($this->view->result as $row_index => $row) {
// Use date_recur's API to generate the events.
Expand All @@ -190,4 +177,44 @@ public function render() {
return $build;
}

/**
* Get Date field type value.
*
* @return string
* Date field type.
*/
protected function getDateFieldType(): string {
$date_field_name = $this->options['date_field'];
$view_date_field = $this->view->field[$date_field_name];
if ($view_date_field instanceof DateRecurDate) {
return 'date_recur';
}
$entity_type = $view_date_field->definition['entity_type'];
/** @var \Drupal\Core\Field\FieldDefinitionInterface[] $field_storage_definitions */
$field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type);
$date_field_definition = $field_storage_definitions[$date_field_name];
/** @var string $date_field_type */
return $date_field_definition->getType();
}

/**
* @return \DateTimeZone
*/
protected function getTimezone(): \DateTimeZone {
$user_timezone = \drupal_get_user_timezone();
$view_field = $this->view->field[$this->options['date_field']];
$timezone = new \DateTimeZone($user_timezone);
if (empty($view_field->options['settings']['timezone_override'])) {
return $timezone;
}

// Make sure the events are made as per the configuration in view.
/** @var string $timezone_override */
$timezone_override = $view_field->options['settings']['timezone_override'];
if ($timezone_override) {
$timezone = new \DateTimeZone($timezone_override);
}
return $timezone;
}

}
3 changes: 2 additions & 1 deletion src/ViewsIcalHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ public function addEvent(array &$events, ContentEntityInterface $entity, \DateTi
* {@inheritdoc}
*/
public function addDateRecurEvent(array &$events, ContentEntityInterface $entity, \DateTimeZone $timezone, array $field_mapping): void {
$field_name = preg_replace('/\_value$/', '', $field_mapping['date_field']);
/** @var \Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem[] $field_items */
$field_items = $entity->{$field_mapping['date_field']};
$field_items = $entity->{$field_name};

foreach ($field_items as $index => $item) {
/** @var \Drupal\date_recur\DateRange[] $occurrences */
Expand Down