From bcf9c23e994785652db937894a7110904b888d3f Mon Sep 17 00:00:00 2001 From: Ousmane KANTE Date: Tue, 14 May 2024 16:11:11 +0200 Subject: [PATCH 1/4] init ibexa fieldTypes --- .ddev/config.yaml | 2 +- components/IbexaFieldTypes/.gitignore | 1 + components/IbexaFieldTypes/README.md | 50 ++++++ .../bundle/AlmaviaCXFieldTypesBundle.php | 19 +++ .../AlmaviaCXFieldTypesExtension.php | 51 ++++++ .../DependencyInjection/Configuration.php | 28 ++++ .../Form/AcxSelectionSettingsType.php | 24 +++ .../AcxSelection/Form/AcxSelectionType.php | 42 +++++ .../Form/FieldValueTransformer.php | 32 ++++ .../bundle/FieldType/AcxSelection/Type.php | 151 ++++++++++++++++++ .../bundle/FieldType/AcxSelection/Value.php | 24 +++ .../Resources/config/default_settings.yaml | 7 + .../config/field_types_templates.yaml | 6 + .../bundle/Resources/config/services.yaml | 19 +++ .../Resources/translations/fieldtypes.en.yaml | 1 + .../standard/fields/acxselection.html.twig | 5 + .../fields/acxselection/default.html.twig | 9 ++ .../fields/definition/acxselection.html.twig | 12 ++ .../bundle/Service/SelectionInterface.php | 8 + .../bundle/Service/SelectionService.php | 20 +++ components/IbexaFieldTypes/ci-config.yaml | 3 + components/IbexaFieldTypes/composer.json | 20 +++ 22 files changed, 533 insertions(+), 1 deletion(-) create mode 100644 components/IbexaFieldTypes/.gitignore create mode 100644 components/IbexaFieldTypes/README.md create mode 100644 components/IbexaFieldTypes/bundle/AlmaviaCXFieldTypesBundle.php create mode 100644 components/IbexaFieldTypes/bundle/DependencyInjection/AlmaviaCXFieldTypesExtension.php create mode 100644 components/IbexaFieldTypes/bundle/DependencyInjection/Configuration.php create mode 100644 components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/AcxSelectionSettingsType.php create mode 100644 components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/AcxSelectionType.php create mode 100644 components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/FieldValueTransformer.php create mode 100644 components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Type.php create mode 100644 components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Value.php create mode 100644 components/IbexaFieldTypes/bundle/Resources/config/default_settings.yaml create mode 100644 components/IbexaFieldTypes/bundle/Resources/config/field_types_templates.yaml create mode 100644 components/IbexaFieldTypes/bundle/Resources/config/services.yaml create mode 100644 components/IbexaFieldTypes/bundle/Resources/translations/fieldtypes.en.yaml create mode 100644 components/IbexaFieldTypes/bundle/Resources/views/themes/standard/fields/acxselection.html.twig create mode 100644 components/IbexaFieldTypes/bundle/Resources/views/themes/standard/fields/acxselection/default.html.twig create mode 100644 components/IbexaFieldTypes/bundle/Resources/views/themes/standard/fields/definition/acxselection.html.twig create mode 100644 components/IbexaFieldTypes/bundle/Service/SelectionInterface.php create mode 100644 components/IbexaFieldTypes/bundle/Service/SelectionService.php create mode 100644 components/IbexaFieldTypes/ci-config.yaml create mode 100644 components/IbexaFieldTypes/composer.json diff --git a/.ddev/config.yaml b/.ddev/config.yaml index 8347238cc..f0b5e171f 100644 --- a/.ddev/config.yaml +++ b/.ddev/config.yaml @@ -40,7 +40,7 @@ web_environment: - PANTHER_CHROME_ARGUMENTS='--disable-dev-shm-usage --ignore-certificate-errors' - PANTHER_NO_SANDBOX=1 - PANTHER_NO_HEADLESS=0 -nodejs_version: "16" +nodejs_version: "18" webimage_extra_packages: - pngquant - jpegoptim diff --git a/components/IbexaFieldTypes/.gitignore b/components/IbexaFieldTypes/.gitignore new file mode 100644 index 000000000..57872d0f1 --- /dev/null +++ b/components/IbexaFieldTypes/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/components/IbexaFieldTypes/README.md b/components/IbexaFieldTypes/README.md new file mode 100644 index 000000000..4bd80cc9d --- /dev/null +++ b/components/IbexaFieldTypes/README.md @@ -0,0 +1,50 @@ +## AlmaviaCX Ibexa FieldTypes +This bundle add some field types to ibexa + +## Installation +```bash +composer require almaviacx/ibexa-field-types +``` + +update `config/bundles.php` file by adding this: + +```php + AlmaviaCX\Ibexa\Bundle\FieldTypes\AlmaviaCXFieldTypesBundle::class => ['all' => true],, +``` + +## Configuration + +### ACX selection fieldType + +#### selection choices configuration +create a configuration `config/packages/acx_field_types.yaml` with the following +```yaml +acx_field_types: + system: + default: # or global or siteaccess GROUP + acx_selection: + my_selection_choices: # value of choices_entry in fiedltypes settings + # Label: value + "atlicon-grid": atlicon-grid + atlicon-industry: atlicon-industry + atlicon-people-simple: atlicon-people-simple + atlicon-gears: atlicon-gears +``` +#### template to render `my_selection_choices` field value +create a template accessible throught: `@ibexadesign/fields/acxselection/my_custom_choices.html.twig` +#### edit (or create) the content_type + - add a field of type almaviacx selection + - fieldSettings: + - Choices entry: `my_selection_choices` + - template: `my_custom_choices` + - check single / multiple selection + +update the template to your needs: +example for atlicon to diplay icon +`@ibexadesign/fields/acxselection/my_custom_choices.html.twig` content can be: +```twig +{% set selected_value = field.value.getSelection() %} +{% if selected_value is not empty %} + getConfiguration($configs, $container); + $config = $this->processConfiguration($configuration, $configs); + + $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); + $loader->load('default_settings.yaml'); + $processor = new ConfigurationProcessor($container, Configuration::CONFIG_RESOLVER_NAMESPACE ); + $processor->mapConfigArray('acx_selection', $config, ContextualizerInterface::MERGE_FROM_SECOND_LEVEL); + + + $loader->load('services.yaml'); + + + + } + + public function prepend(ContainerBuilder $container): void + { + $configFile = __DIR__ . '/../Resources/config/field_types_templates.yaml'; + $container->prependExtensionConfig('ibexa', Yaml::parse(file_get_contents($configFile))); + $container->addResource(new FileResource($configFile)); + } +} \ No newline at end of file diff --git a/components/IbexaFieldTypes/bundle/DependencyInjection/Configuration.php b/components/IbexaFieldTypes/bundle/DependencyInjection/Configuration.php new file mode 100644 index 000000000..4dae039a3 --- /dev/null +++ b/components/IbexaFieldTypes/bundle/DependencyInjection/Configuration.php @@ -0,0 +1,28 @@ +getRootNode(); + + $this->generateScopeBaseNode($rootNode) + ->arrayNode('acx_selection')->info('Choices entries') + ->prototype('array') + ->useAttributeAsKey('name') + ->prototype('variable')->end() + ->end() + ->end() + ; + + return $treeBuilder; + } +} diff --git a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/AcxSelectionSettingsType.php b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/AcxSelectionSettingsType.php new file mode 100644 index 000000000..8eff216ea --- /dev/null +++ b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/AcxSelectionSettingsType.php @@ -0,0 +1,24 @@ +add( + 'isMultiple', + CheckboxType::class, + [ + 'required' => false, + 'label' => 'Multiple', + ]); + $builder->add('choices_entry', TextType::class); + $builder->add('template', TextType::class); + } +} \ No newline at end of file diff --git a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/AcxSelectionType.php b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/AcxSelectionType.php new file mode 100644 index 000000000..dc26b8968 --- /dev/null +++ b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/AcxSelectionType.php @@ -0,0 +1,42 @@ +getBlockPrefix(); + } + + public function getParent(): string + { + return ChoiceType::class; + } + + public function buildForm(FormBuilderInterface $builder, array $options): void + { + $builder->addModelTransformer(new FieldValueTransformer($options['multiple'])); + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'expanded' => false, + 'required' => true, + 'multiple' => false, + 'choices' => [], + ]); + } + +} \ No newline at end of file diff --git a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/FieldValueTransformer.php b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/FieldValueTransformer.php new file mode 100644 index 000000000..712cfe37e --- /dev/null +++ b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/FieldValueTransformer.php @@ -0,0 +1,32 @@ +selection ?? []); + + return $this->isMultiple === true ? $selection : ($selection[0] ?? null); + } + + public function reverseTransform($value): ?Value + { + $value = (array) $value; + return new Value($this->isMultiple === false ? [reset($value)]: $value); + } +} diff --git a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Type.php b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Type.php new file mode 100644 index 000000000..db37a5bbf --- /dev/null +++ b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Type.php @@ -0,0 +1,151 @@ + [ + 'type' => 'string', + 'default' => 'default', + ], + 'template' => [ + 'type' => 'string', + 'default' => 'default', + ], + 'isMultiple' => [ + 'type' => 'bool', + 'default' => false, + ], + ]; + } + + public function fromHash($hash): Value + { + return new Value((array)$hash); + } + + public function toHash(SPIValue $value): ?array + { + if (!($value instanceof Value)) { + return []; + } + return (array) $value->selection; + } + + public function getName(SPIValue $value, FieldDefinition $fieldDefinition, string $languageCode): string + { + if (empty($value->selection)) { + return ''; + } + + $names = []; + $fieldSettings = $fieldDefinition->getFieldSettings(); + + $choices = array_flip($this->selectionService->getChoices((string)$fieldSettings['choices_entry'])); + foreach ($value->selection as $selectedName) { + $name = $choices[$selectedName]?? null; + if ($name === null) { + continue; + } + $names[]= $name; + } + return implode(' ', array_filter($names)); + } + + public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data): void + { + $definition = $data->fieldDefinition; + $fieldSettings = $definition->getFieldSettings(); + $fieldForm->add('value', AcxSelectionType::class, [ + 'required' => $definition->isRequired, + 'label' => $definition->getName(), + 'multiple' => $fieldSettings['isMultiple']?? true, + 'choices' => $this->selectionService->getChoices((string)$fieldSettings['choices_entry']) + ]); + } + public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $data): void + { + $fieldDefinitionForm->add('fieldSettings', AcxSelectionSettingsType::class, [ + 'label' => false, + ]); + } + public function isSearchable(): bool + { + return true; + } + + public function getIndexData(Field $field, SPIFieldDefinition $fieldDefinition): array + { + $fieldValue = $field->value->data??[]; + $fieldValue = (string)reset($fieldValue); + $values = (array)$field->value->data; + return [ + new Search\Field( + 'value', + $fieldValue, + new Search\FieldType\StringField() + ), + new Search\Field( + 'values', + $values, + new Search\FieldType\MultipleStringField() + ), + new Search\Field( + 'fulltext', + $values, + new Search\FieldType\FullTextField() + ), + ]; + } + + public function getIndexDefinition(): array + { + return [ + 'value' => new Search\FieldType\StringField(), + 'values' => new Search\FieldType\MultipleStringField(), + ]; + } + + public function getDefaultMatchField(): string + { + return 'value'; + } + + public function getDefaultSortField(): string + { + return $this->getDefaultMatchField(); + } +} \ No newline at end of file diff --git a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Value.php b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Value.php new file mode 100644 index 000000000..24855f8ea --- /dev/null +++ b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Value.php @@ -0,0 +1,24 @@ +selection = $selection; + } + + public function getSelection(): string + { + return $this->selection[0] ?? ''; + } + + public function __toString() + { + return implode(',', $this->selection); + } +} \ No newline at end of file diff --git a/components/IbexaFieldTypes/bundle/Resources/config/default_settings.yaml b/components/IbexaFieldTypes/bundle/Resources/config/default_settings.yaml new file mode 100644 index 000000000..5f2cc8573 --- /dev/null +++ b/components/IbexaFieldTypes/bundle/Resources/config/default_settings.yaml @@ -0,0 +1,7 @@ +parameters: + acx_field_types.default.acx_selection: + default: + 'Document': 'docs' + 'Logiciel': 'log' + 'Outil': 'tool' + 'Youtube': 'youtube' \ No newline at end of file diff --git a/components/IbexaFieldTypes/bundle/Resources/config/field_types_templates.yaml b/components/IbexaFieldTypes/bundle/Resources/config/field_types_templates.yaml new file mode 100644 index 000000000..4f6c6aac0 --- /dev/null +++ b/components/IbexaFieldTypes/bundle/Resources/config/field_types_templates.yaml @@ -0,0 +1,6 @@ +system: + default: + field_templates: + - {template: '@ibexadesign/fields/acxselection.html.twig', priority: 0} + fielddefinition_edit_templates: + - { template: '@ibexadesign/fields/definition/acxselection.html.twig', priority: 0 } \ No newline at end of file diff --git a/components/IbexaFieldTypes/bundle/Resources/config/services.yaml b/components/IbexaFieldTypes/bundle/Resources/config/services.yaml new file mode 100644 index 000000000..829a7347d --- /dev/null +++ b/components/IbexaFieldTypes/bundle/Resources/config/services.yaml @@ -0,0 +1,19 @@ +services: + _defaults: + autoconfigure: true + autowire: true + public: false + + AlmaviaCX\Ibexa\Bundle\FieldTypes\Service\: + resource: '../../Service' + + AlmaviaCX\Ibexa\Bundle\FieldTypes\FieldType\AcxSelection\Form\AcxSelectionType: ~ + + AlmaviaCX\Ibexa\Bundle\FieldTypes\FieldType\AcxSelection\Form\AcxSelectionSettingsType: ~ + + AlmaviaCX\Ibexa\Bundle\FieldTypes\FieldType\AcxSelection\Type: + tags: + - { name: ibexa.field_type, alias: acxselection } + - { name: ibexa.admin_ui.field_type.form.mapper.value, fieldType: acxselection } + - { name: ibexa.admin_ui.field_type.form.mapper.definition, fieldType: acxselection } + - { name: ibexa.field_type.indexable, alias: acxselection } \ No newline at end of file diff --git a/components/IbexaFieldTypes/bundle/Resources/translations/fieldtypes.en.yaml b/components/IbexaFieldTypes/bundle/Resources/translations/fieldtypes.en.yaml new file mode 100644 index 000000000..b9a2fbb8a --- /dev/null +++ b/components/IbexaFieldTypes/bundle/Resources/translations/fieldtypes.en.yaml @@ -0,0 +1 @@ +acxselection.name: 'ALmaviacx Selection' \ No newline at end of file diff --git a/components/IbexaFieldTypes/bundle/Resources/views/themes/standard/fields/acxselection.html.twig b/components/IbexaFieldTypes/bundle/Resources/views/themes/standard/fields/acxselection.html.twig new file mode 100644 index 000000000..1555fdc82 --- /dev/null +++ b/components/IbexaFieldTypes/bundle/Resources/views/themes/standard/fields/acxselection.html.twig @@ -0,0 +1,5 @@ +{% block acxselection_field %} + {% set option_template = fieldSettings.template|default('default')|trim %} + {% set option_template = option_template == '' ? 'default':option_template %} + {% include '@ibexadesign/fields/acxselection/'~option_template~'.html.twig' %} +{% endblock %} \ No newline at end of file diff --git a/components/IbexaFieldTypes/bundle/Resources/views/themes/standard/fields/acxselection/default.html.twig b/components/IbexaFieldTypes/bundle/Resources/views/themes/standard/fields/acxselection/default.html.twig new file mode 100644 index 000000000..d6db4c83b --- /dev/null +++ b/components/IbexaFieldTypes/bundle/Resources/views/themes/standard/fields/acxselection/default.html.twig @@ -0,0 +1,9 @@ +{% if fieldSettings.isMultiple|default(false) %} + +{% else %} + {{ field.value.getSelection() }} +{% endif %} \ No newline at end of file diff --git a/components/IbexaFieldTypes/bundle/Resources/views/themes/standard/fields/definition/acxselection.html.twig b/components/IbexaFieldTypes/bundle/Resources/views/themes/standard/fields/definition/acxselection.html.twig new file mode 100644 index 000000000..28f330d20 --- /dev/null +++ b/components/IbexaFieldTypes/bundle/Resources/views/themes/standard/fields/definition/acxselection.html.twig @@ -0,0 +1,12 @@ +{% block acxselection_definition_edit %} +
+ {{- form_label(form.fieldSettings.choices_entry) -}} + {{- form_errors(form.fieldSettings.choices_entry) -}} + {{- form_widget(form.fieldSettings.choices_entry) -}} +
+
+ {{- form_label(form.fieldSettings.template) -}} + {{- form_errors(form.fieldSettings.template) -}} + {{- form_widget(form.fieldSettings.template) -}} +
+{% endblock %} \ No newline at end of file diff --git a/components/IbexaFieldTypes/bundle/Service/SelectionInterface.php b/components/IbexaFieldTypes/bundle/Service/SelectionInterface.php new file mode 100644 index 000000000..3199c7874 --- /dev/null +++ b/components/IbexaFieldTypes/bundle/Service/SelectionInterface.php @@ -0,0 +1,8 @@ +configResolver->getParameter('acx_selection', 'acx_field_types')[$choiceEntry]?? []); + } +} \ No newline at end of file diff --git a/components/IbexaFieldTypes/ci-config.yaml b/components/IbexaFieldTypes/ci-config.yaml new file mode 100644 index 000000000..fb3cee709 --- /dev/null +++ b/components/IbexaFieldTypes/ci-config.yaml @@ -0,0 +1,3 @@ +install: true +test: false +repo: Novactive/Ibexa-FieldTypes diff --git a/components/IbexaFieldTypes/composer.json b/components/IbexaFieldTypes/composer.json new file mode 100644 index 000000000..1d4eea120 --- /dev/null +++ b/components/IbexaFieldTypes/composer.json @@ -0,0 +1,20 @@ +{ + "name": "almaviacx/ibexa-field-types", + "description": "Ibexa Bundle that add some ibexa fieldtypes", + "type": "ibexa-bundle", + "require": { + "php": ">=8.1" + }, + "license": "MIT", + "autoload": { + "psr-4": { + "AlmaviaCX\\Ibexa\\Bundle\\FieldTypes\\": "bundle/" + } + }, + "authors": [ + { + "name": "Ousmane KANTE", + "email": "ousmane.kante@almaviacx.com" + } + ] +} From 85146dfe3cb39ccf0422dce35dbc8a53dcc40bba Mon Sep 17 00:00:00 2001 From: Ousmane KANTE Date: Tue, 14 May 2024 16:35:27 +0200 Subject: [PATCH 2/4] Code clean --- components/IbexaFieldTypes/.gitignore | 1 - .../bundle/AlmaviaCXFieldTypesBundle.php | 1 + .../AlmaviaCXFieldTypesExtension.php | 12 ++---------- .../Form/AcxSelectionSettingsType.php | 5 +++-- .../AcxSelection/Form/AcxSelectionType.php | 3 +-- .../AcxSelection/Form/FieldValueTransformer.php | 4 +--- .../bundle/FieldType/AcxSelection/Type.php | 16 ++++++++-------- .../bundle/FieldType/AcxSelection/Value.php | 2 +- .../bundle/Service/SelectionInterface.php | 2 +- .../bundle/Service/SelectionService.php | 2 +- 10 files changed, 19 insertions(+), 29 deletions(-) delete mode 100644 components/IbexaFieldTypes/.gitignore diff --git a/components/IbexaFieldTypes/.gitignore b/components/IbexaFieldTypes/.gitignore deleted file mode 100644 index 57872d0f1..000000000 --- a/components/IbexaFieldTypes/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/vendor/ diff --git a/components/IbexaFieldTypes/bundle/AlmaviaCXFieldTypesBundle.php b/components/IbexaFieldTypes/bundle/AlmaviaCXFieldTypesBundle.php index 32224ad8f..26c5fb16e 100644 --- a/components/IbexaFieldTypes/bundle/AlmaviaCXFieldTypesBundle.php +++ b/components/IbexaFieldTypes/bundle/AlmaviaCXFieldTypesBundle.php @@ -1,4 +1,5 @@ getConfiguration($configs, $container); $config = $this->processConfiguration($configuration, $configs); $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $loader->load('default_settings.yaml'); - $processor = new ConfigurationProcessor($container, Configuration::CONFIG_RESOLVER_NAMESPACE ); + $processor = new ConfigurationProcessor($container, Configuration::CONFIG_RESOLVER_NAMESPACE); $processor->mapConfigArray('acx_selection', $config, ContextualizerInterface::MERGE_FROM_SECOND_LEVEL); $loader->load('services.yaml'); - - - } public function prepend(ContainerBuilder $container): void @@ -48,4 +40,4 @@ public function prepend(ContainerBuilder $container): void $container->prependExtensionConfig('ibexa', Yaml::parse(file_get_contents($configFile))); $container->addResource(new FileResource($configFile)); } -} \ No newline at end of file +} diff --git a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/AcxSelectionSettingsType.php b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/AcxSelectionSettingsType.php index 8eff216ea..175a060c2 100644 --- a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/AcxSelectionSettingsType.php +++ b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/AcxSelectionSettingsType.php @@ -17,8 +17,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void [ 'required' => false, 'label' => 'Multiple', - ]); + ] + ); $builder->add('choices_entry', TextType::class); $builder->add('template', TextType::class); } -} \ No newline at end of file +} diff --git a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/AcxSelectionType.php b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/AcxSelectionType.php index dc26b8968..b080907e0 100644 --- a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/AcxSelectionType.php +++ b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/AcxSelectionType.php @@ -38,5 +38,4 @@ public function configureOptions(OptionsResolver $resolver): void 'choices' => [], ]); } - -} \ No newline at end of file +} diff --git a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/FieldValueTransformer.php b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/FieldValueTransformer.php index 712cfe37e..d0991361d 100644 --- a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/FieldValueTransformer.php +++ b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/FieldValueTransformer.php @@ -4,7 +4,6 @@ namespace AlmaviaCX\Ibexa\Bundle\FieldTypes\FieldType\AcxSelection\Form; - use AlmaviaCX\Ibexa\Bundle\FieldTypes\FieldType\AcxSelection\Value; use Symfony\Component\Form\DataTransformerInterface; @@ -12,14 +11,13 @@ final class FieldValueTransformer implements DataTransformerInterface { public function __construct(protected bool $isMultiple = false) { - } public function transform($value) { if (!$value instanceof Value) { return null; } - $selection = (array)($value->selection ?? []); + $selection = (array) ($value->selection ?? []); return $this->isMultiple === true ? $selection : ($selection[0] ?? null); } diff --git a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Type.php b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Type.php index db37a5bbf..7cae976c6 100644 --- a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Type.php +++ b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Type.php @@ -20,7 +20,7 @@ use Ibexa\Contracts\Core\Search; use Symfony\Component\Validator\Validator\ValidatorInterface; -class Type extends GenericType implements FieldValueFormMapperInterface, FieldDefinitionFormMapperInterface, Indexable +class Type extends GenericType implements FieldValueFormMapperInterface, FieldDefinitionFormMapperInterface, Indexable { public const IDENTIFIER = 'acxselection'; @@ -54,7 +54,7 @@ public function getSettingsSchema(): array public function fromHash($hash): Value { - return new Value((array)$hash); + return new Value((array) $hash); } public function toHash(SPIValue $value): ?array @@ -64,7 +64,7 @@ public function toHash(SPIValue $value): ?array } return (array) $value->selection; } - + public function getName(SPIValue $value, FieldDefinition $fieldDefinition, string $languageCode): string { if (empty($value->selection)) { @@ -74,7 +74,7 @@ public function getName(SPIValue $value, FieldDefinition $fieldDefinition, strin $names = []; $fieldSettings = $fieldDefinition->getFieldSettings(); - $choices = array_flip($this->selectionService->getChoices((string)$fieldSettings['choices_entry'])); + $choices = array_flip($this->selectionService->getChoices((string) $fieldSettings['choices_entry'])); foreach ($value->selection as $selectedName) { $name = $choices[$selectedName]?? null; if ($name === null) { @@ -93,7 +93,7 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data): vo 'required' => $definition->isRequired, 'label' => $definition->getName(), 'multiple' => $fieldSettings['isMultiple']?? true, - 'choices' => $this->selectionService->getChoices((string)$fieldSettings['choices_entry']) + 'choices' => $this->selectionService->getChoices((string) $fieldSettings['choices_entry']) ]); } public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $data): void @@ -110,8 +110,8 @@ public function isSearchable(): bool public function getIndexData(Field $field, SPIFieldDefinition $fieldDefinition): array { $fieldValue = $field->value->data??[]; - $fieldValue = (string)reset($fieldValue); - $values = (array)$field->value->data; + $fieldValue = (string) reset($fieldValue); + $values = (array) $field->value->data; return [ new Search\Field( 'value', @@ -148,4 +148,4 @@ public function getDefaultSortField(): string { return $this->getDefaultMatchField(); } -} \ No newline at end of file +} diff --git a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Value.php b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Value.php index 24855f8ea..bffc175f2 100644 --- a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Value.php +++ b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Value.php @@ -21,4 +21,4 @@ public function __toString() { return implode(',', $this->selection); } -} \ No newline at end of file +} diff --git a/components/IbexaFieldTypes/bundle/Service/SelectionInterface.php b/components/IbexaFieldTypes/bundle/Service/SelectionInterface.php index 3199c7874..5d6cb2680 100644 --- a/components/IbexaFieldTypes/bundle/Service/SelectionInterface.php +++ b/components/IbexaFieldTypes/bundle/Service/SelectionInterface.php @@ -5,4 +5,4 @@ interface SelectionInterface { public function getChoices(?string $choiceEntry): array; -} \ No newline at end of file +} diff --git a/components/IbexaFieldTypes/bundle/Service/SelectionService.php b/components/IbexaFieldTypes/bundle/Service/SelectionService.php index e264ebccb..5cd59af3b 100644 --- a/components/IbexaFieldTypes/bundle/Service/SelectionService.php +++ b/components/IbexaFieldTypes/bundle/Service/SelectionService.php @@ -17,4 +17,4 @@ public function getChoices(?string $choiceEntry): array } return (array) ($this->configResolver->getParameter('acx_selection', 'acx_field_types')[$choiceEntry]?? []); } -} \ No newline at end of file +} From 1526fc9ca1fab62d077dd3341acd22f21fd0e0a3 Mon Sep 17 00:00:00 2001 From: Ousmane KANTE Date: Tue, 14 May 2024 16:40:45 +0200 Subject: [PATCH 3/4] ReadMe --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index eed0941a0..36f88fd4b 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ Please comply with `make codeclean` and `make tests` before to push, your PR won | [SolrSearchExtraBundle](https://github.com/Novactive/NovaeZSolrSearchExtraBundle): Solr search handler additions. It adds many things, binary file plain text content indexation, fullText criterion, custom field configuration, exact matches boosting configuration, etc. | ![eZ-Platform-2.x-OK] ![eZ-Platform-3.x-UNSURE] ![Ibexa-3.3.x-UNSURE] | ![MIT] | | [StaticTemplatesBundle](https://github.com/Novactive/NovaeZStaticTemplatesBundle): Render twig templates via their paths through the design engine mechanism. Simple and perfect tiny bundle to build your Front-end first using Twig. | ![eZ-Platform-2.x-OK] ![eZ-Platform-3.x-OK] ![Ibexa-3.3.x-OK] ![Ibexa-4.x-OK] | ![MIT] | | [TranslationUiBundle](https://github.com/Novactive/AlmaviaCXIbexaTranslationUiBundle): This bundle allows to import translation files content into the database and provide a GUI to edit translations. | ![Ibexa-4.x-OK] | ![MIT] | +| [SélectionFieldType](https://github.com/Novactive/Ibexa-FieldTypes): Enhanced Ibexa sélection type for Ibexa: possibility to add configure selection option by yaml. | ![Ibexa-4.x-OK] | ![MIT] | ## For Maintainers From 9f22277371da6c1449836c69d588201472a3422c Mon Sep 17 00:00:00 2001 From: Ousmane KANTE Date: Tue, 14 May 2024 16:45:28 +0200 Subject: [PATCH 4/4] Code clean --- .../AlmaviaCXFieldTypesExtension.php | 6 ++-- .../DependencyInjection/Configuration.php | 1 + .../Form/FieldValueTransformer.php | 6 ++-- .../bundle/FieldType/AcxSelection/Type.php | 30 ++++++++++++------- .../bundle/FieldType/AcxSelection/Value.php | 1 + .../bundle/Service/SelectionService.php | 3 +- 6 files changed, 30 insertions(+), 17 deletions(-) diff --git a/components/IbexaFieldTypes/bundle/DependencyInjection/AlmaviaCXFieldTypesExtension.php b/components/IbexaFieldTypes/bundle/DependencyInjection/AlmaviaCXFieldTypesExtension.php index 0d15a1103..2ffd3c2f1 100644 --- a/components/IbexaFieldTypes/bundle/DependencyInjection/AlmaviaCXFieldTypesExtension.php +++ b/components/IbexaFieldTypes/bundle/DependencyInjection/AlmaviaCXFieldTypesExtension.php @@ -20,23 +20,23 @@ public function getAlias(): string { return Configuration::CONFIG_RESOLVER_NAMESPACE; } + public function load(array $configs, ContainerBuilder $container): void { $configuration = $this->getConfiguration($configs, $container); $config = $this->processConfiguration($configuration, $configs); - $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); + $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('default_settings.yaml'); $processor = new ConfigurationProcessor($container, Configuration::CONFIG_RESOLVER_NAMESPACE); $processor->mapConfigArray('acx_selection', $config, ContextualizerInterface::MERGE_FROM_SECOND_LEVEL); - $loader->load('services.yaml'); } public function prepend(ContainerBuilder $container): void { - $configFile = __DIR__ . '/../Resources/config/field_types_templates.yaml'; + $configFile = __DIR__.'/../Resources/config/field_types_templates.yaml'; $container->prependExtensionConfig('ibexa', Yaml::parse(file_get_contents($configFile))); $container->addResource(new FileResource($configFile)); } diff --git a/components/IbexaFieldTypes/bundle/DependencyInjection/Configuration.php b/components/IbexaFieldTypes/bundle/DependencyInjection/Configuration.php index 4dae039a3..8ecd66fac 100644 --- a/components/IbexaFieldTypes/bundle/DependencyInjection/Configuration.php +++ b/components/IbexaFieldTypes/bundle/DependencyInjection/Configuration.php @@ -8,6 +8,7 @@ class Configuration extends SAConfiguration { public const CONFIG_RESOLVER_NAMESPACE = 'acx_field_types'; + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder(self::CONFIG_RESOLVER_NAMESPACE); diff --git a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/FieldValueTransformer.php b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/FieldValueTransformer.php index d0991361d..02dd1e90c 100644 --- a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/FieldValueTransformer.php +++ b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Form/FieldValueTransformer.php @@ -12,6 +12,7 @@ final class FieldValueTransformer implements DataTransformerInterface public function __construct(protected bool $isMultiple = false) { } + public function transform($value) { if (!$value instanceof Value) { @@ -19,12 +20,13 @@ public function transform($value) } $selection = (array) ($value->selection ?? []); - return $this->isMultiple === true ? $selection : ($selection[0] ?? null); + return true === $this->isMultiple ? $selection : ($selection[0] ?? null); } public function reverseTransform($value): ?Value { $value = (array) $value; - return new Value($this->isMultiple === false ? [reset($value)]: $value); + + return new Value(false === $this->isMultiple ? [reset($value)] : $value); } } diff --git a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Type.php b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Type.php index 7cae976c6..27fa0ade3 100644 --- a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Type.php +++ b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Type.php @@ -10,23 +10,25 @@ use Ibexa\Contracts\ContentForms\Data\Content\FieldData; use Ibexa\Contracts\ContentForms\FieldType\FieldValueFormMapperInterface; use Ibexa\Contracts\Core\FieldType\Generic\Type as GenericType; -use Ibexa\Contracts\Core\FieldType\Value as SPIValue; use Ibexa\Contracts\Core\FieldType\Indexable; +use Ibexa\Contracts\Core\FieldType\Value as SPIValue; use Ibexa\Contracts\Core\FieldType\ValueSerializerInterface; use Ibexa\Contracts\Core\Persistence\Content\Field; use Ibexa\Contracts\Core\Persistence\Content\Type\FieldDefinition as SPIFieldDefinition; use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition; -use Symfony\Component\Form\FormInterface; use Ibexa\Contracts\Core\Search; +use Symfony\Component\Form\FormInterface; use Symfony\Component\Validator\Validator\ValidatorInterface; class Type extends GenericType implements FieldValueFormMapperInterface, FieldDefinitionFormMapperInterface, Indexable { public const IDENTIFIER = 'acxselection'; - - public function __construct(ValueSerializerInterface $serializer, ValidatorInterface $validator, private readonly SelectionInterface $selectionService) - { + public function __construct( + ValueSerializerInterface $serializer, + ValidatorInterface $validator, + private readonly SelectionInterface $selectionService + ) { parent::__construct($serializer, $validator); } @@ -34,6 +36,7 @@ public function getFieldTypeIdentifier(): string { return self::IDENTIFIER; } + public function getSettingsSchema(): array { return [ @@ -62,6 +65,7 @@ public function toHash(SPIValue $value): ?array if (!($value instanceof Value)) { return []; } + return (array) $value->selection; } @@ -76,12 +80,13 @@ public function getName(SPIValue $value, FieldDefinition $fieldDefinition, strin $choices = array_flip($this->selectionService->getChoices((string) $fieldSettings['choices_entry'])); foreach ($value->selection as $selectedName) { - $name = $choices[$selectedName]?? null; - if ($name === null) { + $name = $choices[$selectedName] ?? null; + if (null === $name) { continue; } - $names[]= $name; + $names[] = $name; } + return implode(' ', array_filter($names)); } @@ -92,16 +97,18 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data): vo $fieldForm->add('value', AcxSelectionType::class, [ 'required' => $definition->isRequired, 'label' => $definition->getName(), - 'multiple' => $fieldSettings['isMultiple']?? true, - 'choices' => $this->selectionService->getChoices((string) $fieldSettings['choices_entry']) + 'multiple' => $fieldSettings['isMultiple'] ?? true, + 'choices' => $this->selectionService->getChoices((string) $fieldSettings['choices_entry']), ]); } + public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $data): void { $fieldDefinitionForm->add('fieldSettings', AcxSelectionSettingsType::class, [ 'label' => false, ]); } + public function isSearchable(): bool { return true; @@ -109,9 +116,10 @@ public function isSearchable(): bool public function getIndexData(Field $field, SPIFieldDefinition $fieldDefinition): array { - $fieldValue = $field->value->data??[]; + $fieldValue = $field->value->data ?? []; $fieldValue = (string) reset($fieldValue); $values = (array) $field->value->data; + return [ new Search\Field( 'value', diff --git a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Value.php b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Value.php index bffc175f2..a2ce9dee2 100644 --- a/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Value.php +++ b/components/IbexaFieldTypes/bundle/FieldType/AcxSelection/Value.php @@ -7,6 +7,7 @@ class Value implements ValueInterface { public array $selection = []; + public function __construct(array $selection = []) { $this->selection = $selection; diff --git a/components/IbexaFieldTypes/bundle/Service/SelectionService.php b/components/IbexaFieldTypes/bundle/Service/SelectionService.php index 5cd59af3b..30925ee2e 100644 --- a/components/IbexaFieldTypes/bundle/Service/SelectionService.php +++ b/components/IbexaFieldTypes/bundle/Service/SelectionService.php @@ -15,6 +15,7 @@ public function getChoices(?string $choiceEntry): array if (empty($choiceEntry)) { $choiceEntry = 'default'; } - return (array) ($this->configResolver->getParameter('acx_selection', 'acx_field_types')[$choiceEntry]?? []); + + return (array) ($this->configResolver->getParameter('acx_selection', 'acx_field_types')[$choiceEntry] ?? []); } }