diff --git a/src/Plugin/VotingApiWidget/FiveStarWidget.php b/src/Plugin/VotingApiWidget/FiveStarWidget.php index dc524deb5a01cb6fdda27eb96c2cf1e95f435ad4..53c5d23d5446b8a286b73b284a9ab42f567c5e90 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 37179c4406cb10d12dca406ed95e2c3af7710058..50598afe5b0f0d6a813d000f6c38077090a5bd2a 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 1713723ee5eca23124fddd2da57e8394e8458170..bbcac49f2308989c1fbf5b5f7c21ed2b021e81be 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 ad8af51466b4616e70069e3d07c3b2536359a9c1..ef80a1f0e6efdef6e62c018e8d211e7071e436d1 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 4666535585389ce8ea69fe869b12486e95c12c8d..74d79333a0106036c5a1f40ec5a53e6bdcf6a331 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 6b3d2f9982b308f3d7db2e6cc2ccc98412b3ac9c..32547c44edc9c45cf6f5aa40f13871dd0bef72d5 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;