From 4bc2da8a2ea3a55e1b1827b278921c0469667d3f Mon Sep 17 00:00:00 2001
From: Michael Kessler <michael.kessler@sgalinski.de>
Date: Fri, 20 Nov 2020 12:10:37 +0100
Subject: [PATCH] [BUGFIX] Fix NewsByAuthors crashing when multiple authors are
 given

---
 Classes/Controller/NewsByAuthorController.php | 29 ++++++++++---------
 Classes/Domain/Repository/NewsRepository.php  | 23 ++++++++++++---
 2 files changed, 35 insertions(+), 17 deletions(-)

diff --git a/Classes/Controller/NewsByAuthorController.php b/Classes/Controller/NewsByAuthorController.php
index 69ef3ee..071293e 100644
--- a/Classes/Controller/NewsByAuthorController.php
+++ b/Classes/Controller/NewsByAuthorController.php
@@ -79,25 +79,28 @@ class NewsByAuthorController extends AbstractController {
 		$categories = $newsMetaData = [];
 		$categoryRepository = $this->objectManager->get(CategoryRepository::class);
 		$excludedNewsIds = GeneralUtility::intExplode(',', $this->settings['excludedNews']);
-		foreach ($news as $newsEntry) {
-			/** @var News $newsEntry */
-			if (in_array($newsEntry->getUid(), $excludedNewsIds, TRUE)) {
-				continue;
-			}
-
-			$categoryId = $newsEntry->getPid();
-			if (!isset($categories[$categoryId])) {
-				$category = $categoryRepository->findByUid($categoryId);
-				if (!$category) {
+		foreach ($news as $newsResult) {
+			foreach ($newsResult as $newsEntry) {
+				/** @var News $newsEntry */
+				if (in_array($newsEntry->getUid(), $excludedNewsIds, TRUE)) {
 					continue;
 				}
 
-				$categories[$categoryId] = $category;
-			}
+				$categoryId = $newsEntry->getPid();
+				if (!isset($categories[$categoryId])) {
+					$category = $categoryRepository->findByUid($categoryId);
+					if (!$category) {
+						continue;
+					}
 
-			$newsMetaData[] = $this->getMetaDataForNews($newsEntry, $categories[$categoryId]);
+					$categories[$categoryId] = $category;
+				}
+
+				$newsMetaData[] = $this->getMetaDataForNews($newsEntry, $categories[$categoryId]);
+			}
 		}
 
+
 		$this->view->assign('newsMetaData', $newsMetaData);
 		$this->view->assign('authors', $authors);
 	}
diff --git a/Classes/Domain/Repository/NewsRepository.php b/Classes/Domain/Repository/NewsRepository.php
index b518f77..4b593d0 100644
--- a/Classes/Domain/Repository/NewsRepository.php
+++ b/Classes/Domain/Repository/NewsRepository.php
@@ -40,17 +40,32 @@ class NewsRepository extends AbstractRepository {
 	 * Finds all news by the given authors.
 	 *
 	 * @param array $authorIds
-	 *
-	 * @return QueryResultInterface|NULL
+	 * @return array Contains only TYPO3\CMS\Extbase\Persistence\Generic\QueryResult items
 	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
 	 */
-	public function findAllByNewsAuthor(array $authorIds): ?QueryResultInterface {
+	public function findAllByNewsAuthor(array $authorIds): ?array {
 		if (count($authorIds) <= 0) {
 			return NULL;
 		}
 
+		$result = [];
+		foreach ($authorIds as $authorId) {
+			$result[] = $this->findNewsByAuthor($authorId);
+		}
+
+		return $result;
+	}
+
+	/**
+	 * Find all news by the given author
+	 *
+	 * @param int $authorId
+	 * @return QueryResultInterface|null
+	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
+	 */
+	public function findNewsByAuthor(int $authorId): ?QueryResultInterface {
 		$query = $this->createQuery();
-		$query->matching($query->contains('newsAuthor', $authorIds));
+		$query->matching($query->contains('newsAuthor', $authorId));
 		return $query->execute();
 	}
 
-- 
GitLab