Initial commit
This commit is contained in:
commit
e1517ecd9d
36 changed files with 2093 additions and 0 deletions
step5-wrapping-up-with-unit-tests/src
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\my_module\Controller;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
use Drupal\my_module\Repository\ArticleRepository;
|
||||
|
||||
class BlogPageController {
|
||||
|
||||
use StringTranslationTrait;
|
||||
|
||||
/**
|
||||
* @var \Drupal\my_module\Repository\ArticleRepository
|
||||
*/
|
||||
private $articleRepository;
|
||||
|
||||
/**
|
||||
* @var \Drupal\Core\Entity\EntityViewBuilderInterface
|
||||
*/
|
||||
private $nodeViewBuilder;
|
||||
|
||||
public function __construct(
|
||||
EntityTypeManagerInterface $entityTypeManager,
|
||||
ArticleRepository $articleRepository
|
||||
) {
|
||||
$this->nodeViewBuilder = $entityTypeManager->getViewBuilder('node');
|
||||
$this->articleRepository = $articleRepository;
|
||||
}
|
||||
|
||||
public function __invoke(): array {
|
||||
$build = [];
|
||||
|
||||
$articles = $this->articleRepository->getAll();
|
||||
|
||||
foreach ($articles as $article) {
|
||||
$build[] = $this->nodeViewBuilder->view($article, 'teaser');
|
||||
}
|
||||
|
||||
return [
|
||||
'#markup' => render($build),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\my_module\Repository;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\node\NodeInterface;
|
||||
|
||||
class ArticleRepository {
|
||||
|
||||
/**
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
private $nodeStorage;
|
||||
|
||||
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
|
||||
$this->nodeStorage = $entityTypeManager->getStorage('node');
|
||||
}
|
||||
|
||||
public function getAll(): array {
|
||||
$articles = $this->nodeStorage->loadByProperties([
|
||||
'status' => Node::PUBLISHED,
|
||||
'type' => 'article',
|
||||
]);
|
||||
|
||||
uasort($articles, function (NodeInterface $a, NodeInterface $b): bool {
|
||||
return $a->getCreatedTime() < $b->getCreatedTime();
|
||||
});
|
||||
|
||||
return $articles;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\my_module\Wrapper;
|
||||
|
||||
use Drupal\Component\Datetime\TimeInterface;
|
||||
use Drupal\node\NodeInterface;
|
||||
|
||||
class ArticleWrapper {
|
||||
|
||||
private $article;
|
||||
|
||||
public function __construct(TimeInterface $time, NodeInterface $node) {
|
||||
$this->verifyNodeType($node);
|
||||
|
||||
$this->time = $time;
|
||||
$this->article = $node;
|
||||
}
|
||||
|
||||
public function getOriginal(): NodeInterface {
|
||||
return $this->article;
|
||||
}
|
||||
|
||||
private function verifyNodeType(NodeInterface $node): void {
|
||||
if ($node->bundle() != 'article') {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'%s is not an article',
|
||||
$node->bundle()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public function isPublishable(): bool {
|
||||
$created = $this->article->getCreatedTime();
|
||||
|
||||
$difference = $this->time->getRequestTime() - $created;
|
||||
|
||||
return $difference >= 60 * 60 * 24 * 3;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue