|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Form\Type; |
| 4 | + |
| 5 | +use App\Manager\CallManager; |
| 6 | +use Symfony\Component\Form\AbstractType; |
| 7 | +use Symfony\Component\Form\FormBuilderInterface; |
| 8 | +use Symfony\Component\Form\Extension\Core\Type\TextType; |
| 9 | +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
| 10 | +use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
| 11 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 12 | + |
| 13 | +class ElasticsearchTemplateFilterType extends AbstractType |
| 14 | +{ |
| 15 | + public function __construct(CallManager $callManager) |
| 16 | + { |
| 17 | + $this->callManager = $callManager; |
| 18 | + } |
| 19 | + |
| 20 | + public function buildForm(FormBuilderInterface $builder, array $options) |
| 21 | + { |
| 22 | + $builder->setMethod('GET'); |
| 23 | + |
| 24 | + $fields = []; |
| 25 | + |
| 26 | + $fields[] = 'name'; |
| 27 | + if ('index_template' == $options['context'] && true === $this->callManager->hasFeature('data_streams')) { |
| 28 | + $fields[] = 'data_stream'; |
| 29 | + } |
| 30 | + if ('index_template_legacy' != $options['context']) { |
| 31 | + $fields[] = 'managed'; |
| 32 | + } |
| 33 | + $fields[] = 'sort'; |
| 34 | + $fields[] = 'page'; |
| 35 | + |
| 36 | + foreach ($fields as $field) { |
| 37 | + switch ($field) { |
| 38 | + case 'name': |
| 39 | + $builder->add('name', TextType::class, [ |
| 40 | + 'label' => 'name', |
| 41 | + 'required' => false, |
| 42 | + 'attr' => [ |
| 43 | + 'data-break-after' => 'yes', |
| 44 | + ], |
| 45 | + ]); |
| 46 | + break; |
| 47 | + case 'data_stream': |
| 48 | + $builder->add('data_stream', ChoiceType::class, [ |
| 49 | + 'placeholder' => '-', |
| 50 | + 'choices' => $options['question'], |
| 51 | + 'choice_label' => function ($choice, $key, $value) use ($options) { |
| 52 | + return $options['question'][$key]; |
| 53 | + }, |
| 54 | + 'label' => 'data_stream', |
| 55 | + 'required' => false, |
| 56 | + 'attr' => [ |
| 57 | + 'data-break-after' => 'yes', |
| 58 | + ], |
| 59 | + ]); |
| 60 | + break; |
| 61 | + case 'managed': |
| 62 | + $builder->add('managed', ChoiceType::class, [ |
| 63 | + 'placeholder' => '-', |
| 64 | + 'choices' => $options['question'], |
| 65 | + 'choice_label' => function ($choice, $key, $value) use ($options) { |
| 66 | + return $options['question'][$key]; |
| 67 | + }, |
| 68 | + 'label' => 'managed', |
| 69 | + 'required' => false, |
| 70 | + 'attr' => [ |
| 71 | + 'data-break-after' => 'yes', |
| 72 | + ], |
| 73 | + ]); |
| 74 | + break; |
| 75 | + case 'sort': |
| 76 | + $builder->add('sort', HiddenType::class, [ |
| 77 | + 'label' => 'sort', |
| 78 | + 'required' => false, |
| 79 | + ]); |
| 80 | + break; |
| 81 | + case 'page': |
| 82 | + $builder->add('page', HiddenType::class, [ |
| 83 | + 'label' => 'page', |
| 84 | + 'required' => false, |
| 85 | + ]); |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public function configureOptions(OptionsResolver $resolver) |
| 92 | + { |
| 93 | + $resolver->setDefaults([ |
| 94 | + 'csrf_protection' => false, |
| 95 | + 'question' => ['yes', 'no'], |
| 96 | + 'context' => null, |
| 97 | + ]); |
| 98 | + } |
| 99 | + |
| 100 | + public function getBlockPrefix() |
| 101 | + { |
| 102 | + return ''; |
| 103 | + } |
| 104 | +} |
0 commit comments