From 8a30940605f4190a32c534b17984ab47ea333184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Mon, 14 Jan 2019 14:19:45 +0100 Subject: [PATCH 01/12] EZP-29998: As a developer, I want to define icons for content types --- .../Configuration/Parser/ContentType.php | 60 ++++++++++++++++ src/bundle/EzPlatformAdminUiBundle.php | 2 + .../config/services/ui_config/common.yml | 19 +++++ .../Twig/ContentTypeIconExtension.php | 69 +++++++++++++++++++ .../UI/Config/Provider/ContentTypeIcons.php | 41 +++++++++++ 5 files changed, 191 insertions(+) create mode 100644 src/bundle/DependencyInjection/Configuration/Parser/ContentType.php create mode 100644 src/bundle/Templating/Twig/ContentTypeIconExtension.php create mode 100644 src/lib/UI/Config/Provider/ContentTypeIcons.php diff --git a/src/bundle/DependencyInjection/Configuration/Parser/ContentType.php b/src/bundle/DependencyInjection/Configuration/Parser/ContentType.php new file mode 100644 index 0000000000..bd65b609ec --- /dev/null +++ b/src/bundle/DependencyInjection/Configuration/Parser/ContentType.php @@ -0,0 +1,60 @@ +setContextualParameter('content_type', $currentScope, $scopeSettings['content_type']); + } + + /** + * {@inheritdoc} + */ + public function addSemanticConfig(NodeBuilder $nodeBuilder) + { + $nodeBuilder + ->arrayNode('content_type') + ->useAttributeAsKey('identifier') + ->arrayPrototype() + ->children() + ->scalarNode('thumbnail')->end() + ->end() + ->end() + ->end(); + } +} diff --git a/src/bundle/EzPlatformAdminUiBundle.php b/src/bundle/EzPlatformAdminUiBundle.php index 443a4a73e6..b042c40fc7 100644 --- a/src/bundle/EzPlatformAdminUiBundle.php +++ b/src/bundle/EzPlatformAdminUiBundle.php @@ -16,6 +16,7 @@ use EzSystems\EzPlatformAdminUiBundle\DependencyInjection\Compiler\ViewBuilderRegistryPass; use EzSystems\EzPlatformAdminUiBundle\DependencyInjection\Configuration\Parser\AdminUiForms; use EzSystems\EzPlatformAdminUiBundle\DependencyInjection\Configuration\Parser\ContentTranslateView; +use EzSystems\EzPlatformAdminUiBundle\DependencyInjection\Configuration\Parser\ContentType; use EzSystems\EzPlatformAdminUiBundle\DependencyInjection\Configuration\Parser\LocationIds; use EzSystems\EzPlatformAdminUiBundle\DependencyInjection\Configuration\Parser\Module; use EzSystems\EzPlatformAdminUiBundle\DependencyInjection\Configuration\Parser\Notifications; @@ -83,6 +84,7 @@ private function getConfigParsers(): array new Notifications(), new ContentTranslateView(), new AdminUiForms(), + new ContentType(), ]; } } diff --git a/src/bundle/Resources/config/services/ui_config/common.yml b/src/bundle/Resources/config/services/ui_config/common.yml index e79eebfc6f..7d50e99c89 100644 --- a/src/bundle/Resources/config/services/ui_config/common.yml +++ b/src/bundle/Resources/config/services/ui_config/common.yml @@ -1,3 +1,7 @@ +parameters: + # default content type icon + ezsystems.ezplatform_admin_ui.default_content_type_thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#file' + services: _defaults: autowire: true @@ -59,6 +63,17 @@ services: tags: - { name: ezplatform.admin_ui.config_provider, key: 'contentTypeNames' } + EzSystems\EzPlatformAdminUi\UI\Config\Provider\ContentTypeIcons: + tags: + - { name: ezplatform.admin_ui.config_provider, key: 'contentTypeIcons' } + + ezsystems.ezplatform_admin_ui.ui.config.provider.default_content_type_icon: + class: EzSystems\EzPlatformAdminUi\UI\Config\Provider\Value + arguments: + $value: '%ezsystems.ezplatform_admin_ui.default_content_type_thumbnail%' + tags: + - { name: ezplatform.admin_ui.config_provider, key: 'defaultContentTypeIcon' } + EzSystems\EzPlatformAdminUi\UI\Config\Provider\Module\UniversalDiscoveryWidget: tags: - { name: ezplatform.admin_ui.config_provider, key: 'universalDiscoveryWidget' } @@ -97,3 +112,7 @@ services: - { name: ezplatform.admin_ui.config_provider, key: 'dateFormat' } EzSystems\EzPlatformAdminUiBundle\Templating\Twig\PathStringExtension: ~ + + EzSystems\EzPlatformAdminUiBundle\Templating\Twig\ContentTypeIconExtension: + arguments: + $defaultThumbnail: '%ezsystems.ezplatform_admin_ui.default_content_type_thumbnail%' diff --git a/src/bundle/Templating/Twig/ContentTypeIconExtension.php b/src/bundle/Templating/Twig/ContentTypeIconExtension.php new file mode 100644 index 0000000000..ea6528bfa6 --- /dev/null +++ b/src/bundle/Templating/Twig/ContentTypeIconExtension.php @@ -0,0 +1,69 @@ +configResolver = $configResolver; + $this->defaultThumbnail = $defaultThumbnail; + } + + /** + * {@inheritdoc} + */ + public function getFunctions(): array + { + return [ + new Twig_SimpleFunction( + 'ezplatform_admin_ui_content_type_icon', + [$this, 'getContentTypeIcon'], + [ + 'is_safe' => ['html'], + ] + ), + ]; + } + + /** + * Returns path to content type icon. + * + * Path is resolved based on configuration (ezpublish.system..content_type). If there isn't coresponding + * entry for given content type, then path to default icon will be returned. + * + * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType|string $contentType + * + * @return null|string + */ + public function getContentTypeIcon($contentType): ?string + { + if ($contentType instanceof ContentType) { + $contentType = $contentType->identifier; + } + + $config = $this->configResolver->getParameter('content_type'); + if (isset($config[$contentType])) { + return $config[$contentType]['thumbnail']; + } + + return $this->defaultThumbnail; + } +} diff --git a/src/lib/UI/Config/Provider/ContentTypeIcons.php b/src/lib/UI/Config/Provider/ContentTypeIcons.php new file mode 100644 index 0000000000..b284685110 --- /dev/null +++ b/src/lib/UI/Config/Provider/ContentTypeIcons.php @@ -0,0 +1,41 @@ +configResolver = $configResolver; + } + + /** + * {@inheritdoc} + */ + public function getConfig(): array + { + $config = []; + + $contentTypes = $this->configResolver->getParameter('content_type'); + foreach ($contentTypes as $identifier => $contentType) { + $config[$identifier] = $contentType['thumbnail']; + } + + return $config; + } +} From 13e7bb3e5ee31008ab3bbb262a198a19392a5a97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Wed, 16 Jan 2019 14:13:53 +0100 Subject: [PATCH 02/12] fixup! EZP-29998: As a developer, I want to define icons for content types --- .../Resources/config/default_parameters.yml | 14 +++++ .../config/services/ui_config/common.yml | 15 +---- .../Twig/ContentTypeIconExtension.php | 44 +++------------ .../UI/Config/Provider/ContentTypeIcons.php | 41 -------------- src/lib/UI/Config/Provider/ContentTypes.php | 14 ++++- .../UI/Service/ContentTypeIconResolver.php | 55 +++++++++++++++++++ 6 files changed, 93 insertions(+), 90 deletions(-) delete mode 100644 src/lib/UI/Config/Provider/ContentTypeIcons.php create mode 100644 src/lib/UI/Service/ContentTypeIconResolver.php diff --git a/src/bundle/Resources/config/default_parameters.yml b/src/bundle/Resources/config/default_parameters.yml index 23f855c766..b1b64e9f97 100644 --- a/src/bundle/Resources/config/default_parameters.yml +++ b/src/bundle/Resources/config/default_parameters.yml @@ -26,6 +26,20 @@ parameters: match: SystemInfo\Identifier: 'symfony_kernel' + ezsettings.default.content_type: + article: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#article' + folder: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#folder' + user: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#user' + user_group: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#user_group' + file: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#file' + image: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#image' + ezplatform.content_view.tabs.default_template: '@@ezdesign/parts/tab/default.html.twig' ezplatform.multifile_upload.location.mappings: [] diff --git a/src/bundle/Resources/config/services/ui_config/common.yml b/src/bundle/Resources/config/services/ui_config/common.yml index 7d50e99c89..c8c64c4246 100644 --- a/src/bundle/Resources/config/services/ui_config/common.yml +++ b/src/bundle/Resources/config/services/ui_config/common.yml @@ -63,17 +63,6 @@ services: tags: - { name: ezplatform.admin_ui.config_provider, key: 'contentTypeNames' } - EzSystems\EzPlatformAdminUi\UI\Config\Provider\ContentTypeIcons: - tags: - - { name: ezplatform.admin_ui.config_provider, key: 'contentTypeIcons' } - - ezsystems.ezplatform_admin_ui.ui.config.provider.default_content_type_icon: - class: EzSystems\EzPlatformAdminUi\UI\Config\Provider\Value - arguments: - $value: '%ezsystems.ezplatform_admin_ui.default_content_type_thumbnail%' - tags: - - { name: ezplatform.admin_ui.config_provider, key: 'defaultContentTypeIcon' } - EzSystems\EzPlatformAdminUi\UI\Config\Provider\Module\UniversalDiscoveryWidget: tags: - { name: ezplatform.admin_ui.config_provider, key: 'universalDiscoveryWidget' } @@ -113,6 +102,8 @@ services: EzSystems\EzPlatformAdminUiBundle\Templating\Twig\PathStringExtension: ~ - EzSystems\EzPlatformAdminUiBundle\Templating\Twig\ContentTypeIconExtension: + EzSystems\EzPlatformAdminUiBundle\Templating\Twig\ContentTypeIconExtension: ~ + + EzSystems\EzPlatformAdminUi\UI\Service\ContentTypeIconResolver: arguments: $defaultThumbnail: '%ezsystems.ezplatform_admin_ui.default_content_type_thumbnail%' diff --git a/src/bundle/Templating/Twig/ContentTypeIconExtension.php b/src/bundle/Templating/Twig/ContentTypeIconExtension.php index ea6528bfa6..06d44dc950 100644 --- a/src/bundle/Templating/Twig/ContentTypeIconExtension.php +++ b/src/bundle/Templating/Twig/ContentTypeIconExtension.php @@ -8,23 +8,21 @@ namespace EzSystems\EzPlatformAdminUiBundle\Templating\Twig; -use eZ\Publish\API\Repository\Values\ContentType\ContentType; -use eZ\Publish\Core\MVC\ConfigResolverInterface; +use EzSystems\EzPlatformAdminUi\UI\Service\ContentTypeIconResolver; use Twig_Extension; use Twig_SimpleFunction; class ContentTypeIconExtension extends Twig_Extension { - /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */ - private $configResolver; + /** @var \EzSystems\EzPlatformAdminUi\UI\Service\ContentTypeIconResolver */ + private $contentTypeIconResolver; - /** @var string|null */ - private $defaultThumbnail; - - public function __construct(ConfigResolverInterface $configResolver, string $defaultThumbnail = null) + /** + * @param \EzSystems\EzPlatformAdminUi\UI\Service\ContentTypeIconResolver $contentTypeIconResolver + */ + public function __construct(ContentTypeIconResolver $contentTypeIconResolver) { - $this->configResolver = $configResolver; - $this->defaultThumbnail = $defaultThumbnail; + $this->contentTypeIconResolver = $contentTypeIconResolver; } /** @@ -35,35 +33,11 @@ public function getFunctions(): array return [ new Twig_SimpleFunction( 'ezplatform_admin_ui_content_type_icon', - [$this, 'getContentTypeIcon'], + [$this->contentTypeIconResolver, 'getContentTypeIcon'], [ 'is_safe' => ['html'], ] ), ]; } - - /** - * Returns path to content type icon. - * - * Path is resolved based on configuration (ezpublish.system..content_type). If there isn't coresponding - * entry for given content type, then path to default icon will be returned. - * - * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType|string $contentType - * - * @return null|string - */ - public function getContentTypeIcon($contentType): ?string - { - if ($contentType instanceof ContentType) { - $contentType = $contentType->identifier; - } - - $config = $this->configResolver->getParameter('content_type'); - if (isset($config[$contentType])) { - return $config[$contentType]['thumbnail']; - } - - return $this->defaultThumbnail; - } } diff --git a/src/lib/UI/Config/Provider/ContentTypeIcons.php b/src/lib/UI/Config/Provider/ContentTypeIcons.php deleted file mode 100644 index b284685110..0000000000 --- a/src/lib/UI/Config/Provider/ContentTypeIcons.php +++ /dev/null @@ -1,41 +0,0 @@ -configResolver = $configResolver; - } - - /** - * {@inheritdoc} - */ - public function getConfig(): array - { - $config = []; - - $contentTypes = $this->configResolver->getParameter('content_type'); - foreach ($contentTypes as $identifier => $contentType) { - $config[$identifier] = $contentType['thumbnail']; - } - - return $config; - } -} diff --git a/src/lib/UI/Config/Provider/ContentTypes.php b/src/lib/UI/Config/Provider/ContentTypes.php index b5cc817801..4b44f4f779 100644 --- a/src/lib/UI/Config/Provider/ContentTypes.php +++ b/src/lib/UI/Config/Provider/ContentTypes.php @@ -9,6 +9,7 @@ use eZ\Publish\API\Repository\ContentTypeService; use eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface; use EzSystems\EzPlatformAdminUi\UI\Config\ProviderInterface; +use EzSystems\EzPlatformAdminUi\UI\Service\ContentTypeIconResolver; class ContentTypes implements ProviderInterface { @@ -18,14 +19,22 @@ class ContentTypes implements ProviderInterface /** @var \eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface */ private $userLanguagePreferenceProvider; + /** @var \EzSystems\EzPlatformAdminUi\UI\Service\ContentTypeIconResolver */ + private $contentTypeIconResolver; + /** * @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService * @param \eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface $userLanguagePreferenceProvider + * @param \EzSystems\EzPlatformAdminUi\UI\Service\ContentTypeIconResolver $contentTypeIconResolver */ - public function __construct(ContentTypeService $contentTypeService, UserLanguagePreferenceProviderInterface $userLanguagePreferenceProvider) - { + public function __construct( + ContentTypeService $contentTypeService, + UserLanguagePreferenceProviderInterface $userLanguagePreferenceProvider, + ContentTypeIconResolver $contentTypeIconResolver + ) { $this->contentTypeService = $contentTypeService; $this->userLanguagePreferenceProvider = $userLanguagePreferenceProvider; + $this->contentTypeIconResolver = $contentTypeIconResolver; } /** @@ -48,6 +57,7 @@ public function getConfig() $contentTypeGroups[$contentTypeGroup->identifier][] = [ 'identifier' => $contentType->identifier, 'name' => $contentType->getName(), + 'thumbnail' => $this->contentTypeIconResolver->getContentTypeIcon($contentType), ]; } } diff --git a/src/lib/UI/Service/ContentTypeIconResolver.php b/src/lib/UI/Service/ContentTypeIconResolver.php new file mode 100644 index 0000000000..a81dd672c3 --- /dev/null +++ b/src/lib/UI/Service/ContentTypeIconResolver.php @@ -0,0 +1,55 @@ +configResolver = $configResolver; + $this->defaultThumbnail = $defaultThumbnail; + } + + /** + * Returns path to content type icon. + * + * Path is resolved based on configuration (ezpublish.system..content_type). If there isn't coresponding + * entry for given content type, then path to default icon will be returned. + * + * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType|string $contentType + * + * @return string|null + */ + public function getContentTypeIcon($contentType): ?string + { + if ($contentType instanceof ContentType) { + $contentType = $contentType->identifier; + } + + $config = $this->configResolver->getParameter('content_type'); + if (isset($config[$contentType])) { + return $config[$contentType]['thumbnail']; + } + + return $this->defaultThumbnail; + } +} From 5c0c9dd0212bfface60b94274e5ec99e8110e54b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Wed, 16 Jan 2019 15:34:54 +0100 Subject: [PATCH 03/12] fixup! EZP-29998: As a developer, I want to define icons for content types --- .../Configuration/Parser/ContentType.php | 2 +- src/bundle/Resources/config/services.yml | 8 ++++++++ src/bundle/Resources/config/services/ui_config/common.yml | 8 -------- src/lib/UI/Service/ContentTypeIconResolver.php | 7 ++++--- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/bundle/DependencyInjection/Configuration/Parser/ContentType.php b/src/bundle/DependencyInjection/Configuration/Parser/ContentType.php index bd65b609ec..171a03116d 100644 --- a/src/bundle/DependencyInjection/Configuration/Parser/ContentType.php +++ b/src/bundle/DependencyInjection/Configuration/Parser/ContentType.php @@ -52,7 +52,7 @@ public function addSemanticConfig(NodeBuilder $nodeBuilder) ->useAttributeAsKey('identifier') ->arrayPrototype() ->children() - ->scalarNode('thumbnail')->end() + ->scalarNode('thumbnail')->defaultNull()->end() ->end() ->end() ->end(); diff --git a/src/bundle/Resources/config/services.yml b/src/bundle/Resources/config/services.yml index a6a552b55a..1398b956c8 100644 --- a/src/bundle/Resources/config/services.yml +++ b/src/bundle/Resources/config/services.yml @@ -21,6 +21,10 @@ imports: - { resource: services/permissions.yml } - { resource: services/forms.yml } +parameters: + # default content type icon + ezsystems.ezplatform_admin_ui.default_content_type_thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#file' + services: _defaults: autowire: true @@ -126,3 +130,7 @@ services: - {name: kernel.event_subscriber, priority: -250} EzSystems\EzPlatformAdminUiBundle\Templating\Twig\UserPreferencesGlobalExtension: ~ + + EzSystems\EzPlatformAdminUi\UI\Service\ContentTypeIconResolver: + arguments: + $defaultThumbnail: '%ezsystems.ezplatform_admin_ui.default_content_type_thumbnail%' diff --git a/src/bundle/Resources/config/services/ui_config/common.yml b/src/bundle/Resources/config/services/ui_config/common.yml index c8c64c4246..20eabeca1f 100644 --- a/src/bundle/Resources/config/services/ui_config/common.yml +++ b/src/bundle/Resources/config/services/ui_config/common.yml @@ -1,7 +1,3 @@ -parameters: - # default content type icon - ezsystems.ezplatform_admin_ui.default_content_type_thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#file' - services: _defaults: autowire: true @@ -103,7 +99,3 @@ services: EzSystems\EzPlatformAdminUiBundle\Templating\Twig\PathStringExtension: ~ EzSystems\EzPlatformAdminUiBundle\Templating\Twig\ContentTypeIconExtension: ~ - - EzSystems\EzPlatformAdminUi\UI\Service\ContentTypeIconResolver: - arguments: - $defaultThumbnail: '%ezsystems.ezplatform_admin_ui.default_content_type_thumbnail%' diff --git a/src/lib/UI/Service/ContentTypeIconResolver.php b/src/lib/UI/Service/ContentTypeIconResolver.php index a81dd672c3..8b82e26711 100644 --- a/src/lib/UI/Service/ContentTypeIconResolver.php +++ b/src/lib/UI/Service/ContentTypeIconResolver.php @@ -11,7 +11,7 @@ use eZ\Publish\API\Repository\Values\ContentType\ContentType; use eZ\Publish\Core\MVC\ConfigResolverInterface; -final class ContentTypeIconResolver +class ContentTypeIconResolver { /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */ private $configResolver; @@ -23,7 +23,7 @@ final class ContentTypeIconResolver * @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver * @param string|null $defaultThumbnail */ - public function __construct(ConfigResolverInterface $configResolver, ?string $defaultThumbnail = null) + public function __construct(ConfigResolverInterface $configResolver, string $defaultThumbnail) { $this->configResolver = $configResolver; $this->defaultThumbnail = $defaultThumbnail; @@ -46,7 +46,8 @@ public function getContentTypeIcon($contentType): ?string } $config = $this->configResolver->getParameter('content_type'); - if (isset($config[$contentType])) { + + if (isset($config[$contentType]) && !empty($config[$contentType]['thumbnail'])) { return $config[$contentType]['thumbnail']; } From 3aa62090afbaac97a7bc4d00a8bf69198dd80c02 Mon Sep 17 00:00:00 2001 From: Jakub Brzegowski Date: Thu, 17 Jan 2019 09:51:53 +0100 Subject: [PATCH 04/12] Add JS helper function returning content type icon href. --- .../helpers/content.type.icon.helper.js | 42 +++++++++++++++++++ src/bundle/Resources/views/layout.html.twig | 1 + 2 files changed, 43 insertions(+) create mode 100644 src/bundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js diff --git a/src/bundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js b/src/bundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js new file mode 100644 index 0000000000..47fc8b6991 --- /dev/null +++ b/src/bundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js @@ -0,0 +1,42 @@ +(function(global, doc, eZ) { + let contentTypesDataMap = null; + + /** + * Creates map with content types identifiers as keys for faster lookup + * + * @returns {Object} contentTypesDataMap + */ + const createContentTypeDataMap = () => + Object.values(eZ.adminUiConfig.contentTypes).reduce((contentTypeDataMap, contentTypeGroup) => { + for (const contentTypeData of contentTypeGroup) { + contentTypeDataMap[contentTypeData.identifier] = contentTypeData; + } + + return contentTypeDataMap; + }, {}); + + /** + * Returns href to content type icon + * + * @function showNotification + * @param {String} contentTypeIdentifier + * @returns {String|null} href to icon + */ + const getContentTypeIcon = (contentTypeIdentifier) => { + if (!contentTypeIdentifier) { + null; + } + + if (!contentTypesDataMap) { + contentTypesDataMap = createContentTypeDataMap(); + } + + const iconHref = contentTypesDataMap[contentTypeIdentifier].thumbnail; + + return iconHref; + }; + + eZ.addConfig('helpers.contentType', { + getContentTypeIcon, + }); +})(window, document, window.eZ); diff --git a/src/bundle/Resources/views/layout.html.twig b/src/bundle/Resources/views/layout.html.twig index a18a8758d0..cbebfbd948 100644 --- a/src/bundle/Resources/views/layout.html.twig +++ b/src/bundle/Resources/views/layout.html.twig @@ -162,6 +162,7 @@ '@EzPlatformAdminUiBundle/Resources/public/js/scripts/helpers/request.helper.js' '@EzPlatformAdminUiBundle/Resources/public/js/scripts/helpers/notification.helper.js' '@EzPlatformAdminUiBundle/Resources/public/js/scripts/helpers/timezone.helper.js' + '@EzPlatformAdminUiBundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js' '@EzPlatformAdminUiBundle/Resources/public/js/scripts/admin.notifications.js' '@EzPlatformAdminUiModulesBundle/Resources/public/js/UniversalDiscovery.module.js' '@EzPlatformAdminUiBundle/Resources/public/js/scripts/button.trigger.js' From 6263aa5634557467000e2d98652455cfaf046d8a Mon Sep 17 00:00:00 2001 From: Jakub Brzegowski Date: Mon, 21 Jan 2019 09:53:59 +0100 Subject: [PATCH 05/12] Fix jsDoc comment --- .../public/js/scripts/helpers/content.type.icon.helper.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js b/src/bundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js index 47fc8b6991..2b818db200 100644 --- a/src/bundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js +++ b/src/bundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js @@ -3,7 +3,8 @@ /** * Creates map with content types identifiers as keys for faster lookup - * + * + * @function createContentTypeDataMap * @returns {Object} contentTypesDataMap */ const createContentTypeDataMap = () => @@ -18,7 +19,7 @@ /** * Returns href to content type icon * - * @function showNotification + * @function getContentTypeIcon * @param {String} contentTypeIdentifier * @returns {String|null} href to icon */ From d3e978e6ca06e120e77f656c033df66ca80a5004 Mon Sep 17 00:00:00 2001 From: Jakub Brzegowski Date: Mon, 21 Jan 2019 11:55:23 +0100 Subject: [PATCH 06/12] Add default content_type icons for in-house Content Types --- .../Resources/config/default_parameters.yml | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/bundle/Resources/config/default_parameters.yml b/src/bundle/Resources/config/default_parameters.yml index b1b64e9f97..cc13a42b46 100644 --- a/src/bundle/Resources/config/default_parameters.yml +++ b/src/bundle/Resources/config/default_parameters.yml @@ -27,18 +27,38 @@ parameters: SystemInfo\Identifier: 'symfony_kernel' ezsettings.default.content_type: + about: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#about' article: thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#article' + blog: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#blog' + blog_post: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#blog_post' folder: thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#folder' + form: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#form' + place: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#place' + product: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#product' + field: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#field' user: thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#user' user_group: thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#user_group' file: thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#file' + gallery: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#gallery' image: thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#image' + video: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#video' + landing_page: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#landing_page' ezplatform.content_view.tabs.default_template: '@@ezdesign/parts/tab/default.html.twig' From 92be400e93b38834d97a70a830bcd7b27bc25e59 Mon Sep 17 00:00:00 2001 From: Jakub Brzegowski Date: Mon, 21 Jan 2019 16:07:50 +0100 Subject: [PATCH 07/12] Fix content type icon helper --- .../public/js/scripts/helpers/content.type.icon.helper.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/bundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js b/src/bundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js index 2b818db200..780fb8bd71 100644 --- a/src/bundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js +++ b/src/bundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js @@ -24,14 +24,14 @@ * @returns {String|null} href to icon */ const getContentTypeIcon = (contentTypeIdentifier) => { - if (!contentTypeIdentifier) { - null; - } - if (!contentTypesDataMap) { contentTypesDataMap = createContentTypeDataMap(); } + if (!contentTypeIdentifier || !contentTypesDataMap[contentTypeIdentifier]) { + return null; + } + const iconHref = contentTypesDataMap[contentTypeIdentifier].thumbnail; return iconHref; From a26f9e8dd6d2e6e67041875f2a6f9633e1a1dd43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Mon, 21 Jan 2019 16:17:23 +0100 Subject: [PATCH 08/12] fixup! EZP-29998: As a developer, I want to define icons for content types --- .../Configuration/Parser/ContentType.php | 4 +- .../Resources/config/default_parameters.yml | 65 +++++++++---------- .../Twig/ContentTypeIconExtension.php | 2 +- .../UI/Service/ContentTypeIconResolver.php | 38 +++++++++-- 4 files changed, 69 insertions(+), 40 deletions(-) diff --git a/src/bundle/DependencyInjection/Configuration/Parser/ContentType.php b/src/bundle/DependencyInjection/Configuration/Parser/ContentType.php index 171a03116d..3881ca1a95 100644 --- a/src/bundle/DependencyInjection/Configuration/Parser/ContentType.php +++ b/src/bundle/DependencyInjection/Configuration/Parser/ContentType.php @@ -39,7 +39,9 @@ public function mapConfig(array &$scopeSettings, $currentScope, ContextualizerIn return; } - $contextualizer->setContextualParameter('content_type', $currentScope, $scopeSettings['content_type']); + foreach ($scopeSettings['content_type'] as $identifier => $config) { + $contextualizer->setContextualParameter("content_type.$identifier", $currentScope, $config); + } } /** diff --git a/src/bundle/Resources/config/default_parameters.yml b/src/bundle/Resources/config/default_parameters.yml index cc13a42b46..ddb46e3087 100644 --- a/src/bundle/Resources/config/default_parameters.yml +++ b/src/bundle/Resources/config/default_parameters.yml @@ -26,39 +26,38 @@ parameters: match: SystemInfo\Identifier: 'symfony_kernel' - ezsettings.default.content_type: - about: - thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#about' - article: - thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#article' - blog: - thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#blog' - blog_post: - thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#blog_post' - folder: - thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#folder' - form: - thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#form' - place: - thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#place' - product: - thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#product' - field: - thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#field' - user: - thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#user' - user_group: - thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#user_group' - file: - thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#file' - gallery: - thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#gallery' - image: - thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#image' - video: - thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#video' - landing_page: - thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#landing_page' + ezsettings.default.content_type.about: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#about' + ezsettings.default.content_type.article: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#article' + ezsettings.default.content_type.blog: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#blog' + ezsettings.default.content_type.blog_post: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#blog_post' + ezsettings.default.content_type.folder: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#folder' + ezsettings.default.content_type.form: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#form' + ezsettings.default.content_type.place: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#place' + ezsettings.default.content_type.product: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#product' + ezsettings.default.content_type.field: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#field' + ezsettings.default.content_type.user: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#user' + ezsettings.default.content_type.user_group: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#user_group' + ezsettings.default.content_type.file: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#file' + ezsettings.default.content_type.gallery: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#gallery' + ezsettings.default.content_type.image: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#image' + ezsettings.default.content_type.video: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#video' + ezsettings.default.content_type.landing_page: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#landing_page' ezplatform.content_view.tabs.default_template: '@@ezdesign/parts/tab/default.html.twig' diff --git a/src/bundle/Templating/Twig/ContentTypeIconExtension.php b/src/bundle/Templating/Twig/ContentTypeIconExtension.php index 06d44dc950..6977a5844b 100644 --- a/src/bundle/Templating/Twig/ContentTypeIconExtension.php +++ b/src/bundle/Templating/Twig/ContentTypeIconExtension.php @@ -32,7 +32,7 @@ public function getFunctions(): array { return [ new Twig_SimpleFunction( - 'ezplatform_admin_ui_content_type_icon', + 'ez_content_type_icon', [$this->contentTypeIconResolver, 'getContentTypeIcon'], [ 'is_safe' => ['html'], diff --git a/src/lib/UI/Service/ContentTypeIconResolver.php b/src/lib/UI/Service/ContentTypeIconResolver.php index 8b82e26711..8d973f9729 100644 --- a/src/lib/UI/Service/ContentTypeIconResolver.php +++ b/src/lib/UI/Service/ContentTypeIconResolver.php @@ -10,22 +10,28 @@ use eZ\Publish\API\Repository\Values\ContentType\ContentType; use eZ\Publish\Core\MVC\ConfigResolverInterface; +use Symfony\Component\Asset\Packages; class ContentTypeIconResolver { /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */ private $configResolver; + /** @var \Symfony\Component\Asset\Packages */ + private $packages; + /** @var string|null */ private $defaultThumbnail; /** * @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver + * @param \Symfony\Component\Asset\Packages $packages * @param string|null $defaultThumbnail */ - public function __construct(ConfigResolverInterface $configResolver, string $defaultThumbnail) + public function __construct(ConfigResolverInterface $configResolver, Packages $packages, string $defaultThumbnail) { $this->configResolver = $configResolver; + $this->packages = $packages; $this->defaultThumbnail = $defaultThumbnail; } @@ -45,12 +51,34 @@ public function getContentTypeIcon($contentType): ?string $contentType = $contentType->identifier; } - $config = $this->configResolver->getParameter('content_type'); + $thumbnail = null; + + $parameterName = $this->getConfigParameterName($contentType); + if ($this->configResolver->hasParameter($parameterName)) { + $thumbnail = $this->configResolver->getParameter($parameterName)['thumbnail']; + } + + if (empty($thumbnail)) { + $thumbnail = $this->defaultThumbnail; + } - if (isset($config[$contentType]) && !empty($config[$contentType]['thumbnail'])) { - return $config[$contentType]['thumbnail']; + $fragment = null; + if (strpos($thumbnail, '#') !== false) { + list($thumbnail, $fragment) = explode('#', $thumbnail); } - return $this->defaultThumbnail; + return $this->packages->getUrl($thumbnail) . ($fragment ? '#' . $fragment : ''); + } + + /** + * Return configuration parameter name for given content type identifier. + * + * @param string $identifier + * + * @return string + */ + private function getConfigParameterName(string $identifier): string + { + return "content_type.$identifier"; } } From c10c434f626e0ad965d58656ad8f971e051ed219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Fri, 25 Jan 2019 13:44:13 +0100 Subject: [PATCH 09/12] Applied CR suggestions --- src/lib/UI/Config/Provider/ContentTypes.php | 2 +- src/lib/UI/Service/ContentTypeIconResolver.php | 11 +++-------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/lib/UI/Config/Provider/ContentTypes.php b/src/lib/UI/Config/Provider/ContentTypes.php index 4b44f4f779..ff4407e5b2 100644 --- a/src/lib/UI/Config/Provider/ContentTypes.php +++ b/src/lib/UI/Config/Provider/ContentTypes.php @@ -57,7 +57,7 @@ public function getConfig() $contentTypeGroups[$contentTypeGroup->identifier][] = [ 'identifier' => $contentType->identifier, 'name' => $contentType->getName(), - 'thumbnail' => $this->contentTypeIconResolver->getContentTypeIcon($contentType), + 'thumbnail' => $this->contentTypeIconResolver->getContentTypeIcon($contentType->identifier), ]; } } diff --git a/src/lib/UI/Service/ContentTypeIconResolver.php b/src/lib/UI/Service/ContentTypeIconResolver.php index 8d973f9729..5bc98b51be 100644 --- a/src/lib/UI/Service/ContentTypeIconResolver.php +++ b/src/lib/UI/Service/ContentTypeIconResolver.php @@ -8,7 +8,6 @@ namespace EzSystems\EzPlatformAdminUi\UI\Service; -use eZ\Publish\API\Repository\Values\ContentType\ContentType; use eZ\Publish\Core\MVC\ConfigResolverInterface; use Symfony\Component\Asset\Packages; @@ -28,7 +27,7 @@ class ContentTypeIconResolver * @param \Symfony\Component\Asset\Packages $packages * @param string|null $defaultThumbnail */ - public function __construct(ConfigResolverInterface $configResolver, Packages $packages, string $defaultThumbnail) + public function __construct(ConfigResolverInterface $configResolver, Packages $packages, ?string $defaultThumbnail) { $this->configResolver = $configResolver; $this->packages = $packages; @@ -41,16 +40,12 @@ public function __construct(ConfigResolverInterface $configResolver, Packages $p * Path is resolved based on configuration (ezpublish.system..content_type). If there isn't coresponding * entry for given content type, then path to default icon will be returned. * - * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType|string $contentType + * @param string $contentType * * @return string|null */ - public function getContentTypeIcon($contentType): ?string + public function getContentTypeIcon(string $contentType): ?string { - if ($contentType instanceof ContentType) { - $contentType = $contentType->identifier; - } - $thumbnail = null; $parameterName = $this->getConfigParameterName($contentType); From 4ed6a8cee931302ce38763de90e0e6ba973bb52d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Fri, 25 Jan 2019 14:33:19 +0100 Subject: [PATCH 10/12] fixup! Applied CR suggestions --- src/lib/UI/Service/ContentTypeIconResolver.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/UI/Service/ContentTypeIconResolver.php b/src/lib/UI/Service/ContentTypeIconResolver.php index 5bc98b51be..7c71db8c34 100644 --- a/src/lib/UI/Service/ContentTypeIconResolver.php +++ b/src/lib/UI/Service/ContentTypeIconResolver.php @@ -40,15 +40,15 @@ public function __construct(ConfigResolverInterface $configResolver, Packages $p * Path is resolved based on configuration (ezpublish.system..content_type). If there isn't coresponding * entry for given content type, then path to default icon will be returned. * - * @param string $contentType + * @param string $identifier * * @return string|null */ - public function getContentTypeIcon(string $contentType): ?string + public function getContentTypeIcon(string $identifier): ?string { $thumbnail = null; - $parameterName = $this->getConfigParameterName($contentType); + $parameterName = $this->getConfigParameterName($identifier); if ($this->configResolver->hasParameter($parameterName)) { $thumbnail = $this->configResolver->getParameter($parameterName)['thumbnail']; } From 288f06e086eaa0ed84855374d85d10dd014d8a62 Mon Sep 17 00:00:00 2001 From: Jakub Brzegowski Date: Fri, 25 Jan 2019 16:36:45 +0100 Subject: [PATCH 11/12] Fix JS - CR --- ...nt.type.icon.helper.js => content.type.helper.js} | 12 ++++++------ src/bundle/Resources/views/layout.html.twig | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) rename src/bundle/Resources/public/js/scripts/helpers/{content.type.icon.helper.js => content.type.helper.js} (78%) diff --git a/src/bundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js b/src/bundle/Resources/public/js/scripts/helpers/content.type.helper.js similarity index 78% rename from src/bundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js rename to src/bundle/Resources/public/js/scripts/helpers/content.type.helper.js index 780fb8bd71..564328d4a9 100644 --- a/src/bundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js +++ b/src/bundle/Resources/public/js/scripts/helpers/content.type.helper.js @@ -17,13 +17,13 @@ }, {}); /** - * Returns href to content type icon + * Returns an URL to a content type icon * * @function getContentTypeIcon * @param {String} contentTypeIdentifier - * @returns {String|null} href to icon + * @returns {String|null} url to icon */ - const getContentTypeIcon = (contentTypeIdentifier) => { + const getContentTypeIconUrl = (contentTypeIdentifier) => { if (!contentTypesDataMap) { contentTypesDataMap = createContentTypeDataMap(); } @@ -32,12 +32,12 @@ return null; } - const iconHref = contentTypesDataMap[contentTypeIdentifier].thumbnail; + const iconUrl = contentTypesDataMap[contentTypeIdentifier].thumbnail; - return iconHref; + return iconUrl; }; eZ.addConfig('helpers.contentType', { - getContentTypeIcon, + getContentTypeIconUrl, }); })(window, document, window.eZ); diff --git a/src/bundle/Resources/views/layout.html.twig b/src/bundle/Resources/views/layout.html.twig index cbebfbd948..1026305467 100644 --- a/src/bundle/Resources/views/layout.html.twig +++ b/src/bundle/Resources/views/layout.html.twig @@ -162,7 +162,7 @@ '@EzPlatformAdminUiBundle/Resources/public/js/scripts/helpers/request.helper.js' '@EzPlatformAdminUiBundle/Resources/public/js/scripts/helpers/notification.helper.js' '@EzPlatformAdminUiBundle/Resources/public/js/scripts/helpers/timezone.helper.js' - '@EzPlatformAdminUiBundle/Resources/public/js/scripts/helpers/content.type.icon.helper.js' + '@EzPlatformAdminUiBundle/Resources/public/js/scripts/helpers/content.type.helper.js' '@EzPlatformAdminUiBundle/Resources/public/js/scripts/admin.notifications.js' '@EzPlatformAdminUiModulesBundle/Resources/public/js/UniversalDiscovery.module.js' '@EzPlatformAdminUiBundle/Resources/public/js/scripts/button.trigger.js' From bcab1f001535f750f6d45f2a5ba7108f35490733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Tue, 29 Jan 2019 11:19:23 +0100 Subject: [PATCH 12/12] fixup! EZP-29998: As a developer, I want to define icons for content types --- .../Resources/config/default_parameters.yml | 2 + src/bundle/Resources/config/services.yml | 8 +- .../Service/ContentTypeIconResolverTest.php | 121 ++++++++++++++++++ .../UI/Service/ContentTypeIconResolver.php | 58 +++++---- 4 files changed, 160 insertions(+), 29 deletions(-) create mode 100644 src/lib/Tests/UI/Config/Service/ContentTypeIconResolverTest.php diff --git a/src/bundle/Resources/config/default_parameters.yml b/src/bundle/Resources/config/default_parameters.yml index ddb46e3087..aedea5e7c3 100644 --- a/src/bundle/Resources/config/default_parameters.yml +++ b/src/bundle/Resources/config/default_parameters.yml @@ -58,6 +58,8 @@ parameters: thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#video' ezsettings.default.content_type.landing_page: thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#landing_page' + ezsettings.default.content_type.default-config: + thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#file' ezplatform.content_view.tabs.default_template: '@@ezdesign/parts/tab/default.html.twig' diff --git a/src/bundle/Resources/config/services.yml b/src/bundle/Resources/config/services.yml index 1398b956c8..ef3e6096b8 100644 --- a/src/bundle/Resources/config/services.yml +++ b/src/bundle/Resources/config/services.yml @@ -21,10 +21,6 @@ imports: - { resource: services/permissions.yml } - { resource: services/forms.yml } -parameters: - # default content type icon - ezsystems.ezplatform_admin_ui.default_content_type_thumbnail: '/bundles/ezplatformadminui/img/ez-icons.svg#file' - services: _defaults: autowire: true @@ -131,6 +127,4 @@ services: EzSystems\EzPlatformAdminUiBundle\Templating\Twig\UserPreferencesGlobalExtension: ~ - EzSystems\EzPlatformAdminUi\UI\Service\ContentTypeIconResolver: - arguments: - $defaultThumbnail: '%ezsystems.ezplatform_admin_ui.default_content_type_thumbnail%' + EzSystems\EzPlatformAdminUi\UI\Service\ContentTypeIconResolver: ~ diff --git a/src/lib/Tests/UI/Config/Service/ContentTypeIconResolverTest.php b/src/lib/Tests/UI/Config/Service/ContentTypeIconResolverTest.php new file mode 100644 index 0000000000..c39a440ebe --- /dev/null +++ b/src/lib/Tests/UI/Config/Service/ContentTypeIconResolverTest.php @@ -0,0 +1,121 @@ +configResolver = $this->createMock(ConfigResolverInterface::class); + $this->packages = $this->createMock(Packages::class); + + $this->contentTypeIconResolver = new ContentTypeIconResolver( + $this->configResolver, + $this->packages + ); + } + + /** + * @dataProvider dataProviderForGetContentTypeIcon + */ + public function testGetContentTypeIcon(array $config, string $identifier, string $expected) + { + $this->configResolver + ->expects($this->any()) + ->method('hasParameter') + ->willReturnCallback(function (string $key) use ($config) { + $key = explode('.', $key); + + return isset($config[array_pop($key)]); + }); + + $this->configResolver + ->expects($this->any()) + ->method('getParameter') + ->willReturnCallback(function (string $key) use ($config) { + $key = explode('.', $key); + + return $config[array_pop($key)]; + }); + + $this->packages + ->expects($this->any()) + ->method('getUrl') + ->willReturnCallback(function (string $uri) { + return "https://cdn.example.com/$uri"; + }); + + $this->assertEquals($expected, $this->contentTypeIconResolver->getContentTypeIcon($identifier)); + } + + public function dataProviderForGetContentTypeIcon(): array + { + return [ + [ + [ + 'custom' => [ + 'thumbnail' => 'icon.svg#custom', + ], + 'default-config' => [ + 'thumbnail' => 'icon.svg#default', + ], + ], + 'custom', + 'https://cdn.example.com/icon.svg#custom', + ], + [ + [ + 'custom-without-fragment' => [ + 'thumbnail' => 'icon.png', + ], + 'default-config' => [ + 'thumbnail' => 'icon.svg#default', + ], + ], + 'custom-without-fragment', + 'https://cdn.example.com/icon.png', + ], + [ + [ + 'custom-without-icon' => [ + 'thumbnail' => null, + ], + 'default-config' => [ + 'thumbnail' => 'icon.svg#default', + ], + ], + 'custom-without-icon', + 'https://cdn.example.com/icon.svg#default', + ], + [ + [ + 'default-config' => [ + 'thumbnail' => 'icon.svg#default', + ], + ], + 'custom-with-missing-config', + 'https://cdn.example.com/icon.svg#default', + ], + ]; + } +} diff --git a/src/lib/UI/Service/ContentTypeIconResolver.php b/src/lib/UI/Service/ContentTypeIconResolver.php index 7c71db8c34..399ff49f7c 100644 --- a/src/lib/UI/Service/ContentTypeIconResolver.php +++ b/src/lib/UI/Service/ContentTypeIconResolver.php @@ -11,58 +11,72 @@ use eZ\Publish\Core\MVC\ConfigResolverInterface; use Symfony\Component\Asset\Packages; -class ContentTypeIconResolver +final class ContentTypeIconResolver { + private const DEFAULT_IDENTIFIER = 'default-config'; + private const PARAM_NAME_FORMAT = 'content_type.%s'; + + private const ICON_KEY = 'thumbnail'; + /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */ private $configResolver; /** @var \Symfony\Component\Asset\Packages */ private $packages; - /** @var string|null */ - private $defaultThumbnail; - /** * @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver * @param \Symfony\Component\Asset\Packages $packages - * @param string|null $defaultThumbnail */ - public function __construct(ConfigResolverInterface $configResolver, Packages $packages, ?string $defaultThumbnail) + public function __construct(ConfigResolverInterface $configResolver, Packages $packages) { $this->configResolver = $configResolver; $this->packages = $packages; - $this->defaultThumbnail = $defaultThumbnail; } /** * Returns path to content type icon. * - * Path is resolved based on configuration (ezpublish.system..content_type). If there isn't coresponding - * entry for given content type, then path to default icon will be returned. + * Path is resolved based on configuration (ezpublish.system..content_type.). If there isn't + * corresponding entry for given content type, then path to default icon will be returned. * * @param string $identifier * - * @return string|null + * @return string */ - public function getContentTypeIcon(string $identifier): ?string + public function getContentTypeIcon(string $identifier): string { - $thumbnail = null; + $icon = $this->resolveIcon($identifier); - $parameterName = $this->getConfigParameterName($identifier); - if ($this->configResolver->hasParameter($parameterName)) { - $thumbnail = $this->configResolver->getParameter($parameterName)['thumbnail']; + $fragment = null; + if (strpos($icon, '#') !== false) { + list($icon, $fragment) = explode('#', $icon); } - if (empty($thumbnail)) { - $thumbnail = $this->defaultThumbnail; + return $this->packages->getUrl($icon) . ($fragment ? '#' . $fragment : ''); + } + + /** + * @param string $identifier + * + * @return string + */ + private function resolveIcon(string $identifier): string + { + $config = null; + + $parameterName = $this->getConfigParameterName($identifier); + if ($this->configResolver->hasParameter($parameterName)) { + $config = $this->configResolver->getParameter($parameterName); } - $fragment = null; - if (strpos($thumbnail, '#') !== false) { - list($thumbnail, $fragment) = explode('#', $thumbnail); + if ($config === null || empty($config[self::ICON_KEY])) { + $config = $this->configResolver->getParameter( + $this->getConfigParameterName(self::DEFAULT_IDENTIFIER) + ); } - return $this->packages->getUrl($thumbnail) . ($fragment ? '#' . $fragment : ''); + return $config[self::ICON_KEY]; } /** @@ -74,6 +88,6 @@ public function getContentTypeIcon(string $identifier): ?string */ private function getConfigParameterName(string $identifier): string { - return "content_type.$identifier"; + return sprintf(self::PARAM_NAME_FORMAT, $identifier); } }