Pre-populate the next podcast episode number
This commit is contained in:
parent
7a4c0223f9
commit
6b54631f46
5 changed files with 72 additions and 1 deletions
|
@ -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().
|
||||
*
|
||||
|
|
5
modules/opd_podcast/opd_podcast.services.yml
Normal file
5
modules/opd_podcast/opd_podcast.services.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
services:
|
||||
Drupal\opd_podcast\Action\GetNextPodcastEpisodeNumber:
|
||||
autowire: true
|
||||
Drupal\opd_podcast\Repository\PodcastNodeRepository:
|
||||
autowire: true
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_podcast\Action;
|
||||
|
||||
use Drupal\opd_podcast\Repository\PodcastNodeRepository;
|
||||
|
||||
readonly final class GetNextPodcastEpisodeNumber {
|
||||
|
||||
public function __construct(private PodcastNodeRepository $repository) {
|
||||
}
|
||||
|
||||
public function __invoke(): int {
|
||||
$episodes = $this->repository->getPublished();
|
||||
|
||||
return count($episodes) + 1;
|
||||
}
|
||||
|
||||
}
|
33
modules/opd_podcast/src/Repository/PodcastNodeRepository.php
Normal file
33
modules/opd_podcast/src/Repository/PodcastNodeRepository.php
Normal file
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_podcast\Repository;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\node\NodeStorageInterface;
|
||||
|
||||
readonly final class PodcastNodeRepository {
|
||||
|
||||
private NodeStorageInterface $nodeStorage;
|
||||
|
||||
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
|
||||
$this->nodeStorage = $entityTypeManager->getStorage('node');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<NodeInterface>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
1
todo.txt
1
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:
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue