abstract class IntervalBase in Commerce Recurring Framework 8
Base class for interval-based billing schedules.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\commerce_recurring\Plugin\Commerce\BillingSchedule\BillingScheduleBase implements BillingScheduleInterface
- class \Drupal\commerce_recurring\Plugin\Commerce\BillingSchedule\IntervalBase
- class \Drupal\commerce_recurring\Plugin\Commerce\BillingSchedule\BillingScheduleBase implements BillingScheduleInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of IntervalBase
File
- src/
Plugin/ Commerce/ BillingSchedule/ IntervalBase.php, line 13
Namespace
Drupal\commerce_recurring\Plugin\Commerce\BillingScheduleView source
abstract class IntervalBase extends BillingScheduleBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'trial_interval' => [],
'interval' => [
'number' => 1,
'unit' => 'month',
],
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['#attached']['library'][] = 'commerce_recurring/admin';
$form['trial_interval'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'interval',
],
],
'#open' => TRUE,
];
$form['trial_interval']['allow_trials'] = [
'#type' => 'checkbox',
'#title' => t('Allow free trials'),
'#default_value' => !empty($this->configuration['trial_interval']),
];
// Default the trial interval to the interval for easier configuration.
if (empty($this->configuration['trial_interval'])) {
$this->configuration['trial_interval'] = $this->configuration['interval'];
}
$form['trial_interval']['number'] = [
'#type' => 'number',
'#title' => $this
->t('Trial interval'),
'#default_value' => $this->configuration['trial_interval']['number'],
'#min' => 1,
'#states' => [
'visible' => [
':input[name="configuration[' . $this->pluginId . '][trial_interval][allow_trials]"]' => [
'checked' => TRUE,
],
],
],
];
$form['trial_interval']['unit'] = [
'#type' => 'select',
'#title' => $this
->t('Unit'),
'#title_display' => 'invisible',
'#options' => [
'hour' => $this
->t('Hour'),
'day' => $this
->t('Day'),
'week' => $this
->t('Week'),
'month' => $this
->t('Month'),
'year' => $this
->t('Year'),
],
'#default_value' => $this->configuration['trial_interval']['unit'],
'#states' => [
'visible' => [
':input[name="configuration[' . $this->pluginId . '][trial_interval][allow_trials]"]' => [
'checked' => TRUE,
],
],
],
];
$form['interval'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'interval',
],
],
'#open' => TRUE,
];
$form['interval']['number'] = [
'#type' => 'number',
'#title' => $this
->t('Interval'),
'#default_value' => $this->configuration['interval']['number'],
'#min' => 1,
'#required' => TRUE,
];
$form['interval']['unit'] = [
'#type' => 'select',
'#title' => $this
->t('Unit'),
'#title_display' => 'invisible',
'#options' => [
'hour' => $this
->t('Hour'),
'day' => $this
->t('Day'),
'week' => $this
->t('Week'),
'month' => $this
->t('Month'),
'year' => $this
->t('Year'),
],
'#default_value' => $this->configuration['interval']['unit'],
'#required' => TRUE,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
if (!$form_state
->getErrors()) {
$values = $form_state
->getValue($form['#parents']);
$this->configuration = [];
$this->configuration['trial_interval'] = [];
if (!empty($values['trial_interval']['allow_trials'])) {
$this->configuration['trial_interval'] = [
'number' => $values['trial_interval']['number'],
'unit' => $values['trial_interval']['unit'],
];
}
$this->configuration['interval'] = $values['interval'];
}
}
/**
* {@inheritdoc}
*/
public function allowTrials() {
return !empty($this->configuration['trial_interval']);
}
/**
* {@inheritdoc}
*/
public function generateTrialPeriod(DrupalDateTime $start_date) {
// Trial periods are always rolling (starting from the given start date).
$interval = new Interval($this->configuration['trial_interval']['number'], $this->configuration['trial_interval']['unit']);
return new BillingPeriod($start_date, $interval
->add($start_date));
}
/**
* Gets the current interval.
*
* @return \Drupal\commerce\Interval
* The interval.
*/
protected function getInterval() {
return new Interval($this->configuration['interval']['number'], $this->configuration['interval']['unit']);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BillingScheduleBase:: |
protected | property | The ID of the parent config entity. | |
BillingScheduleBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
BillingScheduleBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
BillingScheduleBase:: |
public | function |
Gets the billing schedule label. Overrides BillingScheduleInterface:: |
|
BillingScheduleBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
BillingScheduleBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
BillingScheduleBase:: |
public | function |
Constructs a new BillingScheduleBase object. Overrides PluginBase:: |
|
BillingScheduleInterface:: |
public | function | Generates the first billing period. | 2 |
BillingScheduleInterface:: |
public | function | Generates the next billing period. | 2 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
IntervalBase:: |
public | function |
Checks whether the billing schedule allows trials. Overrides BillingScheduleInterface:: |
|
IntervalBase:: |
public | function |
Form constructor. Overrides BillingScheduleBase:: |
1 |
IntervalBase:: |
public | function |
Gets default configuration for this plugin. Overrides BillingScheduleBase:: |
1 |
IntervalBase:: |
public | function |
Generates the trial period. Overrides BillingScheduleInterface:: |
|
IntervalBase:: |
protected | function | Gets the current interval. | |
IntervalBase:: |
public | function |
Form submission handler. Overrides BillingScheduleBase:: |
1 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |