diff --git a/Form/FieldTypeHandler/Time.php b/Form/FieldTypeHandler/Time.php new file mode 100644 index 0000000..42a75cd --- /dev/null +++ b/Form/FieldTypeHandler/Time.php @@ -0,0 +1,75 @@ +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 ); + } +} diff --git a/README.md b/README.md index 679ce61..aebb662 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Currently supported FieldTypes: | Selection | yes | TextBlock | yes | TextLine | yes -| Time | no +| Time | yes | Url | yes | User | yes | XmlText | no diff --git a/Resources/config/form_fieldtype_handlers.yml b/Resources/config/form_fieldtype_handlers.yml index 9edcc4b..3959a67 100644 --- a/Resources/config/form_fieldtype_handlers.yml +++ b/Resources/config/form_fieldtype_handlers.yml @@ -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: @@ -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}