Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add custom rss feed configuration #267

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 42 additions & 15 deletions Classes/Controller/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,36 +117,38 @@ protected function initializeView(ViewInterface $view): void
{
parent::initializeView($view);
if ($this->request->getFormat() === 'rss') {
$action = '.' . $this->request->getControllerActionName();
$action = $this->request->getControllerActionName();
$arguments = [];
switch ($action) {
case '.listPostsByCategory':
case 'listPostsByCategory':
if (isset($this->arguments['category'])) {
$arguments[] = $this->arguments['category']->getValue()->getTitle();
}
break;
case '.listPostsByDate':
case 'listPostsByDate':
$arguments[] = (int)$this->arguments['year']->getValue();
if (isset($this->arguments['month'])) {
$arguments[] = (int)$this->arguments['month']->getValue();
}
break;
case '.listPostsByTag':
case 'listPostsByTag':
if (isset($this->arguments['tag'])) {
$arguments[] = $this->arguments['tag']->getValue()->getTitle();
}
break;
case '.listPostsByAuthor':
case 'listPostsByAuthor':
if (isset($this->arguments['author'])) {
$arguments[] = $this->arguments['author']->getValue()->getName();
}
break;
default:
case ([] !== ($this->settings['demand'] ?? [])):
$this->actionMethodName = 'listByDemandAction';
break;
}

$feedData = [
'title' => LocalizationUtility::translate('feed.title' . $action, 'blog', $arguments),
'description' => LocalizationUtility::translate('feed.description' . $action, 'blog', $arguments),
'title' => LocalizationUtility::translate('feed.title.' . $action, 'blog', $arguments),
'description' => LocalizationUtility::translate('feed.description.' . $action, 'blog', $arguments),
'language' => $this->getSiteLanguage()->getTwoLetterIsoCode(),
'link' => $this->getRequestUrl(),
'date' => date('r'),
Expand Down Expand Up @@ -185,14 +187,23 @@ public function listRecentPostsAction(int $currentPage = 1): ResponseInterface
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function listByDemandAction(): ResponseInterface
public function listByDemandAction(int $currentPage = 1): ResponseInterface
{
$repositoryDemand = $this->postRepositoryDemandFactory->createFromSettings($this->settings['demand'] ?? []);
$posts = $this->postRepository->findByRepositoryDemand($repositoryDemand);

if ([] !== $repositoryDemand->getPosts()) {
$posts = $this->sortPostsByManualOrder($posts, $repositoryDemand->getPosts());
$pagination = [];
} else {
$pagination = $this->getPagination($posts, $currentPage);
}

$this->view->assign('type', 'demand');
$this->view->assign('demand', $repositoryDemand);
$this->view->assign('posts', $this->postRepository->findByRepositoryDemand($repositoryDemand));
$this->view->assign('pagination', []);
$this->view->assign('posts', $posts);
$this->view->assign('pagination', $pagination);

return $this->htmlResponse();
}

Expand Down Expand Up @@ -465,12 +476,28 @@ private function getRequestUrl(): string
protected function getPagination(QueryResultInterface $objects, int $currentPage = 1): ?BlogPagination
{
$maximumNumberOfLinks = (int) ($this->settings['lists']['pagination']['maximumNumberOfLinks'] ?? 0);
$itemsPerPage = 10;
if ($this->request->getFormat() === 'html') {
$itemsPerPage = (int) ($this->settings['lists']['pagination']['itemsPerPage'] ?? 10);
}
$itemsPerPage = (int) ($this->settings['lists']['pagination']['itemsPerPage'] ?? 10);

$paginator = new QueryResultPaginator($objects, $currentPage, $itemsPerPage);
return new BlogPagination($paginator, $maximumNumberOfLinks);
}

/**
* @param Post[] $posts
* @param int[] $postUids
*
* @return Post[]
*/
protected function sortPostsByManualOrder(array $posts, array $postUids): array
{
// Sort manually selected posts by defined order in group field
$sortedPosts = array_flip($postUids);
foreach ($posts as $post) {
$sortedPosts[$post->getUid()] = $post;
}

return array_values(array_filter($sortedPosts, static function ($value) {
return $value instanceof Post;
}));
}
}
21 changes: 4 additions & 17 deletions Classes/Domain/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ public function findByUidRespectQuerySettings(int $uid)

/**
* @param PostRepositoryDemand $repositoryDemand;
* @return Post[]
*
* @return Post[]|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findByRepositoryDemand(PostRepositoryDemand $repositoryDemand): array
public function findByRepositoryDemand(PostRepositoryDemand $repositoryDemand)
{
$query = $this->createQuery();

Expand Down Expand Up @@ -113,21 +114,7 @@ public function findByRepositoryDemand(PostRepositoryDemand $repositoryDemand):
$query->setLimit($limit);
}

/** @var Post[] $result */
$result = $query->execute()->toArray();

if ($repositoryDemand->getPosts() !== []) {
// Sort manually selected posts by defined order in group field
$sortedPosts = array_flip($repositoryDemand->getPosts());
foreach ($result as $post) {
$sortedPosts[$post->getUid()] = $post;
}
$result = array_values(array_filter($sortedPosts, function ($value) {
return $value instanceof Post;
}));
}

return $result;
return $query->execute();
}

/**
Expand Down
5 changes: 4 additions & 1 deletion Classes/Factory/PostRepositoryDemandFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ public function createFromSettings(array $settings): PostRepositoryDemand
$demand->setTagsConjunction($settings['tagsConjunction']);
}

if (isset($GLOBALS['TCA']['pages']['columns'][$settings['sortBy']])) {
if ('' !== ($settings['ordering'] ?? null)) {
$ordering = explode(' ', $settings['ordering']);
$demand->setOrdering($ordering[0], $ordering[1] ?? 'ASC');
} else if (isset($GLOBALS['TCA']['pages']['columns'][$settings['sortBy']])) {
$direction = strtoupper($settings['sortDirection'] ?? 'ASC');
if (!in_array($direction, ['ASC', 'DESC'], true)) {
$direction = 'ASC';
Expand Down
17 changes: 17 additions & 0 deletions Documentation/Configuration/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1683,3 +1683,20 @@ plugin.tx_blog.settings.meta.listfooter.elements.comments.enable
Description
**Show comments in the meta section:**

TYPO3 Blog: RSS Feed
--------------------

Custom configuration
^^^^^^^^^^^^^^^^^^^^

You can modify the rss feed configuration by setting the `PostRepositoryDemand` DTO via TypoScript for the rss feed pagetype

.. code-block:: typoscript

[getTSFE().type == 200]
plugin.tx_blog.settings.demand {
categories = 15,153,158,151,16,87,251,88,18,22,89,19
categoriesConjunction = OR
ordering = publish_date DESC
}
[end]