Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: openscholar/views_ical
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2.4
Choose a base ref
...
head repository: openscholar/views_ical
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 8.x-2.x
Choose a head ref
  • 4 commits
  • 5 files changed
  • 2 contributors

Commits on Oct 7, 2021

  1. Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    3b32a99 View commit details
  2. Copy the full SHA
    035ce10 View commit details
  3. Ical fixes.

    uttkarsh-26 committed Oct 7, 2021
    Copy the full SHA
    5b7330d View commit details

Commits on Nov 7, 2023

  1. Copy the full SHA
    741ed53 View commit details
Showing with 82 additions and 33 deletions.
  1. +75 −27 src/Plugin/views/style/Ical.php
  2. +2 −1 src/ViewsIcalHelper.php
  3. +2 −2 templates/views-view-ical-fields.html.twig
  4. +1 −1 views_ical.info.yml
  5. +2 −2 views_ical.module
102 changes: 75 additions & 27 deletions src/Plugin/views/style/Ical.php
Original file line number Diff line number Diff line change
@@ -2,13 +2,12 @@

namespace Drupal\views_ical\Plugin\views\style;

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;
use Eluceo\iCal\Component\Event;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
@@ -87,41 +86,53 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
/** @var array $field_options */
$field_options = $this->displayHandler->getFieldLabels();

$form['date_field'] = array(
$form['date_field'] = [
'#type' => 'select',
'#title' => $this->t('Date field'),
'#options' => $field_options,
'#default_value' => $this->options['date_field'],
'#description' => $this->t('Please identify the field to use as the iCal date for each item in this view.'),
'#required' => TRUE,
);
];

$form['summary_field'] = array(
$form['summary_field'] = [
'#type' => 'select',
'#title' => $this->t('SUMMARY field'),
'#options' => $field_options,
'#default_value' => $this->options['summary_field'],
'#description' => $this->t('You may optionally change the SUMMARY component for each event in the iCal output. Choose which text field you would like to be output as the SUMMARY.'),
);
];

$form['location_field'] = array(
$form['location_field'] = [
'#type' => 'select',
'#title' => $this->t('LOCATION field'),
'#options' => $field_options,
'#default_value' => $this->options['location_field'],
'#description' => $this->t('You may optionally include a LOCATION component for each event in the iCal output. Choose which text field you would like to be output as the LOCATION.'),
);
];

$form['description_field'] = array(
$form['description_field'] = [
'#type' => 'select',
'#title' => $this->t('DESCRIPTION field'),
'#options' => $field_options,
'#default_value' => $this->options['description_field'],
'#description' => $this->t('You may optionally include a DESCRIPTION component for each event in the iCal output. Choose which text field you would like to be output as the DESCRIPTION.'),
);
];
}

public function attachTo(array &$build, $display_id, Url $feed_url, $title) {
/**
* Attach to.
*
* @param array $build
* Build array.
* @param string $display_id
* Display id.
* @param \Drupal\Core\Url $feed_url
* Feed url.
* @param string $title
* Title.
*/
public function attachTo(array &$build, string $display_id, Url $feed_url, string $title) {
$url_options = [];
$input = $this->view->getExposedInput();
if ($input) {
@@ -150,26 +161,19 @@ 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();

$events = [];
$user_timezone = \drupal_get_user_timezone();
$date_field_type = $this->getDateFieldType();

// 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);
}
$events = [];
$timezone = $this->getTimezone();

$ids = [];
foreach ($this->view->result as $row_index => $row) {
// Distinct query not working in views settings.
// Prevent recurring event to export multiple times.
if (isset($ids[$row->_entity->id()])) {
continue;
}
// Use date_recur's API to generate the events.
// Recursive events will be automatically handled here.
if ($date_field_type === 'date_recur') {
@@ -178,6 +182,7 @@ public function render() {
else {
$this->helper->addEvent($events, $row->_entity, $timezone, $this->options);
}
$ids[$row->_entity->id()] = $row->_entity->id();
}

$build = [
@@ -190,4 +195,47 @@ 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 Timezone.
*
* @return \DateTimeZone
* Timezone.
*/
protected function getTimezone(): \DateTimeZone {
$user_timezone = \date_default_timezone_get();
$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
@@ -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 */
4 changes: 2 additions & 2 deletions templates/views-view-ical-fields.html.twig
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@
*
* @ingroup themeable
*/
#}BEGIN:VEVENT{{ "\r\n" }}{% for field in fields -%}{% spaceless %}
#}BEGIN:VEVENT{{ "\r\n" }}{% for field in fields -%}{% apply spaceless %}
{{ field.separator }}
{%- if field.wrapper_element -%}
<{{ field.wrapper_element }}{{ field.wrapper_attributes }}>
@@ -50,4 +50,4 @@
{%- endif %}
{%- if field.wrapper_element -%}
</{{ field.wrapper_element }}>
{%- endif %}{% endspaceless %}{{ "\r\n" }}{%- endfor %}END:VEVENT{{ "\r\n" }}{# skip line ending #}
{%- endif %}{% endapply %}{{ "\r\n" }}{%- endfor %}END:VEVENT{{ "\r\n" }}{# skip line ending #}
2 changes: 1 addition & 1 deletion views_ical.info.yml
Original file line number Diff line number Diff line change
@@ -2,6 +2,6 @@ name: Views iCal
type: module
description: Provides an iCal Views style plugin.
package: Views
core_version_requirement: ^8 || ^9
core_version_requirement: ^8 || ^9 || ^10
dependencies:
- drupal:views
4 changes: 2 additions & 2 deletions views_ical.module
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ function views_ical_preprocess_views_view_ical(&$variables) {
$variables['title'] = $title;

$calendar = new Calendar('-//Drupal iCal API//EN');
$user_timezone = \drupal_get_user_timezone();
$user_timezone = date_default_timezone_get();

/** @var string $user_timezone */
$v_timezone = new Timezone($user_timezone);
@@ -51,6 +51,6 @@ function views_ical_preprocess_views_view_ical(&$variables) {
* - view: A View object.
*/
function views_ical_preprocess_views_view_ical_fields(&$variables) {
module_load_include('inc', 'views', 'views.theme');
Drupal::moduleHandler()->loadInclude('views', 'inc', $name = 'views.theme');
template_preprocess_views_view_fields($variables);
}