Commit 8f9b15f3 authored by Tim Wagner's avatar Tim Wagner
Browse files

[TASK] Create backend preview for tabs plugin

parent 3b83c2c0
<?php
namespace SGalinski\DfTabs\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\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
/**
* Class PluginRenderer - Renders backend previews for plugins.
*
* @package SGalinski\DfTabs\Hooks\PageLayoutView
*/
class PluginRenderer implements \TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface {
/**
* @inheritDoc
* @noinspection ReferencingObjectsInspection
*/
public function preProcess(
PageLayoutView &$parentObject, &$drawItem, &$headerContent, &$itemContent, array &$row
): void {
if ($row['list_type'] === 'df_tabs_plugin1') {
$drawItem = FALSE;
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->setPartialRootPaths(['EXT:df_tabs/Resources/Private/Partials/Backend']);
$view->setTemplateRootPaths(['EXT:df_tabs/Resources/Private/Templates/Backend']);
$view->setTemplate('Tabs.html');
$view->assign('uid', $row['uid']);
$this->adaptPluginHeaderContent($headerContent, $row);
// Get available plugin settings and their values from flexform
$pluginConfiguration = GeneralUtility::xml2array(
$row['pi_flexform'], 'T3DataStructure'
)['data']['sDEF']['lDEF'];
$tabElements = GeneralUtility::trimExplode(',',$pluginConfiguration['data']['vDEF'], TRUE);
$tabs = [];
foreach ($tabElements as $tabElement) {
// Each $tabElement is either 'pages_<id>' or 'tt_content_<id>'.
$recordTable = substr($tabElement, 0, strrpos($tabElement, '_'));
$recordId = substr($tabElement, strrpos($tabElement, '_') + 1);
if ($recordTable === 'pages'){
$recordTitle = BackendUtility::getRecord($recordTable, $recordId, 'title')['title'];
$recordType = 'page';
} elseif ($recordTable === 'tt_content') {
$recordTitle = BackendUtility::getRecord($recordTable, $recordId, 'header')['header'];
$recordType = 'content';
} else {
$recordTitle = $tabElement;
$recordType = 'unknown';
}
$tabs[] = [
'type' => $recordType,
'title' => $recordTitle,
'uid' => $recordId
];
}
$titles = GeneralUtility::trimExplode("\n",$pluginConfiguration['titles']['vDEF']);
// Remove first item if it is empty to mimic what is actually saved to the DB and frontend behaviour.
// This gets done automatically on a second save of the plugin anyways but it should happen every time.
if ($titles[0] === '') {
array_shift($titles);
}
$templateData = [
'mode' => $pluginConfiguration['mode']['vDEF'],
'tabs' => $tabs,
'titles' => $titles,
'enableAutoPlay' => $pluginConfiguration['enableAutoPlay']['vDEF'],
'autoPlayInterval' => $pluginConfiguration['autoPlayInterval']['vDEF'],
'enableMouseOver' => $pluginConfiguration['enableMouseOver']['vDEF'],
'hashName' => $pluginConfiguration['hashName']['vDEF']
];
$view->assign('data', $templateData);
$itemContent .= $view->render();
}
}
/**
* 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;';
}
}
......@@ -14,15 +14,15 @@
<default>pages</default>
<items type="array">
<numIndex index="0" type="array">
<numIndex index="0">LLL:EXT:df_tabs/Resources/Private/Language/locallang.xlf:flexform.sheets.general.mode.0</numIndex>
<numIndex index="0">LLL:EXT:df_tabs/Resources/Private/Language/locallang.xlf:flexform.sheets.general.mode.tt_content</numIndex>
<numIndex index="1">tt_content</numIndex>
</numIndex>
<numIndex index="1" type="array">
<numIndex index="0">LLL:EXT:df_tabs/Resources/Private/Language/locallang.xlf:flexform.sheets.general.mode.1</numIndex>
<numIndex index="0">LLL:EXT:df_tabs/Resources/Private/Language/locallang.xlf:flexform.sheets.general.mode.pages</numIndex>
<numIndex index="1">pages</numIndex>
</numIndex>
<numIndex index="2" type="array">
<numIndex index="0">LLL:EXT:df_tabs/Resources/Private/Language/locallang.xlf:flexform.sheets.general.mode.2</numIndex>
<numIndex index="0">LLL:EXT:df_tabs/Resources/Private/Language/locallang.xlf:flexform.sheets.general.mode.combined</numIndex>
<numIndex index="1">combined</numIndex>
</numIndex>
</items>
......
......@@ -10,16 +10,16 @@
</header>
<body>
<trans-unit id="flexform.sheets.general.autoPlayInterval" approved="yes">
<source><![CDATA[Autoplay Interval]]></source>
<target><![CDATA[Auto-Play-Intervall]]></target>
<source><![CDATA[Autoplay Interval in Milliseconds]]></source>
<target><![CDATA[Autoplay-Intervall in Millisekunden]]></target>
</trans-unit>
<trans-unit id="flexform.sheets.general.autoPlayInterval.description" approved="yes">
<source><![CDATA[Sets the interval for the automatic tab-switching in milliseconds (e.g. entering the value 7000 switches the tabs every 7 seconds). Is ignored if Autoplay is disabled.]]></source>
<target><![CDATA[Stellt das Intervall für das automatische Umschalten der Tabs in Millisekunden ein (z.B. die Eingabe des Wertes 7000 schaltet die Tabs alle 7 Sekunden um). Wird ignoriert, wenn Autoplay deaktiviert ist.]]></target>
</trans-unit>
<trans-unit id="flexform.sheets.general.data" approved="yes">
<source><![CDATA[Pages and content that shall be rendered as tabs]]></source>
<target><![CDATA[Seiten und Inhalte, welche als Tabs gerendert werden sollen]]></target>
<source><![CDATA[Pages and Content That Shall be Rendered as Tabs]]></source>
<target><![CDATA[Seiten und Inhalte, die als Tabs gerendert werden sollen]]></target>
</trans-unit>
<trans-unit id="flexform.sheets.general.data.description" approved="yes">
<source><![CDATA[If the selection is not empty, "Combined" mode is forced.]]></source>
......@@ -53,17 +53,17 @@
<source><![CDATA[Mode]]></source>
<target><![CDATA[Modus]]></target>
</trans-unit>
<trans-unit id="flexform.sheets.general.mode.0" approved="yes">
<source><![CDATA[Show every content element of the current page as one tab each]]></source>
<target><![CDATA[Zeige jedes Inhaltselement der aktuellen Seite als je ein Tab]]></target>
<trans-unit id="flexform.sheets.general.mode.combined" approved="yes">
<source><![CDATA[Combined (Show elements chosen below, one tab each)]]></source>
<target><![CDATA[Kombiniert (je unten ausgewähltem Element wird ein Tab angezeigt)]]></target>
</trans-unit>
<trans-unit id="flexform.sheets.general.mode.1" approved="yes">
<trans-unit id="flexform.sheets.general.mode.pages" approved="yes">
<source><![CDATA[Show every subpage of this page one tab each]]></source>
<target><![CDATA[Zeige für jede Unterseite dieser Seite jeweils einen Tab]]></target>
</trans-unit>
<trans-unit id="flexform.sheets.general.mode.2" approved="yes">
<source><![CDATA[Combined (Show elements chosen below, one tab each)]]></source>
<target><![CDATA[Kombiniert (je unten ausgewähltem Element wird ein Tab angezeigt)]]></target>
<trans-unit id="flexform.sheets.general.mode.tt_content" approved="yes">
<source><![CDATA[Show every content element of the current page as one tab each]]></source>
<target><![CDATA[Zeige jedes Inhaltselement der aktuellen Seite als je ein Tab]]></target>
</trans-unit>
<trans-unit id="flexform.sheets.general.titles" approved="yes">
<source><![CDATA[Override Tab Titles]]></source>
......@@ -95,4 +95,4 @@
</trans-unit>
</body>
</file>
</xliff>
\ No newline at end of file
</xliff>
<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.0">
<file source-language="en" target-language="de" datatype="plaintext" original="messages" date="2020-08-10T16:46:50Z" product-name="df_tabs">
<header>
<generator>XLIFF Editor</generator>
<authorName>Tim Wagner</authorName>
<authorEmail>tim.wagner@sgalinski.de</authorEmail>
</header>
<body>
<trans-unit id="backend.plugin.preview.settings" approved="yes">
<source><![CDATA[Settings]]></source>
<target><![CDATA[Einstellungen]]></target>
</trans-unit>
<trans-unit id="backend.plugin.preview.values" approved="yes">
<source><![CDATA[Values]]></source>
<target><![CDATA[Werte]]></target>
</trans-unit>
</body>
</file>
</xliff>
......@@ -10,13 +10,13 @@
</header>
<body>
<trans-unit id="flexform.sheets.general.autoPlayInterval">
<source><![CDATA[Autoplay Interval]]></source>
<source><![CDATA[Autoplay Interval in Milliseconds]]></source>
</trans-unit>
<trans-unit id="flexform.sheets.general.autoPlayInterval.description">
<source><![CDATA[Sets the interval for the automatic tab-switching in milliseconds (e.g. entering the value 7000 switches the tabs every 7 seconds). Is ignored if Autoplay is disabled.]]></source>
</trans-unit>
<trans-unit id="flexform.sheets.general.data">
<source><![CDATA[Pages and content that shall be rendered as tabs]]></source>
<source><![CDATA[Pages and Content That Shall be Rendered as Tabs]]></source>
</trans-unit>
<trans-unit id="flexform.sheets.general.data.description">
<source><![CDATA[If the selection is not empty, "Combined" mode is forced.]]></source>
......@@ -42,14 +42,14 @@
<trans-unit id="flexform.sheets.general.mode">
<source><![CDATA[Mode]]></source>
</trans-unit>
<trans-unit id="flexform.sheets.general.mode.0">
<source><![CDATA[Show every content element of the current page as one tab each]]></source>
<trans-unit id="flexform.sheets.general.mode.combined">
<source><![CDATA[Combined (Show elements chosen below, one tab each)]]></source>
</trans-unit>
<trans-unit id="flexform.sheets.general.mode.1">
<trans-unit id="flexform.sheets.general.mode.pages">
<source><![CDATA[Show every subpage of this page one tab each]]></source>
</trans-unit>
<trans-unit id="flexform.sheets.general.mode.2">
<source><![CDATA[Combined (Show elements chosen below, one tab each)]]></source>
<trans-unit id="flexform.sheets.general.mode.tt_content">
<source><![CDATA[Show every content element of the current page as one tab each]]></source>
</trans-unit>
<trans-unit id="flexform.sheets.general.titles">
<source><![CDATA[Override Tab Titles]]></source>
......@@ -74,4 +74,4 @@
</trans-unit>
</body>
</file>
</xliff>
\ No newline at end of file
</xliff>
<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.0">
<file source-language="en" datatype="plaintext" original="messages" date="2020-08-10T16:46:50Z" product-name="df_tabs">
<header>
<generator>XLIFF Editor</generator>
<authorName>Tim Wagner</authorName>
<authorEmail>tim.wagner@sgalinski.de</authorEmail>
</header>
<body>
<trans-unit id="backend.plugin.preview.settings">
<source><![CDATA[Settings]]></source>
</trans-unit>
<trans-unit id="backend.plugin.preview.values">
<source><![CDATA[Values]]></source>
</trans-unit>
</body>
</file>
</xliff>
<thead>
<tr>
<th scope="col" style="width: 50%">
<f:translate key="LLL:EXT:df_tabs/Resources/Private/Language/locallang_backend.xlf:backend.plugin.preview.settings"/>
</th>
<th scope="col" style="width: 50%">
<f:translate key="LLL:EXT:df_tabs/Resources/Private/Language/locallang_backend.xlf:backend.plugin.preview.values"/>
</th>
</tr>
</thead>
{namespace be=TYPO3\CMS\Backend\ViewHelpers}
{namespace core=TYPO3\CMS\Core\ViewHelpers}
<br/>
<table class="table table-striped table-bordered">
<f:render partial="PluginDataTableHeader"/>
<tbody>
<tr>
<th scope="row">
<f:translate
key="LLL:EXT:df_tabs/Resources/Private/Language/locallang.xlf:flexform.sheets.general.mode"/>
</th>
<td>
<f:if condition="{data.tabs.0}">
<f:then>
<f:comment><!--Combined mode is forced.--></f:comment>
<f:translate
key="LLL:EXT:df_tabs/Resources/Private/Language/locallang.xlf:flexform.sheets.general.mode.combined"/>
</f:then>
<f:else>
<f:translate
key="LLL:EXT:df_tabs/Resources/Private/Language/locallang.xlf:flexform.sheets.general.mode.{data.mode}"/>
</f:else>
</f:if>
</td>
</tr>
<f:if condition="{data.tabs.0}">
<tr>
<th scope="row">
<f:translate
key="LLL:EXT:df_tabs/Resources/Private/Language/locallang.xlf:flexform.sheets.general.data"/>
</th>
<td>
<f:for each="{data.tabs}" as="tab">
<f:switch expression="{tab.type}">
<f:case value="page">
<be:link.editRecord uid="{tab.uid}" table="pages">
<core:icon identifier="apps-pagetree-page-default"/> {tab.title} [{tab.uid}]
</be:link.editRecord>
</f:case>
<f:case value="content">
<be:link.editRecord uid="{tab.uid}" table="tt_content">
<core:icon identifier="content-header"/> {tab.title} [{tab.uid}]
</be:link.editRecord>
</f:case>
<f:defaultCase>
<core:icon identifier="default"/> {tab.title} [{tab.uid}]
</f:defaultCase>
</f:switch>
<br/>
</f:for>
</td>
</tr>
</f:if>
<f:if condition="{data.titles.0}">
<tr>
<th scope="row">
<f:translate
key="LLL:EXT:df_tabs/Resources/Private/Language/locallang.xlf:flexform.sheets.general.titles"/>
</th>
<td>
<f:for each="{data.titles}" as="title" iteration="iterator">
<f:format.raw>{title}</f:format.raw>
<br/>
</f:for>
</td>
</tr>
</f:if>
<tr>
<th scope="row">
<f:translate
key="LLL:EXT:df_tabs/Resources/Private/Language/locallang.xlf:flexform.sheets.general.enableAutoPlayInterval"/>
</th>
<td>
<f:if condition="{data.enableAutoPlay}">
<f:then>
<core:icon identifier="actions-check"/>
</f:then>
<f:else>
<core:icon identifier="actions-close"/>
</f:else>
</f:if>
</td>
</tr>
<f:if condition="{data.enableAutoPlay}">
<tr>
<th scope="row">
<f:translate
key="LLL:EXT:df_tabs/Resources/Private/Language/locallang.xlf:flexform.sheets.general.autoPlayInterval"/>
</th>
<td>
{data.autoPlayInterval}
</td>
</tr>
</f:if>
<tr>
<th scope="row">
<f:translate
key="LLL:EXT:df_tabs/Resources/Private/Language/locallang.xlf:flexform.sheets.general.enableMouseOver"/>
</th>
<td>
<f:if condition="{data.enableMouseOver}">
<f:then>
<core:icon identifier="actions-check"/>
</f:then>
<f:else>
<core:icon identifier="actions-close"/>
</f:else>
</f:if>
</td>
</tr>
<tr>
<th scope="row">
<f:translate key="LLL:EXT:df_tabs/Resources/Private/Language/locallang.xlf:flexform.sheets.general.hash"/>
</th>
<td>
{data.hashName}
</td>
</tr>
</tbody>
</table>
......@@ -35,6 +35,10 @@ call_user_func(
'plugin.tx_dftabs_plugin1.userFunc=' . \SGalinski\DfTabs\Controller\PluginController::class . '->main'
);
// Backend preview for plugin
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem'][$extKey]
= \SGalinski\DfTabs\Hooks\PageLayoutView\PluginRenderer::class;
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPageTSConfig(
'@import "EXT:' . $extKey . '/Configuration/TsConfig/Page/NewContentElementWizard.tsconfig"'
);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment