class RlInterpreter in Recurring Dates Field 3.1.x
Same name and namespace in other branches
- 8.2 src/Plugin/DateRecurInterpreter/RlInterpreter.php \Drupal\date_recur\Plugin\DateRecurInterpreter\RlInterpreter
- 3.x src/Plugin/DateRecurInterpreter/RlInterpreter.php \Drupal\date_recur\Plugin\DateRecurInterpreter\RlInterpreter
- 3.0.x src/Plugin/DateRecurInterpreter/RlInterpreter.php \Drupal\date_recur\Plugin\DateRecurInterpreter\RlInterpreter
Provides an interpreter implemented by rlanvin/php-rrule.
Plugin annotation
@DateRecurInterpreter(
id = "rl",
label = @Translation("RL interpreter"),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\date_recur\Plugin\DateRecurInterpreterPluginBase implements DateRecurInterpreterPluginInterface uses PluginWithFormsTrait
- class \Drupal\date_recur\Plugin\DateRecurInterpreter\RlInterpreter implements ContainerFactoryPluginInterface, PluginFormInterface uses DependencyTrait
- class \Drupal\date_recur\Plugin\DateRecurInterpreterPluginBase implements DateRecurInterpreterPluginInterface uses PluginWithFormsTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of RlInterpreter
1 file declares its use of RlInterpreter
- DateRecurRlInterpretationUnitTest.php in tests/
src/ Unit/ DateRecurRlInterpretationUnitTest.php
File
- src/
Plugin/ DateRecurInterpreter/ RlInterpreter.php, line 30
Namespace
Drupal\date_recur\Plugin\DateRecurInterpreterView source
class RlInterpreter extends DateRecurInterpreterPluginBase implements ContainerFactoryPluginInterface, PluginFormInterface {
use DependencyTrait;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* The date format entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $dateFormatStorage;
/**
* Constructs a new RlInterpreter.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
* The date formatter service.
* @param \Drupal\Core\Entity\EntityStorageInterface $dateFormatStorage
* The date format storage.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $dateFormatter, EntityStorageInterface $dateFormatStorage) {
parent::__construct([], $plugin_id, $plugin_definition);
$this
->setConfiguration($configuration);
$this->dateFormatter = $dateFormatter;
$this->dateFormatStorage = $dateFormatStorage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('date.formatter'), $container
->get('entity_type.manager')
->getStorage('date_format'));
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() : array {
return [
'show_start_date' => TRUE,
'show_until' => TRUE,
'date_format' => '',
'show_infinite' => TRUE,
];
}
/**
* {@inheritdoc}
*/
public function interpret(array $rules, string $language, ?\DateTimeZone $timeZone = NULL) : string {
$pluginConfig = $this
->getConfiguration();
if (!in_array($language, $this
->supportedLanguages())) {
throw new \Exception('Language not supported.');
}
$options = [
'locale' => $language,
'include_start' => $pluginConfig['show_start_date'],
'include_until' => $pluginConfig['show_until'],
'explicit_infinite' => $pluginConfig['show_infinite'],
];
$dateFormatId = $this->configuration['date_format'];
if (!empty($dateFormatId)) {
$dateFormat = $this->dateFormatStorage
->load($dateFormatId);
if ($dateFormat) {
$dateFormatter = function (\DateTimeInterface $date) use ($dateFormat, $timeZone) : string {
$timeZoneString = $timeZone ? $timeZone
->getName() : NULL;
return $this->dateFormatter
->format($date
->getTimestamp(), (string) $dateFormat
->id(), '', $timeZoneString);
};
$options['date_formatter'] = $dateFormatter;
}
}
$strings = [];
foreach ($rules as $rule) {
$rrule = new RRule($rule
->getParts());
$strings[] = $rrule
->humanReadable($options);
}
return implode(', ', $strings);
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) : array {
$form['show_start_date'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show the start date'),
'#default_value' => $this->configuration['show_start_date'],
];
$form['show_until'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show the until date'),
'#default_value' => $this->configuration['show_until'],
];
$form['show_infinite'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show infinite if infinite.'),
'#default_value' => $this->configuration['show_infinite'],
];
$exampleDate = new DrupalDateTime();
$dateFormatOptions = array_map(function (DateFormatInterface $dateFormat) use ($exampleDate) : TranslatableMarkup {
return $this
->t('@name (@date)', [
'@name' => $dateFormat
->label(),
'@date' => $this->dateFormatter
->format($exampleDate
->getTimestamp(), (string) $dateFormat
->id()),
]);
}, $this->dateFormatStorage
->loadMultiple());
$form['date_format'] = [
'#type' => 'select',
'#title' => $this
->t('Date format'),
'#description' => $this
->t('Date format used for start and until dates.'),
'#default_value' => $this->configuration['date_format'],
'#options' => $dateFormatOptions,
'#empty_option' => $this
->t('- None -'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) : void {
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) : void {
$this->configuration['show_start_date'] = $form_state
->getValue('show_start_date');
$this->configuration['show_until'] = $form_state
->getValue('show_until');
$this->configuration['date_format'] = $form_state
->getValue('date_format');
$this->configuration['show_infinite'] = $form_state
->getValue('show_infinite');
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() : array {
/** @var string $dateFormatId */
$dateFormatId = $this->configuration['date_format'];
$dateFormat = $this->dateFormatStorage
->load($dateFormatId);
if ($dateFormat) {
$this
->addDependency('config', $dateFormat
->getConfigDependencyName());
}
return $this->dependencies;
}
/**
* {@inheritdoc}
*/
public function supportedLanguages() : array {
return [
'de',
'en',
'es',
'fi',
'fr',
'it',
'nl',
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DateRecurInterpreterPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
DateRecurInterpreterPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
DependencyTrait:: |
protected | property | The object's dependencies. | |
DependencyTrait:: |
protected | function | Adds multiple dependencies. | |
DependencyTrait:: |
protected | function | Adds a dependency. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
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:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginWithFormsTrait:: |
public | function | Implements \Drupal\Core\Plugin\PluginWithFormsInterface::getFormClass(). | |
PluginWithFormsTrait:: |
public | function | Implements \Drupal\Core\Plugin\PluginWithFormsInterface::hasFormClass(). | |
RlInterpreter:: |
protected | property | The date format entity storage. | |
RlInterpreter:: |
protected | property | The date formatter service. | |
RlInterpreter:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
RlInterpreter:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DateRecurInterpreterPluginBase:: |
|
RlInterpreter:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
RlInterpreter:: |
public | function |
Gets default configuration for this plugin. Overrides DateRecurInterpreterPluginBase:: |
|
RlInterpreter:: |
public | function |
Interpret a set of rules in a language. Overrides DateRecurInterpreterPluginInterface:: |
|
RlInterpreter:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
RlInterpreter:: |
public | function |
The languages supported by this plugin. Overrides DateRecurInterpreterPluginInterface:: |
|
RlInterpreter:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
RlInterpreter:: |
public | function |
Constructs a new RlInterpreter. Overrides PluginBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
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. |