- 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
35 lines
773 B
PHP
35 lines
773 B
PHP
<?php
|
|
|
|
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 {
|
|
$nodes = $this->postNodeRepository->findAll();
|
|
|
|
$build = [];
|
|
$build['content']['#theme'] = 'item_list';
|
|
foreach ($nodes as $node) {
|
|
$build['content']['#items'][] = $node->label();
|
|
}
|
|
|
|
return $build;
|
|
}
|
|
|
|
public static function create(ContainerInterface $container): self {
|
|
return new self(
|
|
$container->get(PostNodeRepository::class),
|
|
);
|
|
}
|
|
|
|
}
|