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'] = [ '#cache' => [ 'max-age' => 604800, ], '#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()] ); } }