Skip to content

Commit

Permalink
EZP-28218: Changes view data format to timestamp for DateTimeFieldType (
Browse files Browse the repository at this point in the history
  • Loading branch information
webhdx authored and Łukasz Serwatka committed Nov 17, 2017
1 parent 6fc18ce commit ac19c6e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
30 changes: 25 additions & 5 deletions lib/FieldType/DataTransformer/DateTimeValueTransformer.php
Expand Up @@ -9,9 +9,9 @@
*/
namespace EzSystems\RepositoryForms\FieldType\DataTransformer;

use DateTime;
use eZ\Publish\Core\FieldType\DateAndTime\Value;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

/**
* DataTransformer for DateAndTime\Value.
Expand All @@ -21,28 +21,48 @@ class DateTimeValueTransformer implements DataTransformerInterface
/**
* @param mixed $value
*
* @return DateTime|null
* @return int|null
*
* @throws TransformationFailedException
*/
public function transform($value)
{
if (null === $value) {
return null;
}

if (!$value instanceof Value) {
throw new TransformationFailedException(
sprintf('Expected a %s, got %s instead', Value::class, gettype($value))
);
}

if (null === $value->value) {
return null;
}

return $value->value;
return $value->value->getTimestamp();
}

/**
* @param mixed $value
*
* @return Value|null
*
* @throws TransformationFailedException
*/
public function reverseTransform($value)
{
if ($value === null || !$value instanceof DateTime) {
if (empty($value)) {
return null;
}

return new Value($value);
if (!is_numeric($value)) {
throw new TransformationFailedException(
sprintf('Expected a numeric, got %s instead', gettype($value))
);
}

return Value::fromTimestamp($value);
}
}
26 changes: 9 additions & 17 deletions lib/Form/Type/FieldType/DateTimeFieldType.php
Expand Up @@ -7,8 +7,10 @@

use EzSystems\RepositoryForms\FieldType\DataTransformer\DateTimeValueTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -28,34 +30,24 @@ public function getBlockPrefix()

public function getParent()
{
return DateTimeType::class;
return IntegerType::class;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setAttributes($this->getAttributes($options))
->addModelTransformer(new DateTimeValueTransformer());
}

private function getAttributes(array $options)
public function buildView(FormView $view, FormInterface $form, array $options)
{
$attributes = [];

if ($options['with_seconds']) {
$attributes['step'] = 1;
}

return $attributes;
$view->vars['attr']['data-seconds'] = (int) $options['with_seconds'];
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'input' => 'datetime',
'date_widget' => 'single_text',
'time_widget' => 'single_text',
'html5' => false,
]);
$resolver
->setDefault('with_seconds', true)
->setAllowedTypes('with_seconds', 'bool');
}
}

0 comments on commit ac19c6e

Please sign in to comment.