Return related posts for a specified blog post
Add the initial code for specifying a blog post and returning related posts. This includes adding a repository for related posts, and adding it as a service within the `opdavies_blog` module. References #3
This commit is contained in:
parent
62424d5b04
commit
d7f3d8c482
|
@ -19,3 +19,6 @@ services:
|
||||||
|
|
||||||
Drupal\opdavies_blog\Service\PostPusher\PostPusher:
|
Drupal\opdavies_blog\Service\PostPusher\PostPusher:
|
||||||
alias: Drupal\opdavies_blog\Service\PostPusher\IftttPostPusher
|
alias: Drupal\opdavies_blog\Service\PostPusher\IftttPostPusher
|
||||||
|
|
||||||
|
Drupal\opdavies_blog\Repository\RelatedPostsRepository:
|
||||||
|
autowire: true
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Drupal\opdavies_blog\Repository;
|
||||||
|
|
||||||
|
use Drupal\Core\Entity\EntityStorageInterface;
|
||||||
|
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||||
|
use Drupal\opdavies_blog\Entity\Node\Post;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
|
final class RelatedPostsRepository {
|
||||||
|
|
||||||
|
private EntityStorageInterface $nodeStorage;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
EntityTypeManagerInterface $entityTypeManager
|
||||||
|
) {
|
||||||
|
$this->nodeStorage = $entityTypeManager->getStorage('node');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFor(Post $post): Collection {
|
||||||
|
$posts = $this->nodeStorage->loadByProperties([
|
||||||
|
'type' => 'post',
|
||||||
|
]);
|
||||||
|
|
||||||
|
return (new Collection($posts))
|
||||||
|
->filter(fn(Post $relatedPost) => $relatedPost->id() != $post->id())
|
||||||
|
->values();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// phpcs:disable Drupal.Commenting.DocComment, Drupal.NamingConventions.ValidFunctionName
|
||||||
|
|
||||||
|
namespace Drupal\Tests\opdavies_blog\Kernel;
|
||||||
|
|
||||||
|
use Drupal\opdavies_blog\Repository\RelatedPostsRepository;
|
||||||
|
use Drupal\opdavies_blog_test\Factory\PostFactory;
|
||||||
|
|
||||||
|
final class RelatedPostsTest extends PostTestBase {
|
||||||
|
|
||||||
|
private RelatedPostsRepository $relatedPostsRepository;
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_returns_related_posts(): void {
|
||||||
|
$postA = (new PostFactory())->setTitle('Post A')
|
||||||
|
->withTags(['Drupal 8'])
|
||||||
|
->create();
|
||||||
|
$postA->save();
|
||||||
|
|
||||||
|
$postB = (new PostFactory())->setTitle('Post B')
|
||||||
|
->withTags(['Drupal 8'])
|
||||||
|
->create();
|
||||||
|
$postB->save();
|
||||||
|
|
||||||
|
$relatedPosts = $this->relatedPostsRepository->getFor($postA);
|
||||||
|
|
||||||
|
$this->assertCount(1, $relatedPosts);
|
||||||
|
$this->assertSame('Post B', $relatedPosts->first()->label());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->relatedPostsRepository = $this->container->get(RelatedPostsRepository::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue