From ff945212ecaf81e73836e3625851015935ee3e6b Mon Sep 17 00:00:00 2001 From: Tim Rohaly <tr@202830.no-reply.drupal.org> Date: Wed, 19 Feb 2025 21:13:57 -0800 Subject: [PATCH] Issue #3507935 by tr: Add typing to VotingApiWidgets plugins and manager --- src/Plugin/VotingApiWidget/FiveStarWidget.php | 6 ++- src/Plugin/VotingApiWidget/LikeWidget.php | 6 ++- src/Plugin/VotingApiWidget/UsefulWidget.php | 6 ++- src/Plugin/VotingApiWidgetBase.php | 14 ++--- src/Plugin/VotingApiWidgetInterface.php | 52 +++++++++++++++++-- src/Plugin/VotingApiWidgetManager.php | 2 + 6 files changed, 69 insertions(+), 17 deletions(-) diff --git a/src/Plugin/VotingApiWidget/FiveStarWidget.php b/src/Plugin/VotingApiWidget/FiveStarWidget.php index dc524de..53c5d23 100644 --- a/src/Plugin/VotingApiWidget/FiveStarWidget.php +++ b/src/Plugin/VotingApiWidget/FiveStarWidget.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + namespace Drupal\votingapi_widgets\Plugin\VotingApiWidget; use Drupal\Core\StringTranslation\StringTranslationTrait; @@ -27,7 +29,7 @@ class FiveStarWidget extends VotingApiWidgetBase { /** * Vote form. */ - public function buildForm($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, $settings) { + public function buildForm(string $entity_type, string $entity_bundle, string|int $entity_id, string $vote_type, string $field_name, array $settings): array { $form = $this->getForm($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, $settings); $build = [ 'rating' => [ @@ -68,7 +70,7 @@ class FiveStarWidget extends VotingApiWidgetBase { /** * {@inheritdoc} */ - public function getStyles() { + public function getStyles(): array { return [ 'default' => $this->t('Default'), 'bars-horizontal' => $this->t('Bars horizontal'), diff --git a/src/Plugin/VotingApiWidget/LikeWidget.php b/src/Plugin/VotingApiWidget/LikeWidget.php index 37179c4..50598af 100644 --- a/src/Plugin/VotingApiWidget/LikeWidget.php +++ b/src/Plugin/VotingApiWidget/LikeWidget.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + namespace Drupal\votingapi_widgets\Plugin\VotingApiWidget; use Drupal\Core\StringTranslation\StringTranslationTrait; @@ -23,7 +25,7 @@ class LikeWidget extends VotingApiWidgetBase { /** * Vote form. */ - public function buildForm($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, $settings) { + public function buildForm(string $entity_type, string $entity_bundle, string|int $entity_id, string $vote_type, string $field_name, array $settings): array { $form = $this->getForm($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, $settings); $build = [ 'rating' => [ @@ -60,7 +62,7 @@ class LikeWidget extends VotingApiWidgetBase { /** * {@inheritdoc} */ - public function getStyles() { + public function getStyles(): array { return [ 'default' => $this->t('Default'), ]; diff --git a/src/Plugin/VotingApiWidget/UsefulWidget.php b/src/Plugin/VotingApiWidget/UsefulWidget.php index 1713723..bbcac49 100644 --- a/src/Plugin/VotingApiWidget/UsefulWidget.php +++ b/src/Plugin/VotingApiWidget/UsefulWidget.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + namespace Drupal\votingapi_widgets\Plugin\VotingApiWidget; use Drupal\Core\StringTranslation\StringTranslationTrait; @@ -24,7 +26,7 @@ class UsefulWidget extends VotingApiWidgetBase { /** * Vote form. */ - public function buildForm($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, $settings) { + public function buildForm(string $entity_type, string $entity_bundle, string|int $entity_id, string $vote_type, string $field_name, array $settings): array { $form = $this->getForm($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, $settings); $build = [ 'rating' => [ @@ -61,7 +63,7 @@ class UsefulWidget extends VotingApiWidgetBase { /** * {@inheritdoc} */ - public function getStyles() { + public function getStyles(): array { return [ 'default' => $this->t('Default'), ]; diff --git a/src/Plugin/VotingApiWidgetBase.php b/src/Plugin/VotingApiWidgetBase.php index ad8af51..ef80a1f 100644 --- a/src/Plugin/VotingApiWidgetBase.php +++ b/src/Plugin/VotingApiWidgetBase.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + namespace Drupal\votingapi_widgets\Plugin; use Drupal\Component\Plugin\PluginBase; @@ -112,16 +114,16 @@ abstract class VotingApiWidgetBase extends PluginBase implements VotingApiWidget } /** - * Return label. + * {@inheritdoc} */ - public function getLabel() { - return $this->label; + public function getLabel(): string { + return (string) $this->getPluginDefinition()['label']; } /** - * Return minimal value. + * {@inheritdoc} */ - public function getValues() { + public function getValues(): array { return $this->getPluginDefinition()['values']; } @@ -131,7 +133,7 @@ abstract class VotingApiWidgetBase extends PluginBase implements VotingApiWidget * @return array * Configured vote form. */ - public function getForm($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, $settings) { + public function getForm(string $entity_type, string $entity_bundle, string|int $entity_id, string $vote_type, string $field_name, array $settings): array { $vote = $this->getEntityForVoting($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name); /* * @todo Remove custom entity_form_builder once diff --git a/src/Plugin/VotingApiWidgetInterface.php b/src/Plugin/VotingApiWidgetInterface.php index 4666535..74d7933 100644 --- a/src/Plugin/VotingApiWidgetInterface.php +++ b/src/Plugin/VotingApiWidgetInterface.php @@ -5,18 +5,60 @@ namespace Drupal\votingapi_widgets\Plugin; use Drupal\Component\Plugin\PluginInspectionInterface; /** - * Defines an interface for Voting api widget plugins. + * Defines an interface for Voting API widget plugins. */ interface VotingApiWidgetInterface extends PluginInspectionInterface { /** - * Build form. + * Builds the widget form. + * + * @param string $entity_type + * @param string $entity_bundle + * @param string|int $entity_id + * @param string $vote_type + * @param string $field_name + * @param array $settings + * + * @return array + * The form array for the widget. */ - public function buildForm($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, $settings); + public function buildForm(string $entity_type, string $entity_bundle, string|int $entity_id, string $vote_type, string $field_name, array $settings): array; /** - * Get available styles. + * Gets available widget styles. + * + * @return array + * An associative array of widget styles. Keys are the CSS style file name, + * values are the human-readable name for the style. */ - public function getStyles(); + public function getStyles(): array; + + /** + * Gets the translated label from the plugin definition. + * + * @return string + * The label of the plugin. + * + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * Thrown if the label is not defined for the plugin. + */ + public function getLabel(): string; + + /** + * Gets allowed widget values. + * + * @return array + * An array of allowed values. Each value holds the translated short + * description of a style. + */ + public function getValues(): array; + + /** + * Gets the initial element. + * + * @param array &$form + * The form array to modify. + */ + public function getInitialVotingElement(array &$form); } diff --git a/src/Plugin/VotingApiWidgetManager.php b/src/Plugin/VotingApiWidgetManager.php index 6b3d2f9..32547c4 100644 --- a/src/Plugin/VotingApiWidgetManager.php +++ b/src/Plugin/VotingApiWidgetManager.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + namespace Drupal\votingapi_widgets\Plugin; use Drupal\Core\Cache\CacheBackendInterface; -- GitLab