Skip to content

Commit

Permalink
Added possibility to define icons for content type group
Browse files Browse the repository at this point in the history
  • Loading branch information
mikadamczyk committed Mar 28, 2024
1 parent 64f16ae commit 2ac4f44
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 70 deletions.
9 changes: 9 additions & 0 deletions src/bundle/Resources/config/default_parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ parameters:
ibexa.site_access.config.default.content_type.default-config:
thumbnail: '/bundles/ibexaadminui/img/ibexa-icons.svg#file'

ibexa.site_access.config.default.content_type_group.default-config:
thumbnail: '/bundles/ibexaadminui/img/ibexa-icons.svg#file'
ibexa.site_access.config.default.content_type_group.content:
thumbnail: '/bundles/ibexaadminui/img/ibexa-icons.svg#article'
ibexa.site_access.config.default.content_type_group.users:
thumbnail: '/bundles/ibexaadminui/img/ibexa-icons.svg#user_group'
ibexa.site_access.config.default.content_type_group.media:
thumbnail: '/bundles/ibexaadminui/img/ibexa-icons.svg#gallery'

ibexa.content_view.tabs.default_template: '@@ibexadesign/ui/tab/default.html.twig'

ibexa.multifile_upload.location.mappings: []
Expand Down
9 changes: 8 additions & 1 deletion src/bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,14 @@ services:
Ibexa\Bundle\AdminUi\Templating\Twig\UserPreferencesGlobalExtension:
lazy: true

Ibexa\AdminUi\UI\Service\ContentTypeIconResolver: ~
Ibexa\AdminUi\UI\Service\IconResolver:
abstract: true

Ibexa\AdminUi\UI\Service\ContentTypeIconResolver:
parent: Ibexa\AdminUi\UI\Service\IconResolver

Ibexa\AdminUi\UI\Service\ContentTypeGroupIconResolver:
parent: Ibexa\AdminUi\UI\Service\IconResolver

Ibexa\ContentForms\ConfigResolver\MaxUploadSize: ~

Expand Down
2 changes: 2 additions & 0 deletions src/bundle/Resources/config/services/ui_config/common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ services:

Ibexa\Bundle\AdminUi\Templating\Twig\ContentTypeIconExtension: ~

Ibexa\Bundle\AdminUi\Templating\Twig\ContentTypeGroupIconExtension: ~

Ibexa\Bundle\AdminUi\Templating\Twig\EmbeddedItemEditFormExtension: ~

Ibexa\AdminUi\UI\Config\Provider\UserContentTypes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@
content: col_raw,
raw: true,
}]) %}
{% set col_raw %}
<svg class="ibexa-icon ibexa-icon--small">
<use xlink:href="{{ ibexa_content_type_group_icon(content_type_group.identifier) }}"></use>
</svg>
{% endset %}
{% set body_row_cols = body_row_cols|merge([{
has_icon: true,
content: col_raw,
raw: true,
}]) %}

