Skip to content

Commit

Permalink
Add new block positions automatically on theme installation fixes #4228
Browse files Browse the repository at this point in the history
… (#4519)
  • Loading branch information
craigh committed Oct 10, 2020
1 parent dd2ec23 commit 896e9e9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG-3.1.md
Expand Up @@ -18,7 +18,8 @@
- [extensions] Add StaticContent module to manage all static content (#4369).
- [CoreBundle] Add `Zikula\Bundle\CoreBundle\Configurator` for writing config files to the filesystem (#4433).
- [FormExtensionsBundle] Add bsCustomFileInput for direct file selection feedback (#4491).
- [ZikulaDefaultTheme] Add new default theme (#4462).
- [BlocksModule] Add new block positions automatically on theme installation (#4228).
- [DefaultTheme] Add new default theme (#4462).
- This looks the same as ZikulaBootstrapTheme but improves the templates in a way that is not BC.

- Deprecated:
Expand Down
@@ -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();
}
}

0 comments on commit 896e9e9

Please sign in to comment.