-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new block positions automatically on theme installation fixes #4228…
… (#4519)
- Loading branch information
Showing
2 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/system/BlocksModule/Listener/BlockPositionInstallerListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Zikula package. | ||
* | ||
* Copyright Zikula - https://ziku.la/ | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zikula\BlocksModule\Listener; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Zikula\BlocksModule\Entity\BlockPositionEntity; | ||
use Zikula\BlocksModule\Entity\RepositoryInterface\BlockPositionRepositoryInterface; | ||
use Zikula\ExtensionsModule\AbstractTheme; | ||
use Zikula\ExtensionsModule\Event\ExtensionPostInstallEvent; | ||
use Zikula\ExtensionsModule\Event\ExtensionStateEvent; | ||
|
||
/** | ||
* When a new theme is installed, add non-existing block positions. | ||
*/ | ||
class BlockPositionInstallerListener implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @var BlockPositionRepositoryInterface | ||
*/ | ||
private $blockPositionRepository; | ||
|
||
/** | ||
* @var ManagerRegistry | ||
*/ | ||
private $managerRegistry; | ||
|
||
public function __construct( | ||
BlockPositionRepositoryInterface $blockPositionRepository, | ||
ManagerRegistry $managerRegistry | ||
) { | ||
$this->blockPositionRepository = $blockPositionRepository; | ||
$this->managerRegistry = $managerRegistry; | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
ExtensionPostInstallEvent::class => [['createBlockPositions']], | ||
]; | ||
} | ||
|
||
public function createBlockPositions(ExtensionStateEvent $event): void | ||
{ | ||
$bundle = $event->getExtensionBundle(); | ||
if (!($bundle instanceof AbstractTheme)) { | ||
return; | ||
} | ||
$config = $bundle->getConfig(); | ||
$positions =[]; | ||
foreach ($config as $realm => $attributes) { | ||
if (!isset($attributes['block']) || !isset($attributes['block']['positions'])) { | ||
continue; | ||
} | ||
$positions = array_merge($positions, $attributes['block']['positions']); | ||
} | ||
|
||
foreach (array_keys($positions) as $name) { | ||
$existing = $this->blockPositionRepository->findByName($name); | ||
if (!empty($existing)) { | ||
continue; | ||
} | ||
$positionEntity = new BlockPositionEntity(); | ||
$positionEntity->setName($name); | ||
$positionEntity->setDescription($name . ' blocks'); | ||
$this->managerRegistry->getManager()->persist($positionEntity); | ||
} | ||
$this->managerRegistry->getManager()->flush(); | ||
} | ||
} |