Add lesson 4
- Add initial `PostNodeRepository` - Move logic to Repository - Inject `EntityTypeManagerInterface` - Add `PostNodeRepositoryTest` - Add failing test method - Failing test: non-existent service - The "node" entity type does not exist. - Create posts - Add assertions based on the `created` date - Sort nodes before returning them - Return values to reset array keys
This commit is contained in:
parent
5121502d7f
commit
75b17fd565
4 changed files with 101 additions and 2 deletions
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\example\Repository;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\node\NodeInterface;
|
||||
|
||||
final class PostNodeRepository {
|
||||
|
||||
public function __construct(
|
||||
private EntityTypeManagerInterface $entityTypeManager,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, NodeInterface>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue