Skip to content

Commit

Permalink
Country FieldTypeHandler implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Blažek committed Jul 27, 2015
1 parent 83c02b0 commit e5b967d
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 1 deletion.
117 changes: 117 additions & 0 deletions Form/FieldTypeHandler/Country.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler;

use eZ\Publish\SPI\FieldType\Value;
use Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
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\Country\Value as CountryValue;

class Country extends FieldTypeHandler
{
/**
* Country codes
*
* @var array
*/
protected $countryData;

/**
* Removed redundant data from array
*
* @var array
*/
protected $filteredCountryData;

/**
* Constructor
* Set information data from Service Container for countries
*
* @param array $countryData
*/
public function __construct( $countryData )
{
$this->countryData = $countryData;

foreach( $countryData as $countryCode => $country )
{
$this->filteredCountryData[$countryCode] = $country['Name'];
}
}

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

$isMultiple = $fieldDefinition->getFieldSettings()["isMultiple"];

$options['required'] = $fieldDefinition->isRequired;
$options['expanded'] = false;
$options['multiple'] = false;

if ( $isMultiple )
{
$options['multiple'] = true;
}

$options['choice_list'] = new ChoiceList(
array_keys( $this->filteredCountryData ),
array_values( $this->filteredCountryData )
);


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

/**
* {@inheritDoc}
*
* @return array
*/
public function convertFieldValueToForm( Value $value )
{
return $value->countries;
}

/**
* {@inheritDoc}
*
* @return CountryValue;
*/
public function convertFieldValueFromForm( $data )
{
$country = array();

// case if multiple is true
if ( is_array( $data ) )
{
foreach ( $data as $countryCode )
{
if ( array_key_exists( $countryCode, $this->countryData ) )
{
$country[$countryCode] = $this->countryData[$countryCode];
}
}
}
else
{
if ( array_key_exists( $data, $this->countryData ) )
{
$country[$data] = $this->countryData[$data];
}
}

return new CountryValue( (array)$country );
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Currently supported FieldTypes:
| Author | no
| BinaryFile | no
| Checkbox | yes
| Country | no
| Country | yes
| Date | yes
| DateAndTime | yes
| EmailAddress | yes
Expand Down
9 changes: 9 additions & 0 deletions Resources/config/form_fieldtype_handlers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ parameters:
netgen.ezforms.form.fieldtype_handler.ezinteger.class: Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler\Integer
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


services:
netgen.ezforms.form.fieldtype_handler.ezimage:
Expand Down Expand Up @@ -72,3 +74,10 @@ services:
class: %netgen.ezforms.form.fieldtype_handler.ezurl.class%
tags:
- {name: netgen.ezforms.form.fieldtype_handler, alias: ezurl}

netgen.ezforms.form.fieldtype_handler.ezcountry:
class: %netgen.ezforms.form.fieldtype_handler.ezcountry.class%
arguments:
- '%ezpublish.fieldType.ezcountry.data%'
tags:
- {name: netgen.ezforms.form.fieldtype_handler, alias: ezcountry}

0 comments on commit e5b967d

Please sign in to comment.