Skip to content

Commit

Permalink
Merge pull request #14 from MarioBlazek/time_fieldtype_handler
Browse files Browse the repository at this point in the history
Time FieldTypeHandler implemented
  • Loading branch information
pspanja committed Jul 27, 2015
2 parents b8c4fec + 905c031 commit 0b0c36c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
75 changes: 75 additions & 0 deletions Form/FieldTypeHandler/Time.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler;

use eZ\Publish\SPI\FieldType\Value;
use Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler;
use Symfony\Component\Form\FormBuilderInterface;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\Core\FieldType\Time\Value as TimeValue;
use DateTime;
use Symfony\Component\Validator\Constraints as Assert;

class Time extends FieldTypeHandler
{
/**
* {@inheritdoc}
*/
protected function buildFieldForm(
FormBuilderInterface $formBuilder,
FieldDefinition $fieldDefinition,
$languageCode,
Content $content = null
)
{
$options = $this->getDefaultFieldOptions( $fieldDefinition, $languageCode, $content );

$useSeconds = $fieldDefinition->getFieldSettings()['useSeconds'];
$options['input'] = 'datetime';
$options['widget'] = 'choice';
$options['with_seconds'] = $useSeconds;
$options['required'] = $fieldDefinition->isRequired;
$options['constraints'] = array(
new Assert\Time(),
);

$formBuilder->add( $fieldDefinition->identifier, 'time', $options );
}

/**
* {@inheritDoc}
*
* @return int|null
*/
public function convertFieldValueToForm( Value $value )
{
$time = $value->time;
if ( is_int( $time ) )
{
return new DateTime( "@$time" );
}

return new DateTime();
}

/**
* {@inheritDoc}
*
* @return TimeValue
*/
public function convertFieldValueFromForm( $data )
{
if ( $data instanceof DateTime )
{
return TimeValue::fromDateTime( $data );
}

if ( is_int( $data ) )
{
return new TimeValue( $data );
}

return new TimeValue( null );
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Currently supported FieldTypes:
| Selection | yes
| TextBlock | yes
| TextLine | yes
| Time | no
| Time | yes
| Url | yes
| User | yes
| XmlText | no
6 changes: 6 additions & 0 deletions Resources/config/form_fieldtype_handlers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ parameters:
netgen.ezforms.form.fieldtype_handler.ezfloat.class: Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler\Float
netgen.ezforms.form.fieldtype_handler.ezurl.class: Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler\Url
netgen.ezforms.form.fieldtype_handler.ezcountry.class: Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler\Country
netgen.ezforms.form.fieldtype_handler.eztime.class: Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler\Time


services:
Expand Down Expand Up @@ -81,3 +82,8 @@ services:
- '%ezpublish.fieldType.ezcountry.data%'
tags:
- {name: netgen.ezforms.form.fieldtype_handler, alias: ezcountry}

netgen.ezforms.form.fieldtype_handler.eztime:
class: %netgen.ezforms.form.fieldtype_handler.eztime.class%
tags:
- {name: netgen.ezforms.form.fieldtype_handler, alias: eztime}

0 comments on commit 0b0c36c

Please sign in to comment.