Skip to content

Commit

Permalink
Set configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Hannes Giesenow committed Dec 15, 2023
1 parent 63bbcae commit 6ed2991
Show file tree
Hide file tree
Showing 18 changed files with 244 additions and 31 deletions.
5 changes: 0 additions & 5 deletions Resources/config/ezplatform.yaml

This file was deleted.

13 changes: 0 additions & 13 deletions Resources/config/services.yaml

This file was deleted.

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},
"require": {
"php": "^8.1",
"ezsystems/ezplatform-core": "^2.3"
"ezsystems/ezplatform-core": "^2.3",
"symfony/finder": "^5.4|^6.4"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
Expand Down
7 changes: 7 additions & 0 deletions config/ezplatform.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ezplatform:
system:
default:
field_templates:
- { template: '@ElbformatIconFieldtype/icon_field.html.twig', priority: 0 }
fielddefinition_edit_templates:
- { template: '@ElbformatIconFieldtype/icon_field_type_definition.html.twig', priority: 0 }
29 changes: 29 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
services:
Elbformat\IbexaIconFieldtype\FieldType\Icon\Type:
arguments:
$serializer: '@eZ\Publish\SPI\FieldType\ValueSerializerInterface'
$validator: '@Symfony\Component\Validator\Validator\ValidatorInterface'
tags:
- { name: ezplatform.field_type, alias: icon }
- { name: ezplatform.field_type.form_mapper.value, fieldType: icon }
- { name: ezplatform.field_type.form_mapper.definition, fieldType: icon }

Elbformat\IbexaIconFieldtype\FieldType\Icon\SearchField:
class: '%ezpublish.fieldType.indexable.unindexed.class%'
tags:
- { name: ezplatform.field_type.indexable, alias: icon }

Elbformat\IbexaIconFieldtype\Form\Type\IconType:
arguments:
$iconSetManager: '@Elbformat\IbexaIconFieldtype\IconSet\IconSetManager'
tags:
- { name: form.type }
Elbformat\IbexaIconFieldtype\Form\Type\IconSettingsType:
arguments:
$iconSetManager: '@Elbformat\IbexaIconFieldtype\IconSet\IconSetManager'
tags:
- { name: form.type }

Elbformat\IbexaIconFieldtype\IconSet\IconSetManager:
arguments:
$configs: [] # Will be set from bundle config
32 changes: 32 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

namespace Elbformat\IbexaIconFieldtype\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('elbformat_icon_fieldtype');

$treeBuilder->getRootNode()
->arrayPrototype() // Name of the set
->normalizeKeys(false)
->children()
->arrayNode('items') // Fix list
->requiresAtLeastOneElement()
->normalizeKeys(false)
->scalarPrototype()->end()
->end()
->scalarNode('folder')->end() // Path to look in
->scalarNode('pattern')->end() // Glob expression
->end()
->end()
;

return $treeBuilder;
}
}
19 changes: 15 additions & 4 deletions src/DependencyInjection/ElbformatIconFieldtypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Elbformat\IbexaIconFieldtype\DependencyInjection;

use Elbformat\IbexaIconFieldtype\IconSet\IconSetManager;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
Expand All @@ -14,18 +15,28 @@ class ElbformatIconFieldtypeExtension extends Extension implements PrependExtens
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../Resources/config/'));
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../config/'));
$loader->load('services.yaml');

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$definition = $container->getDefinition(IconSetManager::class);
$definition->setArgument('$configs', $config);
}

public function prepend(ContainerBuilder $container)
{
// Add template for rendering
$configFile = __DIR__ . '/../../Resources/config/ezplatform.yaml';
$configFile = __DIR__.'/../../config/ezplatform.yaml';
$config = Yaml::parse(file_get_contents($configFile));
$container->prependExtensionConfig('ezpublish', $config['ezplatform']);
// register namespace (as this is not done automatically. Maybe the missing "bundle" in path?)
$container->prependExtensionConfig('twig', ['paths'=> [__DIR__.'/../../templates' => 'ElbformatIconFieldtype']]);

// Register namespace (as this is not done automatically. Maybe the missing "bundle" in path?)
$container->prependExtensionConfig('twig', ['paths' => [__DIR__.'/../../templates' => 'ElbformatIconFieldtype']]);

// Register translations (as this is not done automatically. Maybe the missing "bundle" in path?)
$container->prependExtensionConfig('framework', ['translator' => ['paths' => [__DIR__.'/../../translations']]]);

}
}
23 changes: 20 additions & 3 deletions src/FieldType/Icon/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@

namespace Elbformat\IbexaIconFieldtype\FieldType\Icon;

use Elbformat\IbexaIconFieldtype\Form\Type\IconSettingsType;
use Elbformat\IbexaIconFieldtype\Form\Type\IconType;
use eZ\Publish\SPI\FieldType\Generic\Type as GenericType;
use eZ\Publish\SPI\Persistence\Content\Field;
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
use EzSystems\EzPlatformAdminUi\FieldType\FieldDefinitionFormMapperInterface;
use EzSystems\EzPlatformAdminUi\Form\Data\FieldDefinitionData;
use EzSystems\EzPlatformContentForms\Data\Content\FieldData;
use EzSystems\EzPlatformContentForms\FieldType\FieldValueFormMapperInterface;
use Symfony\Component\Form\FormInterface;

final class Type extends GenericType implements FieldValueFormMapperInterface
final class Type extends GenericType implements FieldValueFormMapperInterface, FieldDefinitionFormMapperInterface
{
public function getFieldTypeIdentifier(): string
{
Expand All @@ -21,14 +24,28 @@ public function getFieldTypeIdentifier(): string
public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
{
$definition = $data->fieldDefinition;
$iconSet = $definition->getFieldSettings()['iconset'];
$fieldForm->add('value', IconType::class, [
'required' => $definition->isRequired,
'label' => $definition->getName(),
'icon_set' => $iconSet,
]);
}

public function isSearchable(): bool
public function getSettingsSchema(): array
{
return false;
return [
'iconset' => [
'type' => 'string',
'default' => '',
],
];
}

public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $data): void
{
$fieldDefinitionForm->add('fieldSettings', IconSettingsType::class, [
'label' => false,
]);
}
}
5 changes: 5 additions & 0 deletions src/FieldType/Icon/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public function getIcon(): ?string
{
return $this->icon;
}

public function setIcon(?string $icon): void
{
$this->icon = $icon;
}
}
24 changes: 24 additions & 0 deletions src/Form/Type/IconSettingsType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);

namespace Elbformat\IbexaIconFieldtype\Form\Type;

use Elbformat\IbexaIconFieldtype\IconSet\IconSetManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

final class IconSettingsType extends AbstractType
{
public function __construct(
private readonly IconSetManager $iconSetManager,
) { }

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('iconset', ChoiceType::class,[
'choices' => $this->iconSetManager->getSetList()
]);
}
}
13 changes: 12 additions & 1 deletion src/Form/Type/IconType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,33 @@
namespace Elbformat\IbexaIconFieldtype\Form\Type;

use Elbformat\IbexaIconFieldtype\FieldType\Icon\Value;
use Elbformat\IbexaIconFieldtype\IconSet\IconSetManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class IconType extends AbstractType
{
public function __construct(
private readonly IconSetManager $iconSetManager,
) { }


public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('icon', ChoiceType::class);
$iconSet = $options['icon_set'];
$builder->add('icon', ChoiceType::class,[
'choices' => $this->iconSetManager->getSet($iconSet)->getList()
]);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Value::class,
'icon_set' => null,
]);
$resolver->addAllowedTypes('icon_set','string');
}
}
24 changes: 24 additions & 0 deletions src/IconSet/IconSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);

namespace Elbformat\IbexaIconFieldtype\IconSet;

class IconSet
{
/** @var array<string,string> */
protected array $items;

/** @param string[] */
public function __construct(array $items)
{
// Convert to string => string array
$this->items = array_flip($items);
array_walk($this->items, fn(&$val, $key) => $val = $key);
}

/** @return string[] */
public function getList(): array
{
return $this->items;
}
}
51 changes: 51 additions & 0 deletions src/IconSet/IconSetManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);

namespace Elbformat\IbexaIconFieldtype\IconSet;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

class IconSetManager
{
/** @var array<string,IconSet> */
protected array $sets;

/** array<string,array{?items:string[],?folder:string,?pattern:string} */
public function __construct(array $configs)
{
foreach ($configs as $setName => $setConfig) {
$items = [];
if (null !== ($setConfig['items']??null)) {
$items = $setConfig['items'];
}
if (null !== ($setConfig['folder']??null)) {
$finder = (new Finder())->files()->in($setConfig['folder']);
if (null !== ($setConfig['pattern']??null)) {
$finder = $finder->name($setConfig['pattern']);
}
foreach ($finder as $file) {
$items[] = $file->getFilename();
}
}
$this->sets[$setName] = new IconSet($items);
}
}

public function getSet(string $name): IconSet
{
if (!isset($this->sets[$name])) {
throw new \InvalidArgumentException('IconSet not found');
}
return $this->sets[$name];
}

/** @return array<string,string> */
public function getSetList():array
{
// Convert to string => string array
$sets = array_flip(array_keys($this->sets));
array_walk($sets, fn(&$val, $key) => $val = $key);
return $sets;
}
}
10 changes: 10 additions & 0 deletions templates/icon.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{#
@var icon ?string
@var iconset string
#}
{% if icon is not null %}
<i class="icon-{{ icon }}"></i>
{% if "set2" == iconset %}
<img src="assets/build/images/icons/{{ icon }}" />
{% endif %}
{% endif %}
8 changes: 4 additions & 4 deletions templates/icon_field.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% block icon_field %}
{% if field.value.icon is not null %}
<i class="icon-{{ field.value.icon }}"></i>
<img src="{{ field.value.icon }}.svg" />
{% endif %}
{{ include('@ElbformatIconFieldtype/icon.html.twig', {
icon: field.value.icon,
iconset: fieldSettings.iconset
}, with_context=false) }}
{% endblock %}
7 changes: 7 additions & 0 deletions templates/icon_field_type_definition.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% block icon_field_definition_edit %}
<div class="{% if group_class is not empty %}{{ group_class }}{% endif %}">
{{- form_label(form.fieldSettings.iconset) -}}
{{- form_errors(form.fieldSettings.iconset) -}}
{{- form_widget(form.fieldSettings.iconset) -}}
</div>
{% endblock %}
1 change: 1 addition & 0 deletions translations/fieldtypes.de.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
icon.name: Symbol
1 change: 1 addition & 0 deletions translations/fieldtypes.en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
icon.name: Icon

0 comments on commit 6ed2991

Please sign in to comment.