LinkViewHelper::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
/**
4
 * Link view helper
5
 */
6
7
namespace HDNET\Tagger\ViewHelpers;
8
9
use HDNET\Tagger\LinkBuilderCallbackInterface;
10
use HDNET\Tagger\Utility\TaggerRegister;
11
use TYPO3\CMS\Core\Utility\GeneralUtility;
12
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
13
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
14
15
/**
16
 * Link view helper
17
 */
18
class LinkViewHelper extends AbstractViewHelper
19
{
20
21
    /**
22
     * Render the view Helper
23
     *
24
     * @param string $tableName
25
     * @param int    $foreignUid
26
     *
27
     * @return string
28
     */
29
    public function render($tableName, $foreignUid)
30
    {
31
        $typoLinkConfiguration = $this->getTypoLinkConfiguration($tableName, $foreignUid);
32
        $cObject = $this->objectManager->get(ContentObjectRenderer::class);
33
        return $cObject->typoLink($this->renderChildren(), $typoLinkConfiguration);
34
    }
35
36
    /**
37
     * Get typolink configuration
38
     *
39
     * @param string $tableName
40
     *
41
     * @param int $uid
42
     * @return array
43
     * @throws \Exception
44
     */
45
    protected function getTypoLinkConfiguration($tableName, $uid)
46
    {
47
        $register = TaggerRegister::getRegisterForTableName($tableName);
48
        if ($register === null) {
49
            throw new \Exception('Invalid table name in tagger registry: ' . $tableName);
50
        }
51
52
        $baseConfiguration = (array)$register['typoLinkConfiguration'];
53
        $markers = [
54
            '###TABLENAME###' => $tableName,
55
            '###UID###'       => $uid,
56
        ];
57
        
58
        if ($register['callbackClass'] && class_exists($register['callbackClass'])) {
59
            /** @var LinkBuilderCallbackInterface $callback */
60
            $callback = GeneralUtility::makeInstance($register['callbackClass']);
61
            $callback->prepareLinkBuilding($tableName, $uid, $baseConfiguration, $markers);
62
        }
63
64
        return $this->replaceInArray($baseConfiguration, $markers);
65
    }
66
67
    /**
68
     * Replace in array
69
     *
70
     * @param array $array
71
     * @param array $markers
72
     *
73
     * @return array
74
     */
75
    protected function replaceInArray(array $array, array $markers)
76
    {
77
        foreach ($array as $key => $value) {
78
            if (is_array($value)) {
79
                $array[$key] = $this->replaceInArray($value, $markers);
80
            } else {
81
                $array[$key] = str_replace(array_keys($markers), $markers, $value);
82
            }
83
        }
84
        return $array;
85
    }
86
}
87