-
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.
Country FieldTypeHandler implemented
- Loading branch information
Mario Blažek
committed
Jul 27, 2015
1 parent
83c02b0
commit e5b967d
Showing
3 changed files
with
127 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,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 ); | ||
} | ||
} |
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