Skip to content

Commit

Permalink
Merge pull request #178 from pixelant/addCategoryContentMapping
Browse files Browse the repository at this point in the history
Add category page content mapping
  • Loading branch information
Pavlo Zaporozkyi authored Dec 16, 2019
2 parents 69ba530 + e875814 commit 89ece9d
Show file tree
Hide file tree
Showing 19 changed files with 554 additions and 6 deletions.
42 changes: 42 additions & 0 deletions Classes/Domain/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ class Category extends CategoryExtbase
*/
protected $slug = '';

/**
* @var int
*/
protected $contentPage = 0;

/**
* @var int
*/
protected $contentColPos = 0;

/**
* __construct
*/
Expand Down Expand Up @@ -460,4 +470,36 @@ public function setSlug(string $slug)
{
$this->slug = $slug;
}

/**
* @return int
*/
public function getContentPage(): int
{
return $this->contentPage;
}

/**
* @param int $contentPage
*/
public function setContentPage(int $contentPage): void
{
$this->contentPage = $contentPage;
}

/**
* @return int
*/
public function getContentColPos(): int
{
return $this->contentColPos;
}

/**
* @param int $contentColPos
*/
public function setContentColPos(int $contentColPos): void
{
$this->contentColPos = $contentColPos;
}
}
22 changes: 22 additions & 0 deletions Classes/Domain/Repository/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,26 @@ private function getChildrenCategoriesRecursive(array $categories, int $counter

return $result;
}

/**
* @param int $pageId
* @return array|QueryResultInterface
*/
public function findByRelatedToContentPage(int $pageId)
{
$query = $this->findAll()->getQuery();
$results = $query->matching(
$query->equals(
'contentPage',
$pageId
)
)->execute(true);

return array_map(function ($result) {
return [
'uid' => $result['uid'],
'title' => $result['title']
];
}, $results);
}
}
66 changes: 66 additions & 0 deletions Classes/Hook/PageHookRelatedCategories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);

namespace Pixelant\PxaProductManager\Hook;

use Pixelant\PxaProductManager\Domain\Repository\CategoryRepository;
use Pixelant\PxaProductManager\Service\BackendUriService;
use Pixelant\PxaProductManager\Service\TemplateService;
use Pixelant\PxaProductManager\Utility\ConfigurationUtility;
use TYPO3\CMS\Backend\Controller\PageLayoutController;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;

/**
* Class PageHookRelatedCategories
* @package Pixelant\PxaProductManager\Hook
*/
class PageHookRelatedCategories
{
/**
* @param array $params
* @param PageLayoutController $pageLayoutController
* @return string
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
*/
public function render(array $params, PageLayoutController $pageLayoutController)
{
/** @var CategoryRepository $categoriesRepository */
$categoriesRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(CategoryRepository::class);
$categories = $categoriesRepository->findByRelatedToContentPage($pageLayoutController->id);

if (!$categories) {
return '';
}

/** @var BackendUriService $backendUriService */
$backendUriService = GeneralUtility::makeInstance(BackendUriService::class);

$data = [];

foreach ($categories as $category) {
$uri = $backendUriService->buildUri('record_edit', [
"edit[sys_category][{$category['uid']}]" => 'edit',
'returnUrl' => \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('REQUEST_URI')
]);

$data[] = [
'uri' => $uri,
'title' => $category['title']
];
}

$backendPathsTs = ConfigurationUtility::getTSConfig()['backend'];

/** @var TemplateService $templateService */
$templateService = GeneralUtility::makeInstance(TemplateService::class);

$templateService->setTemplateRootPaths($backendPathsTs['templateRootPaths'])
->setPartialRootPaths($backendPathsTs['partialRootPaths'])
->setLayoutRootPaths($backendPathsTs['layoutRootPaths']);

return $templateService->generateStandaloneTemplate('PageModule/RelatedCategories', [
'data' => $data
]);
}
}
29 changes: 29 additions & 0 deletions Classes/Service/BackendUriService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

namespace Pixelant\PxaProductManager\Service;

use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class BackendUriService
{
/**
* @param string $module
* @param array $params
* @return string
*/
public function buildUri(string $module, array $params)
{
/** @var UriBuilder $uriBuilder */
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);

try {
$uri = $uriBuilder->buildUriFromRoute($module, $params, UriBuilder::ABSOLUTE_URL);
} catch (\TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException $e) {
return '';
}

return (string)$uri;
}
}
97 changes: 97 additions & 0 deletions Classes/Service/TemplateService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
declare(strict_types=1);

namespace Pixelant\PxaProductManager\Service;

use Pixelant\PxaProductManager\Utility\ConfigurationUtility;
use Pixelant\PxaProductManager\Utility\MainUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;

class TemplateService
{
/**
* @var StandaloneView
*/
protected $template;

/**
* @var array
*/
protected $layoutRootPaths;

/**
* @var array
*/
protected $templateRootPaths;

/**
* @var array
*/
protected $partialRootPaths;

/**
* TemplateService constructor.
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
*/
public function __construct()
{
$this->template = MainUtility::getObjectManager()->get(StandaloneView::class);
$viewTs = ConfigurationUtility::getTSConfig()['view'];
$this->template->setTemplateRootPaths($viewTs['templateRootPaths']);
$this->template->setPartialRootPaths($viewTs['partialRootPaths']);
$this->template->setLayoutRootPaths($viewTs['layoutRootPaths']);
}

/**
* @param string $templateName
* @param array $variables
* @return string
*/
public function generateStandaloneTemplate(string $templateName, array $variables = [])
{
$this->template->setTemplate($templateName);
$this->template->assignMultiple($variables);

return $this->template->render();
}

/**
* @param string $controller
* @return TemplateService
*/
public function setController(string $controller): TemplateService
{
$this->template->getRenderingContext()->setControllerName($controller);
return $this;
}

/**
* @param mixed $layoutRootPaths
* @return TemplateService
*/
public function setLayoutRootPaths(array $layoutRootPaths)
{
$this->template->setLayoutRootPaths($layoutRootPaths);
return $this;
}

/**
* @param array $templateRootPaths
* @return TemplateService
*/
public function setTemplateRootPaths(array $templateRootPaths): TemplateService
{
$this->template->setTemplateRootPaths($templateRootPaths);
return $this;
}

/**
* @param array $partialRootPaths
* @return TemplateService
*/
public function setPartialRootPaths(array $partialRootPaths): TemplateService
{
$this->template->setPartialRootPaths($partialRootPaths);
return $this;
}
}
36 changes: 36 additions & 0 deletions Classes/UserFunction/SysCategoryExtendedTca.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);

namespace Pixelant\PxaProductManager\UserFunction;

use Pixelant\PxaProductManager\Service\BackendUriService;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class SysCategoryExtendedTca
{
/**
* @param $PA
* @param $fObj
* @return string
* @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
*/
public function pageModuleLinkField($PA, $fObj)
{
if (! current($PA['row']['pxapm_content_page'])) {
return '';
}

$page = current($PA['row']['pxapm_content_page']);

/** @var BackendUriService $backendUriService */
$backendUriService = GeneralUtility::makeInstance(BackendUriService::class);

$uri = $backendUriService->buildUri('web_layout', [
'id' => $page['uid']
]);

return "<a class='btn btn-default' href='{$uri}'>
<span class='text-primary'>{$page['title']}</span>
</a>";
}
}
44 changes: 43 additions & 1 deletion Configuration/TCA/Overrides/sys_category.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,52 @@
'maxitems' => 1,
'eval' => ''
]
]
],
'pxapm_content_page' => [
'exclude' => 1,
'onChange' => 'reload',
'label' => $ll . 'sys_category.pxapm_content_page',
'config' => [
'type' => 'group',
'internal_type' => 'db',
'allowed' => 'pages',
'size' => 1,
'max_size' => 1,
],
],
'pxapm_content_page_link' => [
'exclude' => 1,
'displayCond' => 'FIELD:pxapm_content_page:>:0',
'label' => $ll . 'sys_category.pxapm_content_page_link',
'config' => [
'type' => 'user',
'userFunc' => \Pixelant\PxaProductManager\UserFunction\SysCategoryExtendedTca::class . '->pageModuleLinkField',
],
],
'pxapm_content_colpos' => [
'exclude' => 1,
'displayCond' => 'FIELD:pxapm_content_page:>:0',
'label' => $ll . 'sys_category.pxapm_content_colpos',
'config' => [
'type' => 'input',
'size' => 30,
'default' => 0
]
],
];
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('sys_category', $tempColumns);

// Additional fields
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'sys_category',
'--div--;' . $ll . 'sys_category.content_tab,
pxapm_content_page,
pxapm_content_page_link,
pxapm_content_colpos',
'',
'after:parent'
);

// Additional fields
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'sys_category',
Expand Down
Loading

1 comment on commit 89ece9d

@NamelessCoder
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Error code: 0 - Error Fetching http headers

  • NamelessCoder\GizzleGitPlugins\GizzlePlugins\ClonePlugin:000000007087a5dc00000000796cac4e:

Please sign in to comment.