Passed
Push — master ( c38fc4...4e12b6 )
by
unknown
13:55
created

ButtonViewHelper::initializeArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Dashboard\ViewHelpers\Widget;
19
20
use TYPO3\CMS\Dashboard\Widgets\ButtonProviderInterface;
21
use TYPO3\CMS\Dashboard\Widgets\ElementAttributesInterface;
22
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
23
24
/**
25
 * @internal
26
 *
27
 * Renders a dashboard button
28
 *
29
 * Examples
30
 * ========
31
 *
32
 * ::
33
 *
34
 *    <dashboard:widget.button button="{button}" class="widget-cta">
35
 *        {f:translate(id: button.title, default: button.title)}
36
 *    </dashboard:widget.button button="{button}" class="widget-cta">
37
 */
38
class ButtonViewHelper extends AbstractTagBasedViewHelper
39
{
40
    /**
41
     * @var string
42
     */
43
    protected $tagName = 'a';
44
45
    public function initializeArguments(): void
46
    {
47
        parent::initializeArguments();
48
        $this->registerUniversalTagAttributes();
49
        $this->registerArgument('button', ButtonProviderInterface::class, 'Dashboard widget button', true);
50
    }
51
52
    public function render(): string
53
    {
54
        $button = $this->arguments['button'];
55
56
        $this->tag->addAttribute('href', $button->getLink() ?: '#');
57
58
        $target = $button->getTarget();
59
        if ($target !== '') {
60
            $this->tag->addAttribute('target', $target);
61
            if ($target === '_blank') {
62
                $this->tag->addAttribute('rel', 'noreferrer');
63
            }
64
        }
65
66
        if ($button instanceof ElementAttributesInterface) {
67
            $this->tag->addAttributes($button->getElementAttributes());
68
        }
69
70
        $this->tag->setContent($this->renderChildren());
71
        $this->tag->forceClosingTag(true);
72
73
        return $this->tag->render();
74
    }
75
}
76