Skip to content

Commit

Permalink
Merge pull request netgen#40 from tbuljevic/master
Browse files Browse the repository at this point in the history
Add a RelationList form field type handler
  • Loading branch information
MarioBlazek committed May 21, 2018
2 parents 2fd1ba2 + c4ba354 commit 11d7d57
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
113 changes: 113 additions & 0 deletions bundle/Form/FieldTypeHandler/RelationList.php
@@ -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);
}
}
8 changes: 8 additions & 0 deletions bundle/Resources/config/form_fieldtype_handlers.yml
Expand Up @@ -114,3 +114,11 @@ services:
- "@ezpublish.translation_helper"
tags:
- { name: netgen.ezforms.form.fieldtype_handler, alias: ezobjectrelation }

netgen.ezforms.form.fieldtype_handler.ezobjectrelationlist:
class: Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler\RelationList
arguments:
- "@ezpublish.api.repository"
- "@ezpublish.translation_helper"
tags:
- { name: netgen.ezforms.form.fieldtype_handler, alias: ezobjectrelationlist }

0 comments on commit 11d7d57

Please sign in to comment.