forked from ezsystems/repository-forms
/
FieldTypeChoiceType.php
87 lines (75 loc) · 2.44 KB
/
FieldTypeChoiceType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
* This file is part of the eZ RepositoryForms package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\RepositoryForms\Form\Type\ContentType;
use eZ\Publish\Core\Base\Container\ApiLoader\FieldTypeCollectionFactory;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
/**
* Form type for field type selection.
*/
class FieldTypeChoiceType extends AbstractType
{
/**
* @var eZ\Publish\Core\Base\Container\ApiLoader\FieldTypeCollectionFactory
*/
private $fieldTypeCollectionFactory;
/**
* @var Symfony\Component\Translation\TranslatorInterface
*/
private $translator;
public function __construct(FieldTypeCollectionFactory $fieldTypeCollectionFactory, TranslatorInterface $translator)
{
$this->fieldTypeCollectionFactory = $fieldTypeCollectionFactory;
$this->translator = $translator;
}
public function getBlockPrefix()
{
return 'ezrepoforms_contenttype_field_type_choice';
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'choices' => $this->getFieldTypeChoices(),
'choices_as_values' => true,
]);
}
public function getName()
{
return $this->getBlockPrefix();
}
public function getParent()
{
return ChoiceType::class;
}
/**
* Returns a hash, with fieldType identifiers as keys and human readable names as values.
*
* @return array
*/
private function getFieldTypeChoices()
{
$choices = [];
foreach ($this->fieldTypeCollectionFactory->getConcreteFieldTypesIdentifiers() as $fieldTypeIdentifier) {
$choices[$this->getFieldTypeLabel($fieldTypeIdentifier)] = $fieldTypeIdentifier;
}
ksort($choices, SORT_NATURAL);
return $choices;
}
/**
* Generate a human readable name for field type identifier.
*
* @param string $fieldTypeIdentifier
* @return string
*/
private function getFieldTypeLabel($fieldTypeIdentifier)
{
return $this->translator->trans(/** @Ignore */$fieldTypeIdentifier . '.name', [], 'fieldtypes');
}
}