forked from netgen/NetgenEzFormsBundle
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 netgen#40 from tbuljevic/master
Add a RelationList form field type handler
- Loading branch information
Showing
2 changed files
with
121 additions
and
0 deletions.
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,113 @@ | ||
<?php | ||
|
||
namespace Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler; | ||
|
||
use eZ\Publish\API\Repository\Repository; | ||
use eZ\Publish\API\Repository\Values\Content\Content; | ||
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition; | ||
use eZ\Publish\SPI\FieldType\Value; | ||
use Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler; | ||
use eZ\Publish\Core\Helper\TranslationHelper; | ||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
class RelationList extends FieldTypeHandler | ||
{ | ||
const BROWSE = 0; | ||
const DROPDOWN = 1; | ||
const LIST_RADIO = 2; | ||
const LIST_CHECK = 3; | ||
const MULTIPLE_SELECTION = 4; | ||
const TPLBASED_MULTI = 5; | ||
const TPLBASED_SINGLE = 6; | ||
|
||
/** | ||
* @var Repository | ||
*/ | ||
private $repository; | ||
|
||
/** | ||
* @var TranslationHelper | ||
*/ | ||
private $translationHelper; | ||
|
||
public function __construct( | ||
Repository $repository, | ||
TranslationHelper $translationHelper | ||
) | ||
{ | ||
$this->repository = $repository; | ||
$this->translationHelper = $translationHelper; | ||
} | ||
|
||
protected function buildFieldForm( | ||
FormBuilderInterface $formBuilder, | ||
FieldDefinition $fieldDefinition, | ||
$languageCode, | ||
Content $content = null | ||
) | ||
{ | ||
$options = $this->getDefaultFieldOptions($fieldDefinition, $languageCode, $content); | ||
|
||
$fieldSettings = $fieldDefinition->getFieldSettings(); | ||
|
||
$selectionMethod = $fieldSettings['selectionMethod']; | ||
|
||
$defaultLocation = $fieldSettings['selectionDefaultLocation']; | ||
$contentTypes = $fieldSettings['selectionContentTypes']; | ||
|
||
/* TODO: implement different selection methods */ | ||
switch ($fieldSettings['selectionMethod']) { | ||
case self::MULTIPLE_SELECTION: | ||
$locationService = $this->repository->getLocationService(); | ||
$location = $locationService->loadLocation($defaultLocation ? $defaultLocation : 2); | ||
$locationList = $locationService->loadLocationChildren($location); | ||
|
||
$choices = []; | ||
foreach ($locationList->locations as $child) { | ||
/** @var Location $child */ | ||
$choices[$this->translationHelper->getTranslatedContentNameByContentInfo($child->contentInfo)] = $child->contentInfo->id; | ||
} | ||
|
||
$formBuilder->add($fieldDefinition->identifier, ChoiceType::class, [ | ||
'choices' => $choices, | ||
'expanded' => false, | ||
'multiple' => true, | ||
'choices_as_values' => true, | ||
], $options); | ||
break; | ||
default: | ||
$locationService = $this->repository->getLocationService(); | ||
$location = $locationService->loadLocation($defaultLocation ? $defaultLocation : 2); | ||
$locationList = $locationService->loadLocationChildren($location); | ||
|
||
$choices = []; | ||
foreach ($locationList->locations as $child) { | ||
/** @var Location $child */ | ||
$choices[$this->translationHelper->getTranslatedContentNameByContentInfo($child->contentInfo)] = $child->contentInfo->id; | ||
} | ||
|
||
$formBuilder->add($fieldDefinition->identifier, ChoiceType::class, [ | ||
'choices' => $choices, | ||
'expanded' => false, | ||
'multiple' => false, | ||
'choices_as_values' => true, | ||
], $options); | ||
break; | ||
} | ||
} | ||
|
||
public function convertFieldValueToForm(Value $value, FieldDefinition $fieldDefinition = null) | ||
{ | ||
if (empty($value->destinationContentIds)) { | ||
return null; | ||
} | ||
|
||
return $value->destinationContentIds; | ||
} | ||
|
||
public function convertFieldValueFromForm($data) | ||
{ | ||
return new RelationListValue($data); | ||
} | ||
} |
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