diff --git a/calendar.services.yml b/calendar.services.yml index a651067..e6a0da9 100644 --- a/calendar.services.yml +++ b/calendar.services.yml @@ -4,3 +4,7 @@ services: arguments: ['@renderer'] tags: - { name: twig.extension, priority: 100 } + calendar.date_recur.service: + class: Drupal\calendar\DateRecurService + calls: + - [setOptionalDependency, ['@?date_recur.occurrences']] diff --git a/src/DateRecurService.php b/src/DateRecurService.php new file mode 100644 index 0000000..0ed35dc --- /dev/null +++ b/src/DateRecurService.php @@ -0,0 +1,31 @@ +optionalDependency = $optionalDependency; + return $this; + } + + public function getOptionalDependency() { + if (isset($this->optionalDependency) && $this->optionalDependency instanceof DateRecurOccurrences) { + return $this->optionalDependency; + } + return NULL; + } + +} \ No newline at end of file diff --git a/src/Plugin/views/row/Calendar.php b/src/Plugin/views/row/Calendar.php index 0eedca0..bbc2273 100644 --- a/src/Plugin/views/row/Calendar.php +++ b/src/Plugin/views/row/Calendar.php @@ -19,6 +19,7 @@ use Drupal\views\Views; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\calendar\DateRecurService; /** * Plugin which creates a Calendar row object. @@ -82,6 +83,13 @@ class Calendar extends RowPluginBase { */ protected $fieldManager; + /** + * The (optional) DateRecur service + * + * @var \Drupal\date_recur\DateRecurOccurrences + */ + protected $dateRecurOccurrences; + /** * Constructs a Calendar row plugin object. * @@ -95,13 +103,16 @@ class Calendar extends RowPluginBase { * The date formatter service. * @param \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager * The entity field manager service. + * @param \Drupal\calendar\DateRecurService $dateRecurService + * The date recur occurences service. */ public function __construct( - array $configuration, $plugin_id, $plugin_definition, DateFormatter $date_formatter, EntityFieldManagerInterface $field_manager) { + array $configuration, $plugin_id, $plugin_definition, DateFormatter $date_formatter, EntityFieldManagerInterface $field_manager, DateRecurService $dateRecurService) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->dateFormatter = $date_formatter; $this->fieldManager = $field_manager; + $this->dateRecurService = $dateRecurService; } /** @@ -113,7 +124,8 @@ class Calendar extends RowPluginBase { $plugin_id, $plugin_definition, $container->get('date.formatter'), - $container->get('entity_field.manager') + $container->get('entity_field.manager'), + $container->get('calendar.date_recur.service') ); } @@ -411,18 +423,6 @@ class Calendar extends RowPluginBase { // of them to find the right values for this view result. foreach ($this->dateFields as $field_name => $info) { - // Check if we may have RecurringDate items. - // We set a flag to use later in explodeValues function. - $moduleHandler = \Drupal::service('module_handler'); - if ($moduleHandler->moduleExists('date_recur')) { - $field_argument_config = \Drupal::entityTypeManager() - ->getStorage('field_storage_config') - ->load($this->view->getBaseEntityType()->id() . '.' . $field_name); - if ($field_argument_config->getTypeProvider() === 'date_recur') { - $recurring = TRUE; - } - } - // Clone this entity so we can change it's values without altering other // occurrences of this entity on the same page, for example in an // "Upcoming" block. @@ -507,11 +507,10 @@ class Calendar extends RowPluginBase { // @todo implement $event->date_id = $entity->date_id[0]; - // For recurring dates pass original entity and storage_format. - // We use these in explode function. - if ($recurring) { + // For recurring dates pass original argument. + // We use this in explode function. + if($this->dateRecurService->getOptionalDependency()) { $event->recurring = $entity->get($field_name)[0]; - $event->storage_format = $storage_format; } // We are working with an array of partially rendered items @@ -568,18 +567,15 @@ class Calendar extends RowPluginBase { */ public function explodeValues(CalendarEvent $calendar_event) { $rows = $events = []; - $dateInfo = $this->dateArgument->view->dateInfo; - if ($calendar_event->recurring instanceof \Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem) { + if (!is_null($calendar_event->recurring)) { $occurrenceHandler = $calendar_event->recurring->getHelper(); $occurrences = $occurrenceHandler->getOccurrences($dateInfo->getMinDate(), $dateInfo->getMaxDate(), NULL); foreach ($occurrences as $occurrence) { $event_recur = clone $calendar_event; - $item_start_date = \DateTime::createFromFormat($calendar_event->storage_format, $occurrence->getStart()->format('Y-m-d\Th:i:s'), $calendar_event->getTimezone()); - $item_end_date = \DateTime::createFromFormat($calendar_event->storage_format, $occurrence->getEnd()->format('Y-m-d\Th:i:s'), $calendar_event->getTimezone()); - $event_recur->setStartDate($item_start_date); - $event_recur->setEndDate($item_end_date); + $event_recur->setStartDate($occurrence->getStart()); + $event_recur->setEndDate($occurrence->getEnd()); // @TODO set striping too? $events[] = $event_recur; }