From 105405e7f916e4441704306d41b543fbdaaf6346 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Mon, 11 Jan 2021 00:58:54 +0000 Subject: [PATCH] Display a list of related post titles References #3 --- .../src/Plugin/Block/RelatedPostsBlock.php | 66 ++++++++++++++++++- .../src/Repository/RelatedPostsRepository.php | 2 +- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/web/modules/custom/blog/src/Plugin/Block/RelatedPostsBlock.php b/web/modules/custom/blog/src/Plugin/Block/RelatedPostsBlock.php index 94554e9..b4ca1eb 100644 --- a/web/modules/custom/blog/src/Plugin/Block/RelatedPostsBlock.php +++ b/web/modules/custom/blog/src/Plugin/Block/RelatedPostsBlock.php @@ -3,6 +3,13 @@ namespace Drupal\opdavies_blog\Plugin\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Link; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Routing\CurrentRouteMatch; +use Drupal\opdavies_blog\Entity\Node\Post; +use Drupal\opdavies_blog\Repository\RelatedPostsRepository; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Tightenco\Collect\Support\Collection; /** * @Block( @@ -11,14 +18,69 @@ use Drupal\Core\Block\BlockBase; * category = @Translation("Blog") * ) */ -class RelatedPostsBlock extends BlockBase { +class RelatedPostsBlock extends BlockBase implements ContainerFactoryPluginInterface { + + private CurrentRouteMatch $currentRouteMatch; + + private RelatedPostsRepository $relatedPostsRepository; + + public function __construct( + array $configuration, + string $pluginId, + array $pluginDefinition, + CurrentRouteMatch $currentRouteMatch, + RelatedPostsRepository $relatedPostsRepository + ) { + parent::__construct($configuration, $pluginId, $pluginDefinition); + + $this->currentRouteMatch = $currentRouteMatch; + $this->relatedPostsRepository = $relatedPostsRepository; + } + + public static function create( + ContainerInterface $container, + array $configuration, + $pluginId, + $pluginDefinition + ): self { + // @phpstan-ignore-next-line + return new self( + $configuration, + $pluginId, + $pluginDefinition, + $container->get('current_route_match'), + $container->get(RelatedPostsRepository::class) + ); + } public function build(): array { + $currentPost = $this->currentRouteMatch->getParameter('node'); + + /** @var Collection|Post[] $relatedPosts */ + $relatedPosts = $this->relatedPostsRepository->getFor($currentPost); + + if ($relatedPosts->isEmpty()) { + return []; + } + $build['content'] = [ - '#markup' => $this->t('It works!'), + '#theme' => 'item_list', + '#items' => $relatedPosts + ->sortByDesc(fn(Post $post) => $post->getCreatedTime()) + ->map(fn(Post $post) => $this->generateLinkToPost($post)) + ->slice(0, 3) + ->toArray(), ]; return $build; } + private function generateLinkToPost(Post $post): Link { + return Link::createFromRoute( + $post->getTitle(), + 'entity.node.canonical', + ['node' => $post->id()] + ); + } + } diff --git a/web/modules/custom/blog/src/Repository/RelatedPostsRepository.php b/web/modules/custom/blog/src/Repository/RelatedPostsRepository.php index add8998..e89e754 100644 --- a/web/modules/custom/blog/src/Repository/RelatedPostsRepository.php +++ b/web/modules/custom/blog/src/Repository/RelatedPostsRepository.php @@ -9,7 +9,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\Query\QueryInterface; use Drupal\opdavies_blog\Entity\Node\Post; use Drupal\taxonomy\TermInterface; -use Illuminate\Support\Collection; +use Tightenco\Collect\Support\Collection; final class RelatedPostsRepository {