Skip to content

Commit

Permalink
[FEATURE] Support for TypolinkParameter objects in TYPO3 v13
Browse files Browse the repository at this point in the history
  • Loading branch information
s2b committed Oct 19, 2024
1 parent a529e86 commit 9b68de8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
29 changes: 28 additions & 1 deletion Classes/Domain/Model/Typolink.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
use SMS\FluidComponents\Exception\InvalidArgumentException;
use SMS\FluidComponents\Interfaces\ConstructibleFromArray;
use SMS\FluidComponents\Interfaces\ConstructibleFromInteger;
use SMS\FluidComponents\Interfaces\ConstructibleFromTypolinkParameter;
use TYPO3\CMS\Core\LinkHandling\LinkService;
use TYPO3\CMS\Core\LinkHandling\TypoLinkCodecService;
use TYPO3\CMS\Core\LinkHandling\TypolinkParameter;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

/**
* Data Structure to provide information extracted from a Typolink string
* in a structured matter.
*/
class Typolink extends Link implements ConstructibleFromInteger, ConstructibleFromArray
class Typolink extends Link implements ConstructibleFromInteger, ConstructibleFromArray, ConstructibleFromTypolinkParameter
{
/**
* Data interpretation of the provided TYPO3 uri.
Expand Down Expand Up @@ -116,6 +118,31 @@ public static function fromArray(array $typolinkData): self
return $instance;
}

public static function fromTypolinkParameter(TypolinkParameter $parameter): self
{
// Analyze structure of provided TYPO3 uri
$linkService = GeneralUtility::makeInstance(LinkService::class);
$uriStructure = $linkService->resolve($parameter->url);

// Generate general purpose uri (https://) from TYPO3 uri (t3://)
// Could also be a mailto or tel uri
$cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
$uri = $cObj->typoLink_URL([
'parameter' => $parameter->url,
'additionalParams' => $parameter->additionalParams,
]);

// TODO constructor code should be avoided in the future, but this would require changes
// to the current constructure method signature (such as making the string nullable),
// which would be a breaking change.
return (new static(''))
->setUri($uri)
->setOriginalLink($uriStructure)
->setTarget($parameter->target)
->setClass($parameter->class)
->setTitle($parameter->title);
}

public function setOriginalLink(array $originalLink): self
{
$this->originalLink = $originalLink;
Expand Down
18 changes: 18 additions & 0 deletions Classes/Interfaces/ConstructibleFromTypolinkParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace SMS\FluidComponents\Interfaces;

use TYPO3\CMS\Core\LinkHandling\TypolinkParameter;

/**
* ConstructibleFromTypolinkParameter defines an alternative constructor
* which "converts" the provided ConstructibleFromTypolinkParameter instance
* to the class implementing the interface.
*/
interface ConstructibleFromTypolinkParameter
{
/**
* Creates an instance of the class based on the provided object.
*/
public static function fromTypolinkParameter(TypolinkParameter $value): object;
}
6 changes: 6 additions & 0 deletions Classes/Utility/ComponentArgumentConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
use SMS\FluidComponents\Interfaces\ConstructibleFromInteger;
use SMS\FluidComponents\Interfaces\ConstructibleFromNull;
use SMS\FluidComponents\Interfaces\ConstructibleFromString;
use SMS\FluidComponents\Interfaces\ConstructibleFromTypolinkParameter;
use Traversable;
use TYPO3\CMS\Core\LinkHandling\TypolinkParameter;
use TYPO3\CMS\Core\Resource\AbstractFile;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
Expand Down Expand Up @@ -66,6 +68,10 @@ class ComponentArgumentConverter implements \TYPO3\CMS\Core\SingletonInterface
ConstructibleFromFileInterface::class,
'fromFileInterface',
],
TypolinkParameter::class => [
ConstructibleFromTypolinkParameter::class,
'fromTypolinkParameter',
],
];

/**
Expand Down

0 comments on commit 9b68de8

Please sign in to comment.