Skip to content

Commit

Permalink
IBX-7299: Added event emission to allow modification of content type …
Browse files Browse the repository at this point in the history
…list (#1054)
  • Loading branch information
mikadamczyk authored Jan 5, 2024
2 parents 4f10d2d + fff55f7 commit b114ef7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 13 deletions.
42 changes: 42 additions & 0 deletions src/lib/Event/FilterContentTypesEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\AdminUi\Event;

use Symfony\Contracts\EventDispatcher\Event;

/**
* @phpstan-import-type TContentTypeData from \Ibexa\AdminUi\UI\Config\Provider\ContentTypes
*/
final class FilterContentTypesEvent extends Event
{
/** @var array<string, array<TContentTypeData>> */
private array $contentTypeGroups;

/**
* @param array<string, array<TContentTypeData>> $contentTypeGroups
*/
public function __construct(
array $contentTypeGroups
) {
$this->contentTypeGroups = $contentTypeGroups;
}

/**
* @return array<string, array<TContentTypeData>>
*/
public function getContentTypeGroups(): array
{
return $this->contentTypeGroups;
}

public function removeContentTypeGroup(string $contentTypeGroupIdentifier): void
{
unset($this->contentTypeGroups[$contentTypeGroupIdentifier]);
}
}
53 changes: 40 additions & 13 deletions src/lib/UI/Config/Provider/ContentTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@
*/
namespace Ibexa\AdminUi\UI\Config\Provider;

use Ibexa\AdminUi\Event\FilterContentTypesEvent;
use Ibexa\AdminUi\UI\Service\ContentTypeIconResolver;
use Ibexa\Contracts\AdminUi\UI\Config\ProviderInterface;
use Ibexa\Contracts\Core\Repository\ContentTypeService;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @phpstan-type TContentTypeData array{
* id: int,
* identifier: string,
* name: string|null,
* isContainer: bool,
* thumbnail: string,
* href: string,
* }
*/
class ContentTypes implements ProviderInterface
{
/** @var \Ibexa\Contracts\Core\Repository\ContentTypeService */
Expand All @@ -27,6 +39,8 @@ class ContentTypes implements ProviderInterface
/** @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface */
private $urlGenerator;

private EventDispatcherInterface $eventDispatcher;

/**
* @param \Ibexa\Contracts\Core\Repository\ContentTypeService $contentTypeService
* @param \Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface $userLanguagePreferenceProvider
Expand All @@ -37,16 +51,18 @@ public function __construct(
ContentTypeService $contentTypeService,
UserLanguagePreferenceProviderInterface $userLanguagePreferenceProvider,
ContentTypeIconResolver $contentTypeIconResolver,
UrlGeneratorInterface $urlGenerator
UrlGeneratorInterface $urlGenerator,
EventDispatcherInterface $eventDispatcher
) {
$this->contentTypeService = $contentTypeService;
$this->userLanguagePreferenceProvider = $userLanguagePreferenceProvider;
$this->contentTypeIconResolver = $contentTypeIconResolver;
$this->urlGenerator = $urlGenerator;
$this->eventDispatcher = $eventDispatcher;
}

/**
* @return mixed Anything that is serializable via json_encode()
* @phpstan-return array<string, array<TContentTypeData>>
*/
public function getConfig()
{
Expand All @@ -67,20 +83,31 @@ public function getConfig()
});

foreach ($contentTypes as $contentType) {
$contentTypeGroups[$contentTypeGroup->identifier][] = [
'id' => $contentType->id,
'identifier' => $contentType->identifier,
'name' => $contentType->getName(),
'isContainer' => $contentType->isContainer,
'thumbnail' => $this->contentTypeIconResolver->getContentTypeIcon($contentType->identifier),
'href' => $this->urlGenerator->generate('ibexa.rest.load_content_type', [
'contentTypeId' => $contentType->id,
]),
];
$contentTypeGroups[$contentTypeGroup->identifier][] = $this->getContentTypeData($contentType);
}
}

return $contentTypeGroups;
/** @var \Ibexa\AdminUi\Event\FilterContentTypesEvent $event */
$event = $this->eventDispatcher->dispatch(new FilterContentTypesEvent($contentTypeGroups));

return $event->getContentTypeGroups();
}

/**
* @phpstan-return TContentTypeData
*/
private function getContentTypeData(ContentType $contentType): array
{
return [
'id' => $contentType->id,
'identifier' => $contentType->identifier,
'name' => $contentType->getName(),
'isContainer' => $contentType->isContainer,
'thumbnail' => $this->contentTypeIconResolver->getContentTypeIcon($contentType->identifier),
'href' => $this->urlGenerator->generate('ibexa.rest.load_content_type', [
'contentTypeId' => $contentType->id,
]),
];
}
}

Expand Down

0 comments on commit b114ef7

Please sign in to comment.