Skip to content

Commit

Permalink
[TASK] Added PluginPreview, Converted list to CType and added migration
Browse files Browse the repository at this point in the history
  • Loading branch information
hojalatheef committed Dec 6, 2024
1 parent a895605 commit 33a070f
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 61 deletions.
97 changes: 97 additions & 0 deletions Classes/Backend/Preview/Weather2PluginPreview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package jweiland/weather2.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace JWeiland\Weather2\Backend\Preview;

use TYPO3\CMS\Backend\Preview\StandardContentPreviewRenderer;
use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridColumnItem;
use TYPO3\CMS\Core\Service\FlexFormService;
use TYPO3\CMS\Core\View\ViewFactoryData;
use TYPO3\CMS\Core\View\ViewFactoryInterface;
use TYPO3\CMS\Core\View\ViewInterface;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

/**
* Add plugin preview for EXT:weather2
*/
class Weather2PluginPreview extends StandardContentPreviewRenderer
{
private const PREVIEW_TEMPLATE = 'EXT:weather2/Resources/Private/Templates/PluginPreview/Weather2.html';

private const ALLOWED_PLUGINS = [
'weather2_currentweather',
'weather2_weatheralert',
];

public function __construct(
protected FlexFormService $flexFormService,
protected ViewFactoryInterface $viewFactory
) {}

public function renderPageModulePreviewContent(GridColumnItem $item): string
{
$ttContentRecord = $item->getRecord();
if (!$this->isValidPlugin($ttContentRecord)) {
return '';
}

$view = $this->viewFactory->create(new ViewFactoryData(
templatePathAndFilename: self::PREVIEW_TEMPLATE
));
$view->assignMultiple($ttContentRecord);

$this->addPluginName($view, $ttContentRecord);

// Add data from column pi_flexform
$piFlexformData = $this->getPiFlexformData($ttContentRecord);
if ($piFlexformData !== []) {
$view->assign('pi_flexform_transformed', $piFlexformData);
}

return $view->render();
}

protected function isValidPlugin(array $ttContentRecord): bool
{
if (!isset($ttContentRecord['CType'])) {
return false;
}

if (!in_array($ttContentRecord['CType'], self::ALLOWED_PLUGINS, true)) {
return false;
}

return true;
}

protected function addPluginName(ViewInterface $view, array $ttContentRecord): void
{
$langKey = sprintf(
'plugin.%s.title',
str_replace('weather2_', '', $ttContentRecord['CType'])
);

$view->assign(
'pluginName',
LocalizationUtility::translate('LLL:EXT:weather2/Resources/Private/Language/locallang_db.xlf:' . $langKey)
);
}

protected function getPiFlexformData(array $ttContentRecord): array
{
$data = [];
if (!empty($ttContentRecord['pi_flexform'] ?? '')) {
$data = $this->flexFormService->convertFlexFormContentToArray($ttContentRecord['pi_flexform']);
}

return $data;
}
}
41 changes: 41 additions & 0 deletions Classes/Updates/PluginToContentElementUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package jweiland/weather2.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace JWeiland\Weather2\Updates;

use TYPO3\CMS\Install\Attribute\UpgradeWizard;
use TYPO3\CMS\Install\Updates\AbstractListTypeToCTypeUpdate;

/**
* With TYPO3 13 all plugins have to be declared as content elements (CType) insteadof "list_type"
*/
#[UpgradeWizard('weather2_migratePluginsToContentElementsUpdate')]
class PluginToContentElementUpdate extends AbstractListTypeToCTypeUpdate
{
protected function getListTypeToCTypeMapping(): array
{
return [
'weather2_currentweather' => 'weather2_currentweather',
'weather2_weatheralert' => 'weather2_weatheralert',
];
}

public function getTitle(): string
{
return 'Migrate plugins to Content Elements';
}

public function getDescription(): string
{
return 'The modern way to register plugins for TYPO3 is to register them as content element types. ' .
'Running this wizard will migrate all weather2 plugins to content element (CType)';
}
}
4 changes: 4 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ services:
JWeiland\Weather2\:
resource: '../Classes/*'

# Called by makeInstance in TYPO3's PluginPreview
JWeiland\Weather2\Backend\Preview\Weather2PluginPreview:
public: true

# Will be called by GeneralUtility in AjaxRoutes
JWeiland\Weather2\Ajax\DeutscherWetterdienstWarnCellSearch:
public: true
Expand Down
2 changes: 2 additions & 0 deletions Configuration/Sets/Weather2/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: jweiland/weather2
label: Weather2 Extension Settings
43 changes: 43 additions & 0 deletions Configuration/Sets/Weather2/settings.definitions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
categories:
Weather2:
label: 'Weather2'
Weather2.templates:
label: 'Templates'
parent: Weather2
Weather2.pages:
label: 'Pages'
parent: Weather2

settings:
weather2.templateRootPath:
label: 'Templates'
category: Weather2.templates
description: 'Path to template root'
type: string
default: 'EXT:weather2/Resources/Private/Templates/'
weather2.partialRootPath:
label: 'Partials'
category: Weather2.templates
description: 'Path to partial root'
type: string
default: 'EXT:weather2/Resources/Private/Partials/'
weather2.layoutRootPath:
label: 'Layouts'
category: Weather2.templates
description: 'Path to layout root'
type: string
default: 'EXT:weather2/Resources/Private/Layouts/'
weather2.iconsPath:
label: 'Icons Path'
category: Weather2.templates
description: 'Path for weather icons'
type: string
default: 'EXT:weather2/Resources/Public/WeatherIcons/'
weather2.storagePid:
label: 'Default storage PID'
category: Weather2.pages
description: 'Folder for storing weather records and fetched data'
type: int
default: 0


Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ plugin.tx_weather2 {
view {
templateRootPaths {
0 = EXT:weather2/Resources/Private/Templates/
1 = {$plugin.tx_weather2.view.templateRootPath}
1 = {$weather2.templateRootPath}
}

partialRootPaths {
0 = EXT:weather2/Resources/Private/Partials/
1 = {$plugin.tx_weather2.view.partialRootPath}
1 = {$weather2.partialRootPath}
}

layoutRootPaths {
0 = EXT:weather2/Resources/Private/Layouts/
1 = {$plugin.tx_weather2.view.layoutRootPath}
1 = {$weather2.layoutRootPath}
}
}

persistence {
storagePid = {$plugin.tx_weather2.persistence.storagePid}
storagePid = {$weather2.storagePid}
}

settings {
iconsPath = {$plugin.tx_weather2.iconsPath}
iconsPath = {$weather2.iconsPath}
}
}
11 changes: 0 additions & 11 deletions Configuration/TCA/Overrides/sys_template.php

This file was deleted.

32 changes: 28 additions & 4 deletions Configuration/TCA/Overrides/tt_content.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,53 @@
die('Access denied.');
}

use JWeiland\Weather2\Backend\Preview\Weather2PluginPreview;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Extbase\Utility\ExtensionUtility;

ExtensionUtility::registerPlugin(
'Weather2',
'Currentweather',
'LLL:EXT:weather2/Resources/Private/Language/locallang_db.xlf:plugin.currentweather.title',
'plugin-current-weather',
'plugins',
'LLL:EXT:weather2/Resources/Private/Language/locallang_db.xlf:plugin.currentweather.description'
);

ExtensionUtility::registerPlugin(
'Weather2',
'Weatheralert',
'LLL:EXT:weather2/Resources/Private/Language/locallang_db.xlf:plugin.weatheralert.title',
'plugin-weather-alert',
'plugins',
'LLL:EXT:weather2/Resources/Private/Language/locallang_db.xlf:plugin.weatheralert.description'
);

$GLOBALS['TCA']['tt_content']['types']['list']['subtypes_addlist']['weather2_currentweather'] = 'pi_flexform';
ExtensionManagementUtility::addPiFlexFormValue(
'weather2_currentweather',
'*',
'FILE:EXT:weather2/Configuration/FlexForms/flexform_currentweather.xml',
'weather2_currentweather',
);

ExtensionManagementUtility::addToAllTCAtypes(
'tt_content',
'--div--;Configuration,pi_flexform',
'weather2_currentweather',
'after:subheader'
);

$GLOBALS['TCA']['tt_content']['types']['list']['subtypes_addlist']['weather2_weatheralert'] = 'pi_flexform';
ExtensionManagementUtility::addPiFlexFormValue(
'weather2_weatheralert',
'*',
'FILE:EXT:weather2/Configuration/FlexForms/flexform_weatheralert.xml',
'weather2_weatheralert',
);

ExtensionManagementUtility::addToAllTCAtypes(
'tt_content',
'--div--;Configuration,pi_flexform',
'weather2_weatheralert',
'after:subheader'
);

$GLOBALS['TCA']['tt_content']['types']['weather2_currentweather']['previewRenderer'] = Weather2PluginPreview::class;
$GLOBALS['TCA']['tt_content']['types']['weather2_weatheralert']['previewRenderer'] = Weather2PluginPreview::class;
22 changes: 0 additions & 22 deletions Configuration/TSconfig/ContentElementWizard.tsconfig

This file was deleted.

18 changes: 0 additions & 18 deletions Configuration/TypoScript/constants.typoscript

This file was deleted.

1 change: 0 additions & 1 deletion Configuration/page.tsconfig

This file was deleted.

9 changes: 9 additions & 0 deletions Resources/Private/Language/locallang_db.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,18 @@
<trans-unit id="plugin.currentweather.title">
<source>Current Weather</source>
</trans-unit>
<trans-unit id="plugin.currentweather.description">
<source>Display the current weather conditions, including temperature, humidity, wind speed, and more, for a specific location.</source>
</trans-unit>
<trans-unit id="plugin.weatheralert.title">
<source>Weather Alerts</source>
</trans-unit>
<trans-unit id="plugin.weatheralert.description">
<source>Show real weather alerts and warnings, keeping users informed about severe weather conditions in their area.</source>
</trans-unit>


plugin.currentweather.descriptiom
</body>
</file>
</xliff>
Loading

0 comments on commit 33a070f

Please sign in to comment.