Skip to content

Instantly share code, notes, and snippets.

@webdesign2be
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save webdesign2be/9cc36a085c59bc329573 to your computer and use it in GitHub Desktop.

Select an option

Save webdesign2be/9cc36a085c59bc329573 to your computer and use it in GitHub Desktop.

Revisions

  1. webdesign2be revised this gist Jul 2, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ConvertExternalUrisImplementation.php
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,7 @@ public function evaluate() {

    /** @var \DOMDocument $dom */
    $dom = new \DOMDocument();
    $dom->loadHTML($text);
    $dom->loadHTML('<?xml encoding="UTF-8">' . $text);
    /** @var \DOMNode $node */
    foreach ($dom->getElementsByTagName('a') as $node) {
    if (preg_match('~^(?:f|ht)tps?://~i', $node->getAttribute('href'))){
  2. webdesign2be created this gist Jul 2, 2014.
    49 changes: 49 additions & 0 deletions ConvertExternalUrisImplementation.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    <?php
    namespace Sms\Lsk\TypoScript;

    use TYPO3\Flow\Annotations as Flow;
    use TYPO3\Neos\Domain\Exception;
    use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
    use TYPO3\TypoScript\TypoScriptObjects\AbstractTypoScriptObject;

    class ConvertExternalUrisImplementation extends AbstractTypoScriptObject {

    /**
    * The string to be processed
    *
    * @return string
    */
    public function getValue() {
    return $this->tsValue('value');
    }

    /**
    * @return string
    * @throws Exception
    */
    public function evaluate() {
    $text = $this->getValue();
    if (!is_string($text)) {
    throw new Exception(sprintf('Only strings can be processed by this TypoScript object, given: "%s".', gettype($text)), 1382624080);
    }
    $currentContext = $this->tsRuntime->getCurrentContext();
    $node = $currentContext['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') {
    return $text;
    }

    /** @var \DOMDocument $dom */
    $dom = new \DOMDocument();
    $dom->loadHTML($text);
    /** @var \DOMNode $node */
    foreach ($dom->getElementsByTagName('a') as $node) {
    if (preg_match('~^(?:f|ht)tps?://~i', $node->getAttribute('href'))){
    $node->setAttribute('target', '_blank');
    }
    }
    return $dom->saveHTML();
    }
    }