TYPO3: Links in backend previews of fluidcontent elements

revision f60cd3a253eb40567622aba0a36093c14dbe4772

raw

BackendPreviewLinkViewHelper.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
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace Tx\Foo\ViewHelpers;
 
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
 
/**
 * Create a HTML link to a page in the backend.
 * Useful for links in the backend preview.
 *
 * @author Christian Weiske <weiske@mogic.com>
 */
class BackendPreviewLinkViewHelper extends AbstractViewHelper
{
    /**
     * Create a HTML link
     *
     * @param string $parameter Typolink
     *
     * @return string HTML
     */
    public function render($parameter)
    {
        $attributes = '';
        $text = $this->renderChildren();
        if (trim($text) == '') {
            $text = '(no text)';
        }
 
        if (trim($parameter) == '') {
            return $text;
        }
 
        if (is_numeric($parameter)) {
            //page id
            $url = BackendUtility::getModuleUrl(
                'web_layout',
                array(
                    'id' => $parameter
                )
            );
            $title = 'Page #' . $parameter;
        } else if (preg_match('.\\d+#\\d+$.', $parameter)) {
            //page id and content element ID
            list($pageId, $contentId) = explode('#', $parameter);
            $url = BackendUtility::getModuleUrl(
                'web_layout',
                array(
                    'id' => $pageId
                )
            ) . '#element-tt_content-' . $contentId;
            $title = 'Page #' . $pageId . ', element #' . $contentId;
        } else if (substr($parameter, 0, 5) == 'file:') {
            //File
            return $text . ' | <tt>' . htmlspecialchars($parameter) . '</tt>';
        } else if (substr($parameter, 0, 7) == 'http://'
            || substr($parameter, 0, 8) == 'https://'
        ) {
            //External URL
            list($url) = explode(' ', $parameter);
            $title = $parameter;
            $attributes = ' target="_blank"';
            $extra = '➚';
        } else if (preg_match('#^[^ ]+@[^a]#', $parameter)) {
            //email
            list($address) = explode(' ', $parameter);
            $url   = 'mailto:' . $address;
            $title = $parameter;
        } else if ($parameter{0} == '/') {
            //custom relative URL
            $url   = $parameter;
            $title = $parameter;
            $attributes = ' target="_blank"';
            $extra = '➚';
        } else {
            //unknown
            return '<tt>' . htmlspecialchars($parameter) . '</tt>';
        }
 
        return '<a href="' . htmlspecialchars($url) . '"'
            . ' title="' . htmlspecialchars($title) . '"'
            . ' style="text-decoration: underline"'
            . $attributes
            . '>'
            . $text
            . '</a>'
            . $extra;
    }
}
?>
 
raw

fluid-usage.xml

1
2
3
4
5
6
<html xmlns:foo="http://typo3.org/ns/Tx/Foo/ViewHelpers">
...
<f:section name="Preview">
 Link: <foo:backendPreviewLink parameter="{settings.link}">{settings.header}</wsb:be.link>
</f:section>
...

History