RollingInterval.php in Recurring Time Period 8
File
src/Plugin/RecurringPeriod/RollingInterval.php
View source
<?php
namespace Drupal\recurring_period\Plugin\RecurringPeriod;
use Drupal\Core\Form\FormStateInterface;
class RollingInterval extends RecurringPeriodBase {
public function defaultConfiguration() {
return [
'interval' => [
'period' => '',
'interval' => '',
],
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$config = $this
->getConfiguration();
$form['interval'] = [
'#type' => 'interval',
'#title' => 'Interval',
'#default_value' => $config['interval'],
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValue($form['#parents']);
$this->configuration['interval'] = $values['interval'];
}
public function calculateDate(\DateTimeImmutable $start) {
return $this
->calculateEnd($start);
}
public function calculateStart(\DateTimeImmutable $date) {
return $date;
}
public function calculateEnd(\DateTimeImmutable $start) {
$config = $this
->getConfiguration();
$interval_configuration = $config['interval'];
$interval_plugin_id = $interval_configuration['period'];
$interval_plugin_definition = $this->pluginManagerIntervals
->getDefinition($interval_plugin_id);
$value = $interval_configuration['interval'] * $interval_plugin_definition['multiplier'];
$date_interval = \DateInterval::createFromDateString($value . ' ' . $interval_plugin_definition['php']);
return $start
->add($date_interval);
}
public function getPeriodLabel(\DateTimeImmutable $start, \DateTimeImmutable $end) {
$config = $this
->getConfiguration();
$interval_configuration = $config['interval'];
$interval_plugin_id = $interval_configuration['period'];
$interval_plugin_definition = $this->pluginManagerIntervals
->getDefinition($interval_plugin_id);
return $this
->t("@count @interval from @start-date", [
'@count' => $config['interval']['interval'],
'@interval' => $config['interval']['interval'] == 1 ? $interval_plugin_definition['singular'] : $interval_plugin_definition['plural'],
'@start-date' => $start
->format(\DateTimeInterface::RSS),
]);
}
}
Classes
Name |
Description |
RollingInterval |
Provides a period based on a rolling interval from the start date. |