/
HostField.php
51 lines (43 loc) · 1.58 KB
/
HostField.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
<?php
namespace Pushword\Admin\FormField;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\DoctrineORMAdminBundle\Filter\ChoiceFilter;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
class HostField extends AbstractField
{
public function formField(FormMapper $form): FormMapper
{
if (1 === \count($this->admin->getApps()->getHosts())) {
$this->admin->getSubject()->setHost($this->admin->getApps()->get()->getMainHost());
return $form;
}
if ($this->admin instanceof FormMapper && null === $this->admin->getSubject()->getHost()) {
$this->admin->getSubject()->setHost($this->admin->getApps()->getMainHost());
}
return $form->add('host', ChoiceType::class, [
'choices' => \Safe\array_combine($this->getHosts(), $this->getHosts()),
'required' => false,
'label' => 'admin.page.host.label',
'empty_data' => $this->getHosts()[0],
]);
}
public function datagridMapper(DatagridMapper $datagrid): DatagridMapper
{
return $datagrid->add('host', ChoiceFilter::class, [
'field_type' => ChoiceType::class,
'field_options' => [
'choices' => \Safe\array_combine($this->getHosts(), $this->getHosts()),
'multiple' => true,
],
'label' => 'admin.page.host.label',
]);
}
/**
* @return string[]
*/
private function getHosts(): array
{
return $this->admin->getApps()->getHosts();
}
}