{% set col_raw %}
{% set view_url = path('ibexa.content_type_group.view', {
Expand Down
36 changes: 36 additions & 0 deletions src/bundle/Templating/Twig/ContentTypeGroupIconExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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\Bundle\AdminUi\Templating\Twig;

use Ibexa\AdminUi\UI\Service\ContentTypeGroupIconResolver;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class ContentTypeGroupIconExtension extends AbstractExtension
{
private ContentTypeGroupIconResolver $contentTypeGroupIconResolver;

public function __construct(ContentTypeGroupIconResolver $contentTypeGroupIconResolver)
{
$this->contentTypeGroupIconResolver = $contentTypeGroupIconResolver;
}

public function getFunctions(): array
{
return [
new TwigFunction(
'ibexa_content_type_group_icon',
[$this->contentTypeGroupIconResolver, 'getContentTypeGroupIcon'],
[
'is_safe' => ['html'],
]
),
];
}
}
9 changes: 1 addition & 8 deletions src/bundle/Templating/Twig/ContentTypeIconExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,13 @@

class ContentTypeIconExtension extends AbstractExtension
{
/** @var \Ibexa\AdminUi\UI\Service\ContentTypeIconResolver */
private $contentTypeIconResolver;
private ContentTypeIconResolver $contentTypeIconResolver;

/**
* @param \Ibexa\AdminUi\UI\Service\ContentTypeIconResolver $contentTypeIconResolver
*/
public function __construct(ContentTypeIconResolver $contentTypeIconResolver)
{
$this->contentTypeIconResolver = $contentTypeIconResolver;
}

/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
Expand Down
30 changes: 30 additions & 0 deletions src/lib/UI/Service/ContentTypeGroupIconResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\UI\Service;

use Symfony\Component\String\Slugger\AsciiSlugger;

final class ContentTypeGroupIconResolver extends IconResolver
{
private const PARAM_NAME_FORMAT = 'content_type_group.%s';

/**
* Returns path to content type group icon.
*
* Path is resolved based on configuration (ibexa.system.<SCOPE>.content_type.<IDENTIFIER>). If there isn't
* corresponding entry for given content type, then path to default icon will be returned.
*/
public function getContentTypeGroupIcon(string $identifier): string
{
$slugger = new AsciiSlugger();
$identifier = (string)$slugger->slug($identifier, '_')->lower();

return $this->getIcon(self::PARAM_NAME_FORMAT, $identifier);
}
}
64 changes: 3 additions & 61 deletions src/lib/UI/Service/ContentTypeIconResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,77 +8,19 @@

namespace Ibexa\AdminUi\UI\Service;

use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Symfony\Component\Asset\Packages;

final class ContentTypeIconResolver
final class ContentTypeIconResolver extends IconResolver
{
private const DEFAULT_IDENTIFIER = 'default-config';
private const PARAM_NAME_FORMAT = 'content_type.%s';

private const ICON_KEY = 'thumbnail';

/** @var \Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface */
private $configResolver;

/** @var \Symfony\Component\Asset\Packages */
private $packages;

/**
* @param \Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface $configResolver
* @param \Symfony\Component\Asset\Packages $packages
*/
public function __construct(ConfigResolverInterface $configResolver, Packages $packages)
{
$this->configResolver = $configResolver;
$this->packages = $packages;
}

/**
* Returns path to content type icon.
*
* Path is resolved based on configuration (ezpublish.system.<SCOPE>.content_type.<IDENTIFIER>). If there isn't
* Path is resolved based on configuration (ibexa.system.<SCOPE>.content_type.<IDENTIFIER>). If there isn't
* corresponding entry for given content type, then path to default icon will be returned.
*
* @throws \Ibexa\AdminUi\Exception\ContentTypeIconNotFoundException
*/
public function getContentTypeIcon(string $identifier): string
{
$icon = $this->resolveIcon($identifier);

$fragment = null;
if (strpos($icon, '#') !== false) {
[$icon, $fragment] = explode('#', $icon);
}

return $this->packages->getUrl($icon) . ($fragment ? '#' . $fragment : '');
}

/**
* @throws \Ibexa\AdminUi\Exception\ContentTypeIconNotFoundException
*/
private function resolveIcon(string $identifier): string
{
$parameterName = $this->getConfigParameterName($identifier);
$defaultParameterName = $this->getConfigParameterName(self::DEFAULT_IDENTIFIER);

if ($this->configResolver->hasParameter($parameterName)) {
$config = $this->configResolver->getParameter($parameterName);
}

if ((empty($config) || empty($config[self::ICON_KEY])) && $this->configResolver->hasParameter($defaultParameterName)) {
$config = $this->configResolver->getParameter($defaultParameterName);
}

return $config[self::ICON_KEY] ?? '';
}

/**
* Return configuration parameter name for given content type identifier.
*/
private function getConfigParameterName(string $identifier): string
{
return sprintf(self::PARAM_NAME_FORMAT, $identifier);
return $this->getIcon(self::PARAM_NAME_FORMAT, $identifier);
}
}

Expand Down
63 changes: 63 additions & 0 deletions src/lib/UI/Service/IconResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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\UI\Service;

use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Symfony\Component\Asset\Packages;

abstract class IconResolver
{
protected const DEFAULT_IDENTIFIER = 'default-config';
protected const ICON_KEY = 'thumbnail';

private ConfigResolverInterface $configResolver;

protected Packages $packages;

public function __construct(ConfigResolverInterface $configResolver, Packages $packages)
{
$this->configResolver = $configResolver;
$this->packages = $packages;
}

protected function getIcon(string $format, string $identifier): string
{
$icon = $this->resolveIcon($format, $identifier);
$fragment = null;
if (strpos($icon, '#') !== false) {
[$icon, $fragment] = explode('#', $icon);
}

return $this->packages->getUrl($icon) . ($fragment ? '#' . $fragment : '');
}

private function resolveIcon(string $format, string $identifier): string
{
$parameterName = $this->getConfigParameterName($format, $identifier);
$defaultParameterName = $this->getConfigParameterName($format, static::DEFAULT_IDENTIFIER);

if ($this->configResolver->hasParameter($parameterName)) {
$config = $this->configResolver->getParameter($parameterName);
}

if ((empty($config) || empty($config[static::ICON_KEY])) && $this->configResolver->hasParameter($defaultParameterName)) {
$config = $this->configResolver->getParameter($defaultParameterName);
}

return $config[static::ICON_KEY] ?? '';
}

/**
* Return configuration parameter name for given content type identifier.
*/
private function getConfigParameterName(string $format, string $identifier): string
{
return sprintf($format, $identifier);
}
}

0 comments on commit 2ac4f44

Please sign in to comment.