Skip to content
Snippets Groups Projects
Commit 667576dc authored by Sourav Paul's avatar Sourav Paul Committed by Tim Rohaly
Browse files

Issue #3467759 by TR, Sourav_Paul: [10.3] Convert VotingApiWidget plugins and...

Issue #3467759 by TR, Sourav_Paul: [10.3] Convert VotingApiWidget plugins and discovery to attributes
parent 210a8430
No related branches found
No related tags found
1 merge request!11Issue #3467759: [10.3] Convert VotingApiWidget plugins and discovery to attributes
Pipeline #265605 failed
<?php
declare(strict_types=1);
namespace Drupal\votingapi_widgets\Attribute;
use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Defines a Voting API widget attribute object.
*
* Plugin Namespace: Plugin\votingapi_widgets.
*
* For a working example, see
* \Drupal\votingapi_widgets\Plugin\votingapi_widgets\UsefulWidget
*
* @see hook_vote_result_info_alter()
* @see \Drupal\votingapi_widgets\Plugin\VotingApiWidgetInterface
* @see \Drupal\votingapi_widgets\Plugin\VotingApiWidgetBase
* @see \Drupal\votingapi_widgets\Plugin\VotingApiWidgetManager
* @see plugin_api
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class VotingApiWidget extends Plugin {
/**
* Constructs a VotingApiWidget attribute object.
*
* @param string $id
* The plugin ID. The machine-name of the function plugin.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $label
* The human-readable name of the widget.
* @param array $values
* An array of possible vote values.
* @param class-string|null $deriver
* (optional) The deriver class.
*/
public function __construct(
public readonly string $id,
public readonly TranslatableMarkup $label,
public readonly array $values,
public readonly ?string $deriver = NULL,
) {}
}
......@@ -5,6 +5,7 @@ namespace Drupal\votingapi_widgets\Plugin;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\votingapi_widgets\Attribute\VotingApiWidget;
/**
* Provides the Voting api widget plugin manager.
......@@ -23,7 +24,7 @@ class VotingApiWidgetManager extends DefaultPluginManager {
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/votingapi_widget', $namespaces, $module_handler, 'Drupal\votingapi_widgets\Plugin\VotingApiWidgetInterface', 'Drupal\votingapi_widgets\Annotation\VotingApiWidget');
parent::__construct('Plugin/VotingApiWidget', $namespaces, $module_handler, VotingApiWidgetInterface::class, VotingApiWidget::class, 'Drupal\votingapi_widgets\Annotation\VotingApiWidget');
$this->alterInfo('votingapi_widgets_voting_api_widget_info');
$this->setCacheBackend($cache_backend, 'votingapi_widgets_voting_api_widget_plugins');
}
......
......@@ -3,6 +3,8 @@
namespace Drupal\votingapi_widgets\Plugin\votingapi_widget;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\votingapi_widgets\Attribute\VotingApiWidget;
use Drupal\votingapi_widgets\Plugin\VotingApiWidgetBase;
/**
......@@ -20,8 +22,18 @@ use Drupal\votingapi_widgets\Plugin\VotingApiWidgetBase;
* },
* )
*/
#[VotingApiWidget(
id: "fivestar",
label: new TranslatableMarkup("Fivestar rating"),
values: [
1 => new TranslatableMarkup("Poor"),
2 => new TranslatableMarkup("Not so poor"),
3 => new TranslatableMarkup("Average"),
4 => new TranslatableMarkup("Good"),
5 => new TranslatableMarkup("Very good"),
]
)]
class FiveStarWidget extends VotingApiWidgetBase {
use StringTranslationTrait;
/**
......
......@@ -3,6 +3,8 @@
namespace Drupal\votingapi_widgets\Plugin\votingapi_widget;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\votingapi_widgets\Attribute\VotingApiWidget;
use Drupal\votingapi_widgets\Plugin\VotingApiWidgetBase;
/**
......@@ -16,8 +18,14 @@ use Drupal\votingapi_widgets\Plugin\VotingApiWidgetBase;
* },
* )
*/
#[VotingApiWidget(
id: "like",
label: new TranslatableMarkup("Like"),
values: [
1 => new TranslatableMarkup("Like"),
]
)]
class LikeWidget extends VotingApiWidgetBase {
use StringTranslationTrait;
/**
......
......@@ -3,6 +3,8 @@
namespace Drupal\votingapi_widgets\Plugin\votingapi_widget;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\votingapi_widgets\Attribute\VotingApiWidget;
use Drupal\votingapi_widgets\Plugin\VotingApiWidgetBase;
/**
......@@ -17,8 +19,15 @@ use Drupal\votingapi_widgets\Plugin\VotingApiWidgetBase;
* },
* )
*/
#[VotingApiWidget(
id: "useful",
label: new TranslatableMarkup("Useful rating"),
values: [
-1 => new TranslatableMarkup("Not useful"),
1 => new TranslatableMarkup("Useful"),
]
)]
class UsefulWidget extends VotingApiWidgetBase {
use StringTranslationTrait;
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment