-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added possibility to define icons for content type group
- Loading branch information
1 parent
64f16ae
commit 2ac4f44
Showing
9 changed files
with
162 additions
and
70 deletions.
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
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
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
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
36 changes: 36 additions & 0 deletions
36
src/bundle/Templating/Twig/ContentTypeGroupIconExtension.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,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'], | ||
] | ||
), | ||
]; | ||
} | ||
} |
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
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,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); | ||
} | ||
} |
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
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,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); | ||
} | ||
} |