From 6b54631f46ed64e9e7af8325a5a6ea1c292a44c6 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sun, 13 Jul 2025 01:12:40 +0100 Subject: [PATCH] Pre-populate the next podcast episode number --- modules/opd_podcast/opd_podcast.module | 14 ++++++++ modules/opd_podcast/opd_podcast.services.yml | 5 +++ .../Action/GetNextPodcastEpisodeNumber.php | 20 +++++++++++ .../src/Repository/PodcastNodeRepository.php | 33 +++++++++++++++++++ todo.txt | 1 - 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 modules/opd_podcast/opd_podcast.services.yml create mode 100644 modules/opd_podcast/src/Action/GetNextPodcastEpisodeNumber.php create mode 100644 modules/opd_podcast/src/Repository/PodcastNodeRepository.php diff --git a/modules/opd_podcast/opd_podcast.module b/modules/opd_podcast/opd_podcast.module index 494bd7b5f..68daff3c5 100644 --- a/modules/opd_podcast/opd_podcast.module +++ b/modules/opd_podcast/opd_podcast.module @@ -2,12 +2,26 @@ declare(strict_types=1); +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\node\NodeInterface; +use Drupal\opd_podcast\Action\GetNextPodcastEpisodeNumber; use Drupal\opd_podcast\Episode; use Drupal\opd_podcast\Guest; +/** + * Implements hook_form_FORM_ID_alter(). + * + * @param array{} $form + */ +function opd_podcast_form_node_podcast_episode_form_alter(array &$form, FormStateInterface $formState): void { + $nextEpisodeNumber = \Drupal::service(GetNextPodcastEpisodeNumber::class); + assert($nextEpisodeNumber instanceof GetNextPodcastEpisodeNumber); + + $form['field_episode_number']['widget'][0]['value']['#default_value'] = $nextEpisodeNumber(); +} + /** * Implements hook_entity_bundle_info_alter(). * diff --git a/modules/opd_podcast/opd_podcast.services.yml b/modules/opd_podcast/opd_podcast.services.yml new file mode 100644 index 000000000..ff56c3f97 --- /dev/null +++ b/modules/opd_podcast/opd_podcast.services.yml @@ -0,0 +1,5 @@ +services: + Drupal\opd_podcast\Action\GetNextPodcastEpisodeNumber: + autowire: true + Drupal\opd_podcast\Repository\PodcastNodeRepository: + autowire: true diff --git a/modules/opd_podcast/src/Action/GetNextPodcastEpisodeNumber.php b/modules/opd_podcast/src/Action/GetNextPodcastEpisodeNumber.php new file mode 100644 index 000000000..bc65e89b6 --- /dev/null +++ b/modules/opd_podcast/src/Action/GetNextPodcastEpisodeNumber.php @@ -0,0 +1,20 @@ +repository->getPublished(); + + return count($episodes) + 1; + } + +} diff --git a/modules/opd_podcast/src/Repository/PodcastNodeRepository.php b/modules/opd_podcast/src/Repository/PodcastNodeRepository.php new file mode 100644 index 000000000..24ae7ba9f --- /dev/null +++ b/modules/opd_podcast/src/Repository/PodcastNodeRepository.php @@ -0,0 +1,33 @@ +nodeStorage = $entityTypeManager->getStorage('node'); + } + + /** + * @return list + */ + public function getPublished(): array { + $query = $this->nodeStorage->getQuery(); + $query->accessCheck(); + $query->condition('status', NodeInterface::PUBLISHED); + $query->condition('type', 'podcast_episode'); + + $nodeIds = $query->execute(); + + return $this->nodeStorage->loadMultiple($nodeIds); + } + +} diff --git a/todo.txt b/todo.txt index 1c50e2880..d4ef27599 100644 --- a/todo.txt +++ b/todo.txt @@ -12,7 +12,6 @@ Podcast: - Link field for episode links. - Quotes and talking points in episodes. - Add copyright statement to podcast episodes. -- Pre-populate the episode number to a default value based on the number of nodes. Testimonials: