diff --git a/src/Hook/VotingApiWidgetsEntityHooks.php b/src/Hook/VotingApiWidgetsEntityHooks.php new file mode 100644 index 0000000000000000000000000000000000000000..54b5d0fd637634b287bf1af871e438e8d6c32254 --- /dev/null +++ b/src/Hook/VotingApiWidgetsEntityHooks.php @@ -0,0 +1,63 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\votingapi_widgets\Hook; + +use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\Core\Hook\Attribute\Hook; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\field\Entity\FieldStorageConfig; +use Drupal\votingapi_widgets\Plugin\VotingApiWidgetManager; + +/** + * Hook implementations used to create and dispatch Entity Events. + */ +final class VotingApiWidgetsEntityHooks { + + /** + * Constructs a new VotingApiWidgetsEntityHooks service. + * + * @param \Drupal\votingapi_widgets\Plugin\VotingApiWidgetManager $widgetManager + * The entity_type.manager service. + */ + public function __construct( + protected VotingApiWidgetManager $widgetManager, + ) {} + + /** + * Implements hook_entity_base_field_info(). + */ + #[Hook('entity_base_field_info')] + public function entityBaseFieldInfo(EntityTypeInterface $entity_type): array { + $fields = []; + + // Add the field_name as a base field. + if ($entity_type->id() != 'vote') { + return $fields; + } + + $fields['field_name'] = BaseFieldDefinition::create('string') + ->setLabel(new TranslatableMarkup('Field name')) + ->setName('field_name') + ->setRevisionable(FALSE) + ->setRequired(FALSE) + ->setDescription(new TranslatableMarkup('Holds the field name.')) + ->setPropertyConstraints('value', ['Length' => ['max' => FieldStorageConfig::NAME_MAX_LENGTH]]); + + return $fields; + } + + /** + * Implements hook_entity_type_build(). + */ + #[Hook('entity_type_build')] + public function entityTypeBuild(array &$entity_types): void { + $plugins = $this->widgetManager->getDefinitions(); + foreach ($plugins as $plugin_id => $definition) { + $entity_types['vote']->setFormClass('votingapi_' . $plugin_id, 'Drupal\votingapi_widgets\Form\BaseRatingForm'); + } + } + +} diff --git a/src/Hook/VotingApiWidgetsFormHooks.php b/src/Hook/VotingApiWidgetsFormHooks.php new file mode 100644 index 0000000000000000000000000000000000000000..6d23e8b9947c9ed7d6978aba0af5985d8a6e17f3 --- /dev/null +++ b/src/Hook/VotingApiWidgetsFormHooks.php @@ -0,0 +1,43 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\votingapi_widgets\Hook; + +use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Hook\Attribute\Hook; +use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\StringTranslation\TranslationInterface; + +/** + * Hook implementations used to alter and enhance forms. + */ +final class VotingApiWidgetsFormHooks { + use StringTranslationTrait; + + /** + * Constructs a new VotingApiWidgetsFormHooks service. + * + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation + * The string translation service. + */ + public function __construct( + TranslationInterface $string_translation, + ) { + $this->stringTranslation = $string_translation; + } + + /** + * Implements hook_form_FORM_ID_alter() for field_config_edit_form. + */ + #[Hook('form_field_config_edit_form_alter')] + public function fieldConfigEditFormAlter(array &$form, FormStateInterface $form_state): void { + if ($form_state->getFormObject()->getEntity()->bundle() == 'voting_api_field') { + // We only support posting one comment at the time so it doesn't make + // sense to let the site builder choose anything else. + $form['field_storage']['subform']['cardinality_container']['cardinality']['#default_value'] = 1; + $form['field_storage']['subform']['cardinality_container']['#access'] = FALSE; + } + } + +} diff --git a/src/Hook/VotingApiWidgetsHelpHooks.php b/src/Hook/VotingApiWidgetsHelpHooks.php new file mode 100644 index 0000000000000000000000000000000000000000..7c32f4500e2cd6214a6ad8bd7b6a057be83f9929 --- /dev/null +++ b/src/Hook/VotingApiWidgetsHelpHooks.php @@ -0,0 +1,47 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\votingapi_widgets\Hook; + +use Drupal\Core\Hook\Attribute\Hook; +use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\StringTranslation\TranslationInterface; + +/** + * Hook implementations used to provide help. + */ +final class VotingApiWidgetsHelpHooks { + use StringTranslationTrait; + + /** + * Constructs a new VotingApiWidgetsHelpHooks service. + * + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation + * The string translation service. + */ + public function __construct( + TranslationInterface $string_translation, + ) { + $this->stringTranslation = $string_translation; + } + + /** + * Implements hook_help(). + */ + #[Hook('help')] + public function help(string $route_name, RouteMatchInterface $route_match): ?string { + switch ($route_name) { + // Main module help for the votingapi_widgets module. + case 'help.page.votingapi_widgets': + $output = '<h3>' . $this->t('About') . '</h3>'; + $output .= '<p>' . $this->t('Voting API Widgets') . '</p>'; + return $output; + + default: + } + return NULL; + } + +} diff --git a/votingapi_widgets.module b/votingapi_widgets.module index 55e2efc5bb206c444534b47c373112c9061b4dd1..83be81834aa0fe43fe68c8122f7201e83da645fe 100644 --- a/votingapi_widgets.module +++ b/votingapi_widgets.module @@ -2,73 +2,48 @@ /** * @file - * Contains votingapi_widgets.module.. + * Contains votingapi_widgets.module. */ use Drupal\Core\Entity\EntityTypeInterface; -use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Hook\Attribute\LegacyHook; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\field\Entity\FieldConfig; -use Drupal\field\Entity\FieldStorageConfig; +use Drupal\votingapi_widgets\Hook\VotingApiWidgetsEntityHooks; +use Drupal\votingapi_widgets\Hook\VotingApiWidgetsFormHooks; +use Drupal\votingapi_widgets\Hook\VotingApiWidgetsHelpHooks; /** * Implements hook_help(). */ +#[LegacyHook] function votingapi_widgets_help($route_name, RouteMatchInterface $route_match) { - switch ($route_name) { - // Main module help for the votingapi_widgets module. - case 'help.page.votingapi_widgets': - $output = ''; - $output .= '<h3>' . t('About') . '</h3>'; - $output .= '<p>' . t('Voting API Widgets') . '</p>'; - return $output; - - default: - } + return \Drupal::service(VotingApiWidgetsHelpHooks::class)->help($route_name, $route_match); } /** * Implements hook_entity_base_field_info(). */ +#[LegacyHook] function votingapi_widgets_entity_base_field_info(EntityTypeInterface $entity_type) { - // Add the field_name as a base field. - if ($entity_type->id() != 'vote') { - return; - } - $fields = []; - - $fields['field_name'] = BaseFieldDefinition::create('string') - ->setLabel(t('Field name')) - ->setName('field_name') - ->setRevisionable(FALSE) - ->setRequired(FALSE) - ->setDescription(t('Holds the field name.')) - ->setPropertyConstraints('value', ['Length' => ['max' => FieldStorageConfig::NAME_MAX_LENGTH]]); - - return $fields; + return \Drupal::service(VotingApiWidgetsEntityHooks::class)->entityBaseFieldInfo($entity_type); } /** - * Implements hook_form_FORM_ID_alter() for field_config_edit_form. + * Implements hook_entity_type_build(). */ -function votingapi_widgets_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state) { - if ($form_state->getFormObject()->getEntity()->bundle() == 'voting_api_field') { - // We only support posting one comment at the time so it doesn't make sense - // to let the site builder choose anything else. - $form['field_storage']['subform']['cardinality_container']['cardinality']['#default_value'] = 1; - $form['field_storage']['subform']['cardinality_container']['#access'] = FALSE; - } +#[LegacyHook] +function votingapi_widgets_entity_type_build(array &$entity_types) { + \Drupal::service(VotingApiWidgetsEntityHooks::class)->entityTypeBuild($entity_types); } /** - * Implements hook_entity_type_build(). + * Implements hook_form_FORM_ID_alter() for field_config_edit_form. */ -function votingapi_widgets_entity_type_build(array &$entity_types) { - $plugins = \Drupal::service('plugin.manager.voting_api_widget.processor')->getDefinitions(); - foreach ($plugins as $plugin_id => $definition) { - $entity_types['vote']->setFormClass('votingapi_' . $plugin_id, 'Drupal\votingapi_widgets\Form\BaseRatingForm'); - } +#[LegacyHook] +function votingapi_widgets_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state) { + \Drupal::service(VotingApiWidgetsFormHooks::class)->fieldConfigEditFormAlter($form, $form_state); } /** diff --git a/votingapi_widgets.services.yml b/votingapi_widgets.services.yml index 574153436ba5323dd93e92011c0afddcfa8f5ffc..d38a4981b5406c01254547f799616d17b12e4b41 100644 --- a/votingapi_widgets.services.yml +++ b/votingapi_widgets.services.yml @@ -1,7 +1,21 @@ services: plugin.manager.voting_api_widget.processor: - class: Drupal\votingapi_widgets\Plugin\VotingApiWidgetManager + class: \Drupal\votingapi_widgets\Plugin\VotingApiWidgetManager parent: default_plugin_manager + Drupal\votingapi_widgets\Plugin\VotingApiWidgetManager: '@plugin.manager.voting_api_widget.processor' + voting_api.lazy_loader: - class: Drupal\votingapi_widgets\VotingApiLoader + class: \Drupal\votingapi_widgets\VotingApiLoader + autowire: true arguments: ['@plugin.manager.voting_api_widget.processor', '@entity_type.manager'] + Drupal\votingapi_widgets\VotingApiLoader: '@voting_api.lazy_loader' + + Drupal\votingapi_widgets\Hook\VotingApiWidgetsEntityHooks: + class: \Drupal\votingapi_widgets\Hook\VotingApiWidgetsEntityHooks + autowire: true + Drupal\votingapi_widgets\Hook\VotingApiWidgetsFormHooks: + class: \Drupal\votingapi_widgets\Hook\VotingApiWidgetsFormHooks + autowire: true + Drupal\votingapi_widgets\Hook\VotingApiWidgetsHelpHooks: + class: \Drupal\votingapi_widgets\Hook\VotingApiWidgetsHelpHooks + autowire: true