You are here

class RlInterpreter in Recurring Dates Field 3.1.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/DateRecurInterpreter/RlInterpreter.php \Drupal\date_recur\Plugin\DateRecurInterpreter\RlInterpreter
  2. 3.x src/Plugin/DateRecurInterpreter/RlInterpreter.php \Drupal\date_recur\Plugin\DateRecurInterpreter\RlInterpreter
  3. 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

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\DateRecurInterpreter
View 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

Namesort descending Modifiers Type Description Overrides
DateRecurInterpreterPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
DateRecurInterpreterPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
DependencyTrait::$dependencies protected property The object's dependencies.
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginWithFormsTrait::getFormClass public function Implements \Drupal\Core\Plugin\PluginWithFormsInterface::getFormClass().
PluginWithFormsTrait::hasFormClass public function Implements \Drupal\Core\Plugin\PluginWithFormsInterface::hasFormClass().
RlInterpreter::$dateFormatStorage protected property The date format entity storage.
RlInterpreter::$dateFormatter protected property The date formatter service.
RlInterpreter::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
RlInterpreter::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DateRecurInterpreterPluginBase::calculateDependencies
RlInterpreter::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
RlInterpreter::defaultConfiguration public function Gets default configuration for this plugin. Overrides DateRecurInterpreterPluginBase::defaultConfiguration
RlInterpreter::interpret public function Interpret a set of rules in a language. Overrides DateRecurInterpreterPluginInterface::interpret
RlInterpreter::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
RlInterpreter::supportedLanguages public function The languages supported by this plugin. Overrides DateRecurInterpreterPluginInterface::supportedLanguages
RlInterpreter::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
RlInterpreter::__construct public function Constructs a new RlInterpreter. Overrides PluginBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.