-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from MarioBlazek/time_fieldtype_handler
Time FieldTypeHandler implemented
- Loading branch information
Showing
3 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters