Neos\Neos\TypoScript\ConvertUrisImplementation::evaluate PHP Method

evaluate() public method

If the workspace of the current node context is not live, no replacement will be done unless forceConversion is set. This is needed to show the editable links with metadata in the content module.
public evaluate ( ) : string
return string
    public function evaluate()
    {
        $text = $this->tsValue('value');
        if ($text === '' || $text === null) {
            return '';
        }
        if (!is_string($text)) {
            throw new Exception(sprintf('Only strings can be processed by this TypoScript object, given: "%s".', gettype($text)), 1382624080);
        }
        $node = $this->tsValue('node');
        if (!$node instanceof NodeInterface) {
            throw new Exception(sprintf('The current node must be an instance of NodeInterface, given: "%s".', gettype($text)), 1382624087);
        }
        if ($node->getContext()->getWorkspace()->getName() !== 'live' && !$this->tsValue('forceConversion')) {
            return $text;
        }
        $unresolvedUris = array();
        $linkingService = $this->linkingService;
        $controllerContext = $this->tsRuntime->getControllerContext();
        $absolute = $this->tsValue('absolute');
        $processedContent = preg_replace_callback(LinkingService::PATTERN_SUPPORTED_URIS, function (array $matches) use($node, $linkingService, $controllerContext, &$unresolvedUris, $absolute) {
            switch ($matches[1]) {
                case 'node':
                    $resolvedUri = $linkingService->resolveNodeUri($matches[0], $node, $controllerContext, $absolute);
                    $this->tsRuntime->addCacheTag('node', $matches[2]);
                    break;
                case 'asset':
                    $resolvedUri = $linkingService->resolveAssetUri($matches[0]);
                    $this->tsRuntime->addCacheTag('asset', $matches[2]);
                    break;
                default:
                    $resolvedUri = null;
            }
            if ($resolvedUri === null) {
                $unresolvedUris[] = $matches[0];
                return $matches[0];
            }
            return $resolvedUri;
        }, $text);
        if ($unresolvedUris !== array()) {
            $processedContent = preg_replace('/<a[^>]* href="(node|asset):\\/\\/[^"]+"[^>]*>(.*?)<\\/a>/', '$2', $processedContent);
            $processedContent = preg_replace(LinkingService::PATTERN_SUPPORTED_URIS, '', $processedContent);
        }
        $processedContent = $this->replaceLinkTargets($processedContent);
        return $processedContent;
    }

Usage Example

 /**
  * This test checks that targets for resource links are correctly replaced
  *
  * @test
  */
 public function evaluateReplaceResourceLinkTargets()
 {
     $assetIdentifier = 'aeabe76a-551a-495f-a324-ad9a86b2aff8';
     $resourceLinkTarget = '_blank';
     $value = 'This string contains two asset links and an external link: one with a target set <a target="top" href="asset://' . $assetIdentifier . '">example</a> and one without a target <a href="asset://' . $assetIdentifier . '">example2</a> and an external link <a href="http://www.example.org">example3</a>';
     $this->addValueExpectation($value, null, false, null, $resourceLinkTarget);
     $this->mockWorkspace->expects($this->any())->method('getName')->will($this->returnValue('live'));
     $self = $this;
     $this->mockLinkingService->expects($this->atLeastOnce())->method('resolveAssetUri')->will($this->returnCallback(function ($assetUri) use($self, $assetIdentifier) {
         if ($assetUri !== 'asset://' . $assetIdentifier) {
             $self->fail('Unexpected asset URI "' . $assetUri . '"');
         }
         return 'http://localhost/_Resources/01';
     }));
     $expectedResult = 'This string contains two asset links and an external link: one with a target set <a target="' . $resourceLinkTarget . '" href="http://localhost/_Resources/01">example</a> and one without a target <a target="' . $resourceLinkTarget . '" href="http://localhost/_Resources/01">example2</a> and an external link <a href="http://www.example.org">example3</a>';
     $actualResult = $this->convertUrisImplementation->evaluate();
     $this->assertSame($expectedResult, $actualResult);
 }
ConvertUrisImplementation