diff --git a/web/modules/custom/example/example.services.yml b/web/modules/custom/example/example.services.yml new file mode 100644 index 0000000..e5c0402 --- /dev/null +++ b/web/modules/custom/example/example.services.yml @@ -0,0 +1,4 @@ +services: + Drupal\example\Repository\PostNodeRepository: + arguments: + - '@entity_type.manager' diff --git a/web/modules/custom/example/src/Controller/BlogPageController.php b/web/modules/custom/example/src/Controller/BlogPageController.php index cbcd1f6..cdc5d3a 100644 --- a/web/modules/custom/example/src/Controller/BlogPageController.php +++ b/web/modules/custom/example/src/Controller/BlogPageController.php @@ -3,12 +3,18 @@ namespace Drupal\example\Controller; use Drupal\Core\Controller\ControllerBase; +use Drupal\example\Repository\PostNodeRepository; +use Symfony\Component\DependencyInjection\ContainerInterface; class BlogPageController extends ControllerBase { + public function __construct( + private PostNodeRepository $postNodeRepository, + ) { + } + public function __invoke(): array { - $nodeStorage = $this->entityTypeManager()->getStorage('node'); - $nodes = $nodeStorage->loadMultiple(); + $nodes = $this->postNodeRepository->findAll(); $build = []; $build['content']['#theme'] = 'item_list'; @@ -19,4 +25,10 @@ class BlogPageController extends ControllerBase { return $build; } + public static function create(ContainerInterface $container): self { + return new self( + $container->get(PostNodeRepository::class), + ); + } + } diff --git a/web/modules/custom/example/src/Repository/PostNodeRepository.php b/web/modules/custom/example/src/Repository/PostNodeRepository.php new file mode 100644 index 0000000..b7221fc --- /dev/null +++ b/web/modules/custom/example/src/Repository/PostNodeRepository.php @@ -0,0 +1,29 @@ + + */ + public function findAll(): array { + $nodeStorage = $this->entityTypeManager->getStorage('node'); + $nodes = $nodeStorage->loadMultiple(); + + uasort($nodes, function (NodeInterface $a, NodeInterface $b): int { + return $a->getCreatedTime() <=> $b->getCreatedTime(); + }); + + return array_values($nodes); + } + +} diff --git a/web/modules/custom/example/tests/src/Kernel/PostNodeRepositoryTest.php b/web/modules/custom/example/tests/src/Kernel/PostNodeRepositoryTest.php new file mode 100644 index 0000000..9683002 --- /dev/null +++ b/web/modules/custom/example/tests/src/Kernel/PostNodeRepositoryTest.php @@ -0,0 +1,54 @@ +createNode([ + 'title' => 'Post one', + 'created' => (new DrupalDateTime('-1 week'))->getTimestamp(), + 'type' => 'post', + ]); + + $this->createNode([ + 'title' => 'Post two', + 'created' => (new DrupalDateTime('-8 days'))->getTimestamp(), + 'type' => 'post', + ]); + + $this->createNode([ + 'title' => 'Post three', + 'created' => (new DrupalDateTime('yesterday'))->getTimestamp(), + 'type' => 'post', + ]); + + // Act. + $postRepository = $this->container->get(PostNodeRepository::class); + assert($postRepository instanceof PostNodeRepository); + $nodes = $postRepository->findAll(); + + // Assert. + self::assertCount(3, $nodes); + + self::assertSame( + ['Post two', 'Post one', 'Post three'], + array_map( + fn (NodeInterface $node) => $node->label(), + $nodes + ) + ); + } + +}