Skip to content

Commit

Permalink
[TASK] Dispatch event after extension install with site config
Browse files Browse the repository at this point in the history
An extension can provide site configuration to be imported into
the TYPO3 instance by providing a file in Initialisation/Site.
After the processing has been done, an event is dispatched now.

Resolves: #93687
Releases: master, 10.4
Change-Id: Iff932d3855ebfd6532f3a011d3a511015f1226db
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/68312
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Richard Haeser <richard@richardhaeser.com>
Reviewed-by: Richard Haeser <richard@richardhaeser.com>
  • Loading branch information
maddy2101 authored and haassie committed Mar 29, 2021
1 parent 936c9be commit c637d09
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace TYPO3\CMS\Extensionmanager\Event;

/**
* Event that is triggered after a package has imported its site configurations (from Initialisation/Site)
*/
final class AfterExtensionSiteFilesHaveBeenImportedEvent
{
/**
* @var string
*
* extension that imported the site configuration
*/
private $packageKey;

/**
* @var array list of site identifiers that were imported
*
* use as
* foreach ($siteIdentifierList as $siteIdentifier) {
* $configuration = $siteConfiguration->load($siteIdentifier);
* // do things
* }
*/
private $siteIdentifierList;

public function __construct(string $packageKey, array $newSiteIdentifierList)
{
$this->packageKey = $packageKey;
$this->siteIdentifierList = $newSiteIdentifierList;
}

public function getPackageKey(): string
{
return $this->packageKey;
}

/**
* @return array
*/
public function getSiteIdentifierList(): array
{
return $this->siteIdentifierList;
}
}
21 changes: 15 additions & 6 deletions typo3/sysext/extensionmanager/Classes/Utility/InstallUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository;
use TYPO3\CMS\Extensionmanager\Event\AfterExtensionDatabaseContentHasBeenImportedEvent;
use TYPO3\CMS\Extensionmanager\Event\AfterExtensionFilesHaveBeenImportedEvent;
use TYPO3\CMS\Extensionmanager\Event\AfterExtensionSiteFilesHaveBeenImportedEvent;
use TYPO3\CMS\Extensionmanager\Event\AfterExtensionStaticDatabaseContentHasBeenImportedEvent;
use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
use TYPO3\CMS\Impexp\Import;
Expand Down Expand Up @@ -218,7 +219,7 @@ public function processExtensionSetup(string $extensionKey): void
$this->importInitialFiles($extension['siteRelPath'] ?? '', $extensionKey);
$this->importStaticSqlFile($extensionKey, $extension['siteRelPath']);
$import = $this->importT3DFile($extensionKey, $extension['siteRelPath']);
$this->importSiteConfiguration($extension['siteRelPath'], $import);
$this->importSiteConfiguration($extensionKey, $extension['siteRelPath'], $import);
}

/**
Expand Down Expand Up @@ -580,10 +581,11 @@ protected function importInitialFiles($extensionSiteRelPath, $extensionKey)
}

/**
* @param string $extensionKey
* @param string $extensionSiteRelPath
* @param Import|null $import
*/
protected function importSiteConfiguration(string $extensionSiteRelPath, Import $import = null): void
protected function importSiteConfiguration(string $extensionKey, string $extensionSiteRelPath, Import $import = null): void
{
$importRelFolder = $extensionSiteRelPath . 'Initialisation/Site';
$importAbsFolder = Environment::getPublicPath() . '/' . $importRelFolder;
Expand Down Expand Up @@ -623,23 +625,30 @@ protected function importSiteConfiguration(string $extensionSiteRelPath, Import

/** @var Site[] $newSites */
$newSites = array_diff_key($siteConfiguration->resolveAllExistingSites(false), $existingSites);
$importedPages = $import->import_mapId['pages'] ?? null;
$importedPages = [];
if ($import instanceof Import && !empty($import->import_mapId['pages'])) {
$importedPages = $import->import_mapId['pages'];
}

$newSiteIdentifierList = [];
foreach ($newSites as $newSite) {
$exportedPageId = $newSite->getRootPageId();
$siteIdentifier = $newSite->getIdentifier();
$newSiteIdentifierList[] = $siteIdentifier;
$importedPageId = $importedPages[$exportedPageId] ?? null;
if ($importedPageId === null) {
$this->logger->warning(
sprintf(
'Imported site configuration with identifier %s could not be mapped to imported page id',
$newSite->getIdentifier()
$siteIdentifier
)
);
continue;
}
$configuration = $siteConfiguration->load($newSite->getIdentifier());
$configuration = $siteConfiguration->load($siteIdentifier);
$configuration['rootPageId'] = $importedPageId;
$siteConfiguration->write($newSite->getIdentifier(), $configuration);
$siteConfiguration->write($siteIdentifier, $configuration);
}
$this->eventDispatcher->dispatch(new AfterExtensionSiteFilesHaveBeenImportedEvent($extensionKey, $newSiteIdentifierList));
}
}

0 comments on commit c637d09

Please sign in to comment.