Last active
April 23, 2018 11:28
-
-
Save aertmann/33c74c10f76edc26ed67 to your computer and use it in GitHub Desktop.
Add target _blank to external links in Neos – Included in https://speakerdeck.com/aertmann/tasty-recipes-for-every-day-neos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Acme\Acme\TypoScript; | |
| use TYPO3\Flow\Annotations as Flow; | |
| use TYPO3\Neos\Domain\Exception; | |
| use TYPO3\Neos\Service\LinkingService; | |
| use TYPO3\TYPO3CR\Domain\Model\NodeInterface; | |
| use TYPO3\TypoScript\TypoScriptObjects\AbstractTypoScriptObject; | |
| /** | |
| * A TypoScript Object that converts link references in the format "<type>://<UUID>" to proper URIs | |
| * | |
| * Right now node://<UUID> and asset://<UUID> are supported URI schemes. | |
| * | |
| * Usage:: | |
| * | |
| * someTextProperty.@process.1 = TYPO3.Neos:ConvertUris | |
| * | |
| * The optional property ``forceConversion`` can be used to have the links converted even when not | |
| * rendering the live workspace. This is used for links that are not inline editable (for | |
| * example links on images):: | |
| * | |
| * someTextProperty.@process.1 = TYPO3.Neos:ConvertUris { | |
| * forceConversion = true | |
| * } | |
| */ | |
| class ConvertUrisImplementation extends \TYPO3\Neos\TypoScript\ConvertUrisImplementation { | |
| /** | |
| * Convert URIs matching a supported scheme with generated URIs | |
| * | |
| * 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. | |
| * | |
| * @return string | |
| * @throws Exception | |
| */ | |
| public function evaluate() { | |
| $text = parent::evaluate(); | |
| 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); | |
| } | |
| try { | |
| $processedContent = preg_replace_callback( | |
| '~<a.*?href="(.*?)".*?>~i', | |
| function ($matches) { | |
| $uriHost = parse_url($matches[1], PHP_URL_HOST); | |
| if ( | |
| (is_string($uriHost) && $uriHost != $_SERVER["HTTP_HOST"]) || | |
| strpos($matches[1], '_Resources') !== false | |
| ) { | |
| if (!preg_match_all('~target="(.*?)~i', $matches[0], $targetMatches)) { | |
| return str_replace('<a', '<a target="_blank"', $matches[0]); | |
| } | |
| } | |
| return $matches[0]; | |
| }, | |
| $text | |
| ); | |
| } catch (\Exception $e) { | |
| die($e->getMessage()); | |
| } | |
| return $processedContent; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When doing this, adjust it to add
rel="noopener"as done in https://github.com/neos/neos-development-collection/pull/1729/files as of Neos 4.0