[FEATURE] Add debug attribute to RenderViewHelper
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Debug / RenderViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Debug;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\PathUtility;
18 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
19
20 /**
21 * Debuggable version of f:render - performs the same
22 * rendering operation but wraps the output with HTML
23 * that can be inspected with the admin panel in FE.
24 *
25 * Replaces `f:render` when the admin panel decides
26 * (see ViewHelperResolver class). Also possible to use
27 * explicitly by using `f:debug.render` instead of the
28 * normal `f:render` statement.
29 *
30 * @api
31 */
32 class RenderViewHelper extends AbstractViewHelper
33 {
34
35 /**
36 * @var bool
37 */
38 protected $escapeOutput = false;
39
40 /**
41 * Initializes additional arguments available for this view helper.
42 */
43 public function initializeArguments()
44 {
45 parent::initializeArguments();
46 $this->registerArgument('debug', 'boolean', 'If true, the admin panel shows debug information if activated,', false, true);
47 $this->registerArgument('section', 'string', 'Section to render - combine with partial to render section in partial', false, null);
48 $this->registerArgument('partial', 'string', 'Partial to render, with or without section', false, null);
49 $this->registerArgument('arguments', 'array', 'Array of variables to be transferred. Use {_all} for all variables', false, []);
50 $this->registerArgument('optional', 'boolean', 'If TRUE, considers the *section* optional. Partial never is.', false, false);
51 $this->registerArgument('default', 'mixed', 'Value (usually string) to be displayed if the section or partial does not exist', false, null);
52 $this->registerArgument('contentAs', 'string', 'If used, renders the child content and adds it as a template variable with this name for use in the partial/section', false, null);
53 }
54
55 /**
56 * Renders the content.
57 *
58 * @return string
59 * @api
60 */
61 public function render()
62 {
63 $isDebug = $this->arguments['debug'];
64 $section = $this->arguments['section'];
65 $partial = $this->arguments['partial'];
66 $arguments = (array)$this->arguments['arguments'];
67 $optional = (boolean)$this->arguments['optional'];
68 $contentAs = $this->arguments['contentAs'];
69 $tagContent = $this->renderChildren();
70
71 if ($contentAs !== null) {
72 $arguments[$contentAs] = $tagContent;
73 }
74
75 $content = '';
76 if ($partial !== null) {
77 $content = $this->viewHelperVariableContainer->getView()->renderPartial($partial, $section, $arguments, $optional);
78 } elseif ($section !== null) {
79 $content = $this->viewHelperVariableContainer->getView()->renderSection($section, $arguments, $optional);
80 }
81 // Replace empty content with default value. If default is
82 // not set, NULL is returned and cast to a new, empty string
83 // outside of this ViewHelper.
84 if ($content === '') {
85 $content = $this->arguments['default'] ?? $tagContent;
86 }
87
88 // if debug is disabled, return content
89 if (!$isDebug) {
90 return $content;
91 }
92
93 $cssRules = [];
94 $cssRules[] = 'display: block';
95 $cssRules[] = 'background-color: #fff';
96 $cssRules[] = 'padding: 5px';
97 $cssRules[] = 'border: 1px solid #f00';
98 $cssRules[] = 'color: #000';
99 $cssRules[] = 'overflow: hidden';
100 $cssWrapper = implode(';', $cssRules);
101 $cssRules[] = 'font-size: 11px';
102 $cssRules[] = 'font-family: Monospace';
103 $cssTitle = implode(';', $cssRules);
104
105 $debugInfo = [];
106 if (isset($this->arguments['partial'])) {
107 $path = $this->renderingContext->getTemplatePaths()->getPartialPathAndFilename($partial);
108 $path = PathUtility::stripPathSitePrefix($path);
109 $path = str_replace('typo3conf/ext/', 'EXT:', $path);
110 $path = str_replace('typo3/sysext/', 'EXT:', $path);
111 $debugInfo['Partial'] = 'Partial: ' . $path;
112 }
113 if (isset($this->arguments['section'])) {
114 $debugInfo['Section'] = 'Section: ' . htmlspecialchars($section);
115 }
116
117 $debugContent = sprintf(
118 '<strong>%s</strong>',
119 implode('<br />', $debugInfo)
120 );
121
122 return sprintf(
123 '<div class="t3js-debug-template" title="%s" style="%s"><span style="%s">%s</span>%s</div>',
124 htmlspecialchars(implode('/', array_keys($debugInfo))),
125 $cssTitle,
126 $cssWrapper,
127 $debugContent,
128 $content
129 );
130 }
131 }