-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
ExternalViewHelper.php
80 lines (73 loc) · 2.53 KB
/
ExternalViewHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
namespace Neos\FluidAdaptor\ViewHelpers\Link;
/*
* This file is part of the Neos.FluidAdaptor package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\FluidAdaptor\Core\ViewHelper\AbstractTagBasedViewHelper;
/**
* A view helper for creating links to external targets.
*
* = Examples =
*
* <code>
* <f:link.external uri="https://www.neos.io" target="_blank">external link</f:link.external>
* </code>
* <output>
* <a href="https://www.neos.io" target="_blank">external link</a>
* </output>
*
* <code title="custom default scheme">
* <f:link.external uri="neos.io" defaultScheme="sftp">external ftp link</f:link.external>
* </code>
* <output>
* <a href="sftp://neos.io">external ftp link</a>
* </output>
*
* @api
*/
class ExternalViewHelper extends AbstractTagBasedViewHelper
{
/**
* @var string
*/
protected $tagName = 'a';
/**
* Initialize arguments
*
* @return void
* @api
*/
public function initializeArguments()
{
$this->registerUniversalTagAttributes();
$this->registerTagAttribute('name', 'string', 'Specifies the name of an anchor');
$this->registerTagAttribute('rel', 'string', 'Specifies the relationship between the current document and the linked document');
$this->registerTagAttribute('rev', 'string', 'Specifies the relationship between the linked document and the current document');
$this->registerTagAttribute('target', 'string', 'Specifies where to open the linked document');
$this->registerArgument('uri', 'string', 'the URI that will be put in the href attribute of the rendered link tag', true);
$this->registerArgument('defaultScheme', 'string', 'scheme the href attribute will be prefixed with if specified $uri does not contain a scheme already', false, 'http');
}
/**
* @return string Rendered link
* @api
*/
public function render()
{
$uri = $this->arguments['uri'];
$defaultScheme = $this->arguments['defaultScheme'];
$scheme = parse_url($uri, PHP_URL_SCHEME);
if ($scheme === null && $defaultScheme !== '') {
$uri = $defaultScheme . '://' . $uri;
}
$this->tag->addAttribute('href', $uri);
$this->tag->setContent($this->renderChildren());
$this->tag->forceClosingTag(true);
return $this->tag->render();
}
}