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

Refactor LinkHandling for TYPO3 9.5 #10

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
63 changes: 63 additions & 0 deletions Classes/LinkHandling/ShopwareLinkBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Portrino\PxShopware\LinkHandling;

use Portrino\PxShopware\LinkResolver\ArticleLinkResolver;
use Portrino\PxShopware\LinkResolver\CategoryLinkResolver;
use Portrino\PxShopware\Service\Shopware\ArticleClientInterface;
use Portrino\PxShopware\Service\Shopware\CategoryClientInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Frontend\Service\TypoLinkCodecService;
use TYPO3\CMS\Frontend\Typolink\AbstractTypolinkBuilder;

/**
* Class ShopwareLinkBuilder
* @package Portrino\PxShopware\LinkHandling
* @author Christian Hellmund <[email protected]>
*/
class ShopwareLinkBuilder extends AbstractTypolinkBuilder
{
public function __construct(ContentObjectRenderer $contentObjectRenderer, TypoScriptFrontendController $typoScriptFrontendController = null)
{
parent::__construct($contentObjectRenderer, $typoScriptFrontendController);


}

/**
* @param array $linkDetails
* @param string $linkText
* @param string $target
* @param array $conf
* @return array
*/
public function build(array &$linkDetails, string $linkText, string $target, array $conf): array
{
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);

$client = null;
$objectId = null;
if (isset($linkDetails['article'])) {
/** @var ArticleClientInterface $client */
$client = $objectManager->get(ArticleClientInterface::class);
$objectId = $linkDetails['article'];
} else if (isset($linkDetails['category'])) {
/** @var CategoryClientInterface $client */
$client = $objectManager->get(CategoryClientInterface::class);
$objectId = $linkDetails['category'];
}

if ($client && $objectId) {
/** @var \Portrino\PxShopware\Domain\Model\ShopwareModelInterface $object */
$object = $client->findById((int)$objectId);
if ($object && method_exists($object, 'getUri') && method_exists($object, 'getName')) {
return [(string)$object->getUri(), $linkText, $target];
}
}

return ['', $linkText, $target];
}
}
44 changes: 44 additions & 0 deletions Classes/LinkHandling/ShopwareLinkHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Portrino\PxShopware\LinkHandling;

use TYPO3\CMS\Core\LinkHandling\LinkHandlingInterface;

/**
* Class ShopwareLinkHandler
* @package Portrino\PxShopware\LinkHandling
* @author Christian Hellmund <[email protected]>
*/
class ShopwareLinkHandler implements LinkHandlingInterface
{
/**
* @var string
*/
protected $type;

public function asString(array $parameters): string
{
$urn = 't3://shopware';

if (!empty($parameters['article'])) {
$urn .= '?article=' . $parameters['article'];
} else if (!empty($parameters['category'])) {
$urn .= '?category=' . $parameters['category'];
}

return $urn;
}

public function resolveHandlerData(array $data): array
{
$result = [];

if (isset($data['article'])) {
$result['article'] = $data['article'];
} else if (isset($data['category'])) {
$result['category'] = $data['category'];
}

return $result;
}
}
77 changes: 0 additions & 77 deletions Classes/LinkResolver/AbstractLinkResolver.php

This file was deleted.

41 changes: 0 additions & 41 deletions Classes/LinkResolver/ArticleLinkResolver.php

This file was deleted.

41 changes: 0 additions & 41 deletions Classes/LinkResolver/CategoryLinkResolver.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Portrino\PxShopware\LinkHandler;
namespace Portrino\PxShopware\Recordlist\LinkHandler;

/***************************************************************
* Copyright notice
Expand Down Expand Up @@ -62,12 +62,11 @@ public function canHandleLink(array $linkParts)
if (!$linkParts['url']) {
return false;
}
$url = rawurldecode($linkParts['url']);
if (StringUtility::beginsWith($url, $this->getPrefix())) {
$id = intval(substr($url, strlen($this->getPrefix())));
$this->object = $this->client->findById($id);

if ($linkParts['type'] == 'shopware') {
return true;
}

return false;
}

Expand Down Expand Up @@ -156,6 +155,6 @@ public function getScriptUrl()

protected function getPrefix()
{
return 'shopware_' . $this->type . ':';
return 't3://shopware?' . $this->type . '=';
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Portrino\PxShopware\LinkHandler;
namespace Portrino\PxShopware\Recordlist\LinkHandler;

/***************************************************************
* Copyright notice
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Portrino\PxShopware\LinkHandler;
namespace Portrino\PxShopware\Recordlist\LinkHandler;

/***************************************************************
* Copyright notice
Expand Down
4 changes: 2 additions & 2 deletions Configuration/PageTSconfig/linkHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TCEMAIN.linkHandler.shopware_category {
handler = Portrino\PxShopware\LinkHandler\CategoryLinkHandler
handler = Portrino\PxShopware\Recordlist\LinkHandler\CategoryLinkHandler
label = LLL:EXT:px_shopware/Resources/Private/Language/locallang_db.xlf:link_handler.category
displayAfter = page
scanAfter = page
Expand All @@ -8,7 +8,7 @@ TCEMAIN.linkHandler.shopware_category {
}

TCEMAIN.linkHandler.shopware_article {
handler = Portrino\PxShopware\LinkHandler\ArticleLinkHandler
handler = Portrino\PxShopware\Recordlist\LinkHandler\ArticleLinkHandler
label = LLL:EXT:px_shopware/Resources/Private/Language/locallang_db.xlf:link_handler.article
displayAfter = page
scanAfter = page
Expand Down
6 changes: 3 additions & 3 deletions Configuration/TypoScript/setup.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ plugin.tx_pxshopware_pi1 {
extensionName = PxShopware
controller = Article
action = list
settings =< plugin.tx_pxshopware.settings
persistence =< plugin.tx_pxshopware.persistence
view =< plugin.tx_pxshopware.view
settings < plugin.tx_pxshopware.settings
persistence < plugin.tx_pxshopware.persistence
view < plugin.tx_pxshopware.view
}

plugin.tx_pxshopware_pi2 < plugin.tx_pxshopware_pi1
Expand Down
4 changes: 2 additions & 2 deletions Resources/Private/Partials/Article/Item.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<f:if condition="{article.url}">
<f:if condition="{article.uri}">
<f:then>
<a href="{article.url}">
<a href="{article.uri}">
<f:render section="article" arguments="{_all}" />
</a>
</f:then>
Expand Down
4 changes: 2 additions & 2 deletions Resources/Private/Partials/Category/Item.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<f:if condition="{category.url}">
<f:if condition="{category.uri}">
<f:then>
<a href="{category.url}">
<a href="{category.uri}">
<f:render section="category" arguments="{_all}" />
</a>
</f:then>
Expand Down
Loading