This repository has been archived by the owner on Aug 14, 2019. It is now read-only.
forked from genemu/GenemuFormBundle
-
Notifications
You must be signed in to change notification settings - Fork 4
/
PlainType.php
91 lines (82 loc) · 2.71 KB
/
PlainType.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
88
89
90
91
<?php
namespace SymfonyHackers\Bundle\FormBundle\Form\Core\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\Extension\Core\Type\DateType;
/**
* A Form type that just renders the field as a p tag. This is useful for forms where certain field
* need to be shown but not editable.
*
* @author Adam Kuśmierz <adam@kusmierz.be>
*/
class PlainType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'widget' => 'field',
'read_only' => true,
'disabled' => true,
'date_format' => null,
'date_pattern' => null,
'time_format' => null,
'time_zone' => 'UTC',
'attr' => array(
'class' => $this->getName()
),
'compound' => false,
));
}
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$value = $form->getViewData();
// set string representation
if (true === $value) {
$value = 'true';
} elseif (false === $value) {
$value = 'false';
} elseif (null === $value) {
$value = 'null';
} elseif (is_array($value)) {
$value = implode(', ', $value);
} elseif ($value instanceof \DateTime) {
$dateFormat = is_int($options['date_format']) ? $options['date_format'] : DateType::DEFAULT_FORMAT;
$timeFormat = is_int($options['time_format']) ? $options['time_format'] : DateType::DEFAULT_FORMAT;
$calendar = \IntlDateFormatter::GREGORIAN;
$pattern = is_string($options['date_pattern']) ? $options['date_pattern'] : null;
$formatter = new \IntlDateFormatter(
\Locale::getDefault(),
$dateFormat,
$timeFormat,
$options['time_zone'],
$calendar,
$pattern
);
$formatter->setLenient(false);
$value = $formatter->format($value);
} elseif (is_object($value)) {
if (method_exists($value, '__toString')) {
$value = $value->__toString();
} else {
$value = get_class($value);
}
}
$view->vars['value'] = (string) $value;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'genemu_plain';
}
}