PluginRenderer.php 4.57 KB
Newer Older
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
<?php

namespace SGalinski\SgYoutube\Hooks\PageLayoutView;

/***************************************************************
 *  Copyright notice
 *  (c) sgalinski Internet Services (https://www.sgalinski.de)
 *  All rights reserved
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\View\PageLayoutView;
use TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
use \TYPO3\CMS\Core\Localization\LanguageService;

/**
 * Class PluginRenderer
 * Renders back-end preview for the SG YouTube Videos plugin
 *
 * @package SGalinski\SgYoutube\Hooks
 */
class PluginRenderer implements PageLayoutViewDrawItemHookInterface {
	/**
	 * Preprocesses the preview rendering of a content element of type "sg_youtube"
	 *
	 * @param PageLayoutView $parentObject Calling parent object
	 * @param bool $drawItem Whether to draw the item using the default functionality
	 * @param string $headerContent Header content
	 * @param string $itemContent Item content
	 * @param array $row Record row of tt_content
	 * @return void
	 * @noinspection ReferencingObjectsInspection
	 */
	public function preProcess(
		PageLayoutView &$parentObject, &$drawItem, &$headerContent, &$itemContent, array &$row
	): void {
		if ($row['list_type'] === 'sgyoutube_youtube') {
			$drawItem = FALSE;

54
55
			$this->adaptPluginHeaderContent($headerContent, $row);

56
			$view = GeneralUtility::makeInstance(StandaloneView::class);
Tim Wagner's avatar
Tim Wagner committed
57
			$view->setPartialRootPaths(['EXT:sg_youtube/Resources/Private/Partials/Backend']);
58
59
60
61
			$view->setTemplateRootPaths(['EXT:sg_youtube/Resources/Private/Templates/Youtube']);
			$view->setTemplate('Backend.html');
			$view->assign('uid', $row['uid']);

Tim Wagner's avatar
Tim Wagner committed
62
63
64
65
			// Get available plugin settings and their values from flexform
			$pluginConfiguration = GeneralUtility::xml2array(
				$row['pi_flexform'], 'T3DataStructure'
			)['data']['sDEF']['lDEF'];
66

Tim Wagner's avatar
Tim Wagner committed
67
68
69
			$templateData = [
				'youtubeId' => $pluginConfiguration['settings.id']['vDEF'],
				'maxResults' => $pluginConfiguration['settings.maxResults']['vDEF'],
70
				'showTitle' => (int) ($pluginConfiguration['settings.showTitle']['vDEF'] ?? 1),
Tim Wagner's avatar
Tim Wagner committed
71
72
73
74
75
76
77
78
79
80
				'showDescription' => (int) ($pluginConfiguration['settings.showDescription']['vDEF'] ?? 1),
				'disableLightbox' => $pluginConfiguration['settings.disableLightbox']['vDEF'],
				'disableLightboxMobile' => $pluginConfiguration['settings.disableLightboxMobile']['vDEF'],
				'aspectRatio' => $pluginConfiguration['settings.aspectRatio']['vDEF'],
				'thumbnailType' => $pluginConfiguration['settings.thumbnailType']['vDEF'],
				'thumbnailImagesCount' => $pluginConfiguration['settings.thumbnailImages']['vDEF'],
				'showApiResult' => $pluginConfiguration['settings.showApiResult']['vDEF'],
			];

			$view->assign('data', $templateData);
81
82
83
84

			$itemContent .= $view->render();
		}
	}
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

	/**
	 * Adapts the given $headerContent.
	 * To be used in all plugin previews so the Header Contents appear similarly.
	 *
	 * @param $headerContent
	 * @param $row
	 */
	protected function adaptPluginHeaderContent(&$headerContent, $row): void {
		$headerContent = '<h4>' . $this->getPluginNameForHeaderContent(
				(int) $row['pid'], $row['list_type']
			) . $headerContent . '</h4>';
	}

	/**
	 * Finds the label of the given $listType element on the page with the given $pid
	 * and returns it wrapped for use in the backend preview's header content.
	 *
	 * @param int $pid
	 * @param string $listType
	 * @return string
	 */
	protected function getPluginNameForHeaderContent(int $pid, string $listType): string {
		$languageService = GeneralUtility::makeInstance(LanguageService::class);

		$pluginName = $languageService->sL(
			BackendUtility::getLabelFromItemListMerged(
				$pid, 'tt_content', 'list_type', $listType
			)
		);
		return '<span class="label label-primary">' . $pluginName . '</span>&nbsp;';
	}
117
}