Skip to content
Snippets Groups Projects
Commit 5a8749ad authored by Michael Strelan's avatar Michael Strelan Committed by dpi
Browse files

Issue #3425292 by mstrelan, dpi: Use attributes for DateRecurInterpreter plugin discovery

parent 4200723f
Branches 3.2.x
No related tags found
No related merge requests found
<?php
declare(strict_types=1);
namespace Drupal\date_recur\Attribute;
use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Attribute for defining an interpreter for Recurring date field.
*
* @see plugin_api
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
final class DateRecurInterpreter extends Plugin {
private const USE_NAMED_PARAMETERS = 'Using this attribute without named parameters is not supported.';
/**
* @phpstan-param class-string<\Drupal\Component\Plugin\Derivative\DeriverInterface>|null $deriver
*/
public function __construct(
string $id,
public readonly TranslatableMarkup $label,
?string $useNamedParameters = self::USE_NAMED_PARAMETERS,
?string $deriver = NULL,
) {
parent::__construct($id, $deriver);
if (self::USE_NAMED_PARAMETERS !== $useNamedParameters) {
throw new \LogicException(self::USE_NAMED_PARAMETERS);
}
}
}
......@@ -13,6 +13,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\date_recur\Attribute\DateRecurInterpreter;
use Drupal\date_recur\Plugin\DateRecurInterpreterPluginBase;
use RRule\RRule;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -20,14 +21,13 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides an interpreter implemented by rlanvin/php-rrule.
*
* @DateRecurInterpreter(
* id = "rl",
* label = @Translation("RL interpreter"),
* )
*
* @ingroup RLanvinPhpRrule
* @phpstan-property array{show_start_date: bool, show_until: bool, date_format: string, show_infinite: bool} $configuration
*/
#[DateRecurInterpreter(
id: 'rl',
label: new TranslatableMarkup('RL interpreter'),
)]
class RlInterpreter extends DateRecurInterpreterPluginBase implements ContainerFactoryPluginInterface, PluginFormInterface {
use DependencyTrait;
......
......@@ -7,6 +7,7 @@ namespace Drupal\date_recur\Plugin;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\date_recur\Attribute\DateRecurInterpreter;
//@codingStandardsIgnoreStart
/**
......@@ -26,6 +27,7 @@ class DateRecurInterpreterManager extends DefaultPluginManager implements DateRe
$namespaces,
$module_handler,
'Drupal\date_recur\Plugin\DateRecurInterpreterPluginInterface',
DateRecurInterpreter::class,
'Drupal\date_recur\Annotation\DateRecurInterpreter',
);
$this->setCacheBackend($cache_backend, 'date_recur_interpreter_info', ['config:date_recur_interpreter_list']);
......
......@@ -173,13 +173,10 @@ final class DateRecurBasicWidgetTest extends BrowserTestBase {
/**
* Data provider for testFields.
*
* @return array
* Data for testing.
* @phpstan-return \Generator<array{0: array<mixed>, 1: bool, 2?: string|null}>
*/
public function providerFields(): array {
$scenarios = [];
$scenarios['Test no failures if nothing is filled.'] = [
public static function providerFields(): \Generator {
yield 'Test no failures if nothing is filled.' => [
[
'dr[0][value][date]' => '',
'dr[0][value][time]' => '',
......@@ -191,7 +188,7 @@ final class DateRecurBasicWidgetTest extends BrowserTestBase {
TRUE,
];
$scenarios['Test failure when only start date field filled.'] = [
yield 'Test failure when only start date field filled.' => [
[
'dr[0][value][date]' => '2008-06-17',
'dr[0][value][time]' => '',
......@@ -204,7 +201,7 @@ final class DateRecurBasicWidgetTest extends BrowserTestBase {
'Missing time zone for date.',
];
$scenarios['Test failure when only start time field filled.'] = [
yield 'Test failure when only start time field filled.' => [
[
'dr[0][value][date]' => '',
'dr[0][value][time]' => '10:00:00',
......@@ -217,7 +214,7 @@ final class DateRecurBasicWidgetTest extends BrowserTestBase {
'Missing time zone for date.',
];
$scenarios['Test failure when start date and time field filled.'] = [
yield 'Test failure when start date and time field filled.' => [
[
'dr[0][value][date]' => '2008-06-17',
'dr[0][value][time]' => '10:00:00',
......@@ -230,7 +227,7 @@ final class DateRecurBasicWidgetTest extends BrowserTestBase {
'Missing time zone for date.',
];
$scenarios['Test failure when end date filled.'] = [
yield 'Test failure when end date filled.' => [
[
'dr[0][value][date]' => '',
'dr[0][value][time]' => '',
......@@ -243,7 +240,7 @@ final class DateRecurBasicWidgetTest extends BrowserTestBase {
'Missing time zone for date.',
];
$scenarios['Test failure when end time filled.'] = [
yield 'Test failure when end time filled.' => [
[
'dr[0][value][date]' => '',
'dr[0][value][time]' => '',
......@@ -256,7 +253,7 @@ final class DateRecurBasicWidgetTest extends BrowserTestBase {
'Missing time zone for date.',
];
$scenarios['Test success when start date and time and time zone field filled.'] = [
yield 'Test success when start date and time and time zone field filled.' => [
[
'dr[0][value][date]' => '2008-06-17',
'dr[0][value][time]' => '10:00:00',
......@@ -268,7 +265,7 @@ final class DateRecurBasicWidgetTest extends BrowserTestBase {
TRUE,
];
$scenarios['Test failure when end date and time and time zone field filled.'] = [
yield 'Test failure when end date and time and time zone field filled.' => [
[
'dr[0][value][date]' => '',
'dr[0][value][time]' => '',
......@@ -281,14 +278,14 @@ final class DateRecurBasicWidgetTest extends BrowserTestBase {
'Start date must be set if end date is set.',
];
$scenarios['Tests failure on invalid rule.'] = [
yield 'Tests failure on invalid rule.' => [
[
'dr[0][value][date]' => '2008-06-17',
'dr[0][value][time]' => '12:00:00',
'dr[0][end_value][date]' => '2008-06-17',
'dr[0][end_value][time]' => '12:00:00',
'dr[0][timezone]' => 'America/Chicago',
'dr[0][rrule]' => $this->randomMachineName(),
'dr[0][rrule]' => '123FAKE_RRULE321',
],
FALSE,
'Repeat rule is formatted incorrectly.',
......@@ -296,7 +293,7 @@ final class DateRecurBasicWidgetTest extends BrowserTestBase {
// Tests validation that comes automatically from date range. Specifically,
// assert end date comes on or after start date.
$scenarios['Tests inherited validation: end before start'] = [
yield 'Tests inherited validation: end before start' => [
[
'dr[0][value][date]' => '2008-06-17',
'dr[0][value][time]' => '03:00:00',
......@@ -308,8 +305,6 @@ final class DateRecurBasicWidgetTest extends BrowserTestBase {
FALSE,
'end date cannot be before the start date',
];
return $scenarios;
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment