Skip to content

Anchormenu Basicimplementation #103

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

Open
wants to merge 6 commits into
base: main
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
65 changes: 65 additions & 0 deletions Classes/Compiler/AnchorMenuCompiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace B13\Menus\Compiler;

/*
* This file is part of TYPO3 CMS-based extension "menus" by b13.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*/

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

class AnchorMenuCompiler extends AbstractMenuCompiler
{
/**
* @inheritDoc
*/
public function compile(ContentObjectRenderer $contentObjectRenderer, array $configuration): array
{

$cacheIdentifier = $this->generateCacheIdentifierForMenu('anchor', $configuration);
$excludePages = $this->parseStdWrap($configuration['excludePages'] ?? '', $configuration['excludePages.'] ?? []);
$configuration['excludePages'] = $excludePages;
$depth = (int)$contentObjectRenderer->stdWrap($configuration['depth'] ?? 1, $configuration['depth.'] ?? []);


$pageIds = $contentObjectRenderer->stdWrap($configuration['pages'] ?? $this->getPageIds($this->menuRepository->getSubPagesOfPage($this->getCurrentSite()->getRootPageId(), $depth, $configuration)), $configuration['pages.'] ?? []);
$pageIds = GeneralUtility::intExplode(',', (string)$pageIds);

$cacheIdentifier .= '-' . substr(md5(json_encode([$pageIds])), 0, 10);

return $this->cache->get($cacheIdentifier, function () use ($configuration, $pageIds) {
$pages = [];
foreach ($pageIds as $pageId) {
$page = $this->menuRepository->getPage($pageId, $configuration);
$links = $this->menuRepository->getAnchorMenu($pageId, $configuration);
$page["anchors"] = $links;
if (!empty($page)) {
$pages[$pageId] = $page;
}
}
return $pages;
});
}

/**
* Get page ids from rootline + depth
*
* @param $pageArr
* @return string
*/
public function getPageIds($pageArr)
{
$pageIds = [];
foreach ($pageArr as $page) {
$pageIds[] = $page["uid"];
}
return implode(",", $pageIds);
}
}
58 changes: 58 additions & 0 deletions Classes/ContentObject/AnchorMenuContentObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);
namespace B13\Menus\ContentObject;

/*
* This file is part of TYPO3 CMS-based extension "menus" by b13.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*/

use B13\Menus\Compiler\AnchorMenuCompiler;
use B13\Menus\PageStateMarker;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\AbstractContentObject;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

/**
* Build a menu out of a fixed list of items
*/
class AnchorMenuContentObject extends AbstractContentObject
{
protected AnchorMenuCompiler $anchorMenuCompiler;

public function __construct(ContentObjectRenderer $cObj)
{
if ((GeneralUtility::makeInstance(Typo3Version::class))->getMajorVersion() < 12) {
parent::__construct($cObj);
}
$this->anchorMenuCompiler = (GeneralUtility::makeInstance(ContentObjectServiceContainer::class))->getAnchorMenuCompiler();
}

/**
* @param array $conf
* @return string
*/
public function render($conf = [])
{
$pages = $this->anchorMenuCompiler->compile($this->cObj, $conf);
$content = $this->renderItems($pages, $conf);
return $this->cObj->stdWrap($content, $conf);
}

protected function renderItems(array $pages, array $conf): string
{
$content = '';
$cObjForItems = GeneralUtility::makeInstance(ContentObjectRenderer::class);
foreach ($pages as $page) {
PageStateMarker::markStates($page);
$cObjForItems->start($page, 'pages');
$content .= $cObjForItems->cObjGetSingle($conf['renderObj'] ?? '', $conf['renderObj.'] ?? []);
}
return $content;
}
}
18 changes: 14 additions & 4 deletions Classes/ContentObject/ContentObjectServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use B13\Menus\Compiler\LanguageMenuCompiler;
use B13\Menus\Compiler\ListMenuCompiler;
use B13\Menus\Compiler\AnchorMenuCompiler;
use B13\Menus\Compiler\TreeMenuCompiler;
use B13\Menus\Domain\Repository\MenuRepository;
use TYPO3\CMS\Core\SingletonInterface;
Expand All @@ -26,17 +27,21 @@ class ContentObjectServiceContainer implements SingletonInterface
protected LanguageMenuCompiler $languageMenuCompiler;
protected MenuRepository $menuRepository;
protected ListMenuCompiler $listMenuCompiler;
protected AnchorMenuCompiler $anchorMenuCompiler;
protected TreeMenuCompiler $treeMenuCompiler;

public function __construct(
LanguageMenuCompiler $languageMenuCompiler,
MenuRepository $menuRepository,
ListMenuCompiler $listMenuCompiler,
TreeMenuCompiler $treeMenuCompiler
) {
MenuRepository $menuRepository,
ListMenuCompiler $listMenuCompiler,
AnchorMenuCompiler $anchorMenuCompiler,
TreeMenuCompiler $treeMenuCompiler
)
{
$this->languageMenuCompiler = $languageMenuCompiler;
$this->menuRepository = $menuRepository;
$this->listMenuCompiler = $listMenuCompiler;
$this->anchorMenuCompiler = $anchorMenuCompiler;
$this->treeMenuCompiler = $treeMenuCompiler;
}

Expand All @@ -55,6 +60,11 @@ public function getListMenuCompiler(): ListMenuCompiler
return $this->listMenuCompiler;
}

public function getAnchorMenuCompiler(): AnchorMenuCompiler
{
return $this->anchorMenuCompiler;
}

public function getTreeMenuCompiler(): TreeMenuCompiler
{
return $this->treeMenuCompiler;
Expand Down
55 changes: 55 additions & 0 deletions Classes/DataProcessing/AnchorMenu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace B13\Menus\DataProcessing;

/*
* This file is part of TYPO3 CMS-based extension "menus" by b13.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*/

use B13\Menus\Compiler\AnchorMenuCompiler;
use B13\Menus\PageStateMarker;
use TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

/**
* DataProcessor to retrieve a list of pages.
*/
class AnchorMenu extends AbstractMenu
{
protected AnchorMenuCompiler $anchorMenuCompiler;

public function __construct(ContentDataProcessor $contentDataProcessor, AnchorMenuCompiler $anchorMenuCompiler)
{
$this->anchorMenuCompiler = $anchorMenuCompiler;
parent::__construct($contentDataProcessor);
}

/**
* @inheritDoc
*/
public function process(ContentObjectRenderer $cObj, array $contentObjectConfiguration, array $processorConfiguration, array $processedData)
{
if (isset($processorConfiguration['if.']) && !$cObj->checkIf($processorConfiguration['if.'])) {
return $processedData;
}

$pages = $this->anchorMenuCompiler->compile($cObj, $processorConfiguration);

foreach ($pages as &$page) {
PageStateMarker::markStates($page);
}
foreach ($pages as &$page) {
$this->processAdditionalDataProcessors($page, $processorConfiguration);
}
$targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration);
$processedData[$targetVariableName] = $pages;

return $processedData;
}
}
44 changes: 42 additions & 2 deletions Classes/Domain/Repository/MenuRepository.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

declare(strict_types=1);

namespace B13\Menus\Domain\Repository;

/*
Expand All @@ -12,11 +13,13 @@
*/

use B13\Menus\Event\PopulatePageInformationEvent;
use B13\Menus\Helpers\HelperFunctions;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\LanguageAspect;
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;

/**
* Responsible for interacting with the PageRepository class, in addition, should be responsible for overlays
Expand All @@ -27,6 +30,7 @@ class MenuRepository
protected Context $context;
protected PageRepository $pageRepository;
protected EventDispatcherInterface $eventDispatcher;
private ConnectionPool $connectionPool;

// Never show or query them.
protected $excludedDoktypes = [
Expand All @@ -35,13 +39,48 @@ class MenuRepository
PageRepository::DOKTYPE_SYSFOLDER,
];

public function __construct(Context $context, PageRepository $pageRepository, EventDispatcherInterface $eventDispatcher)
public function __construct(Context $context, PageRepository $pageRepository, EventDispatcherInterface $eventDispatcher, ConnectionPool $connectionPool)
{
$this->context = $context;
$this->pageRepository = $pageRepository;
$this->eventDispatcher = $eventDispatcher;
$this->connectionPool = $connectionPool;
}

public function getAnchorMenu(int $pageId, array $configuration): array
{
//@TODO add configuration options like e.g. filter


//Get the queryBuilder for tt_content
$queryBuilder = $this->connectionPool
->getQueryBuilderForTable('tt_content');
$result = $queryBuilder
->select("header", "tx_menus_anchor_nav_title")
->from("tt_content")
->where($queryBuilder->expr()->eq("pid", $queryBuilder->createNamedParameter($pageId)))
->andWhere($queryBuilder->expr()->eq("deleted", 0))
->andWhere($queryBuilder->expr()->eq("hidden", 0))
->andWhere($queryBuilder->expr()->eq("tx_menus_show_in_anchor_menu", 1))
->andWhere($queryBuilder->expr()->eq("CType", $queryBuilder->createNamedParameter("header")))
->orderBy("sorting")
->executeQuery();

$menu = [];
while ($row = $result->fetchAssociative()) {
$tmp = [];
$navTitle = $row["tx_menus_anchor_nav_title"];

$tmp["title"] = $row["header"];
$tmp["id"] = HelperFunctions::getAnchorId($row["header"]);
$tmp["nav_title"] = $navTitle;
$menu[] = $tmp;
}

return $menu;
}


public function getBreadcrumbsMenu(array $originalRootLine, array $configuration): array
{
$pages = [];
Expand Down Expand Up @@ -145,6 +184,7 @@ public function getSubPagesOfPage(int $pageId, int $depth, array $configuration)
'AND doktype NOT IN (' . implode(',', $excludedDoktypes) . ') ' . $whereClause,
false
);

/** @var LanguageAspect $languageAspect */
$languageAspect = $this->context->getAspect('language');
foreach ($pageTree as $k => &$page) {
Expand All @@ -153,7 +193,7 @@ public function getSubPagesOfPage(int $pageId, int $depth, array $configuration)
continue;
}
if ($depth > 0) {
$page['subpages'] = $this->getSubPagesOfPage((int)$page['uid'], $depth-1, $configuration);
$page['subpages'] = $this->getSubPagesOfPage((int)$page['uid'], $depth - 1, $configuration);
}
$this->populateAdditionalKeysForPage($page);
}
Expand Down
24 changes: 24 additions & 0 deletions Classes/Helpers/HelperFunctions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace B13\Menus\Helpers;

class HelperFunctions
{
/*
* Takes an $string and transforms it to be used as a HTML ID
*
*/
public static function getAnchorId($string)
{
//Lower case everything
$string = strtolower($string);
//Make alphanumeric (removes all other characters)
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
//Clean up multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
}

}
32 changes: 32 additions & 0 deletions Classes/ViewHelpers/AnchorViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace B13\Menus\ViewHelpers;

use B13\Menus\Helpers\HelperFunctions;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

final class AnchorViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;

protected $escapeOutput = false;

public function initializeArguments()
{
// registerArgument($name, $type, $description, $required, $defaultValue, $escape)
$this->registerArgument('title', 'string', 'The string which should be converted to be used as an html identifier', true);
}

public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
)
{
$title = $renderChildrenClosure();
return HelperFunctions::getAnchorId($title);
}

}
Loading