Skip to content

Commit d37ad5a

Browse files
authored
Merge pull request #13 from oliverde8/feature-#12/entity-config
Feature #12 - Added new config type for selecting Entities
2 parents f371b95 + b2c1a1a commit d37ad5a

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.1.0
2+
3+
- :star2: Added new Entity config type.
4+
15
# 1.0.0
26

37
- :confetti_ball: :tada: First stable release :tada: :confetti_ball:

Model/EntityConfig.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
4+
namespace oliverde8\ComfyBundle\Model;
5+
6+
7+
use Doctrine\ORM\EntityManagerInterface;
8+
use oliverde8\ComfyBundle\Manager\ConfigManagerInterface;
9+
use Symfony\Component\Validator\Validator\ValidatorInterface;
10+
11+
class EntityConfig extends TextConfig
12+
{
13+
private EntityManagerInterface $entityManager;
14+
15+
protected string $entity;
16+
17+
protected string $choiceLabel;
18+
19+
public function __construct(
20+
ConfigManagerInterface $configManager,
21+
ValidatorInterface $validator,
22+
EntityManagerInterface $entityManager,
23+
string $path,
24+
string $name,
25+
string $description = "",
26+
int $scope = PHP_INT_MAX,
27+
?string $defaultValue = null,
28+
bool $isHidden = false,
29+
string $entity = "",
30+
string $choiceLabel = "id"
31+
) {
32+
parent::__construct($configManager, $validator, $path, $name, $description, $scope, $defaultValue, $isHidden);
33+
34+
$this->entityManager = $entityManager;
35+
$this->entity = $entity;
36+
$this->choiceLabel = $choiceLabel;
37+
}
38+
39+
/**
40+
* Serialize value to save it in Database.
41+
*
42+
* @param $value
43+
*
44+
* @return string|null
45+
*/
46+
protected function serialize($value): ?string
47+
{
48+
if (is_object($value)) {
49+
return $value->getId();
50+
} else {
51+
return $value;
52+
}
53+
}
54+
55+
/**
56+
* @return string
57+
*/
58+
public function getEntity(): string
59+
{
60+
return $this->entity;
61+
}
62+
63+
/**
64+
* @return string
65+
*/
66+
public function getChoiceLabel(): string
67+
{
68+
return $this->choiceLabel;
69+
}
70+
71+
/**
72+
* Deserialize value read from database.
73+
*
74+
* @param string|null $value
75+
*
76+
* @return mixed|null
77+
*/
78+
protected function deserialize(?string $value)
79+
{
80+
if ($value) {
81+
return $this->entityManager->getRepository($this->entity)->find($value);
82+
} else {
83+
return null;
84+
}
85+
}
86+
}

Resolver/Form/EntityFormProvider.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace oliverde8\ComfyBundle\Resolver\Form;
4+
5+
use oliverde8\ComfyBundle\Exception\InvalidConfigTypeForFormProvider;
6+
use oliverde8\ComfyBundle\Model\ConfigInterface;
7+
use oliverde8\ComfyBundle\Model\EntityConfig;
8+
use oliverde8\ComfyBundle\Model\SelectConfig;
9+
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
10+
use Symfony\Component\Form\FormBuilderInterface;
11+
use Symfony\Component\Form\FormInterface;
12+
13+
class EntityFormProvider extends SimpleFormProvider
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
public function addTypeToForm(string $name, ConfigInterface $config, FormBuilderInterface $formBuilder, string $scope)
19+
{
20+
if (!($config instanceof EntityConfig)) {
21+
throw new InvalidConfigTypeForFormProvider(
22+
sprintf("Config is expected to be of type %s but is of type %s", SelectConfig::class, get_class($config))
23+
);
24+
}
25+
26+
$formBuilder->add(
27+
$name,
28+
$this->formType,
29+
[
30+
'label' => $config->getName(),
31+
'help' => $this->getHelpHtml($config, $scope),
32+
'data' => $config->get($scope),
33+
'class' => $config->getEntity(),
34+
'choice_label' => $config->getChoiceLabel(),
35+
],
36+
);
37+
}
38+
}

Resources/config/form-type-provider.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ services:
2828
$formType: 'Symfony\Component\Form\Extension\Core\Type\TextareaType'
2929
tags:
3030
- "comfy.form_provider"
31+
oliverde8.comfy_bundle.form_type_provider.entity:
32+
class: oliverde8\ComfyBundle\Resolver\Form\EntityFormProvider
33+
arguments:
34+
$configType: 'oliverde8\ComfyBundle\Model\EntityConfig'
35+
$formType: 'Symfony\Bridge\Doctrine\Form\Type\EntityType'
36+
tags:
37+
- "comfy.form_provider"
3138

3239
oliverde8.comfy_bundle.form_type_provider.default:
3340
class: oliverde8\ComfyBundle\Resolver\Form\SimpleFormProvider

docs/getting-started.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,19 @@ $this->myConfig->set($newValue, "default/en_GB");
157157
tags:
158158
- "comfy.config"
159159
```
160+
161+
#### Entity
162+
163+
```yaml
164+
myvendor.mybundle.comfy.generic.entity:
165+
class: oliverde8\ComfyBundle\Model\JsonConfig
166+
arguments:
167+
$path: "dummy/generic/entity"
168+
$name: "My Entity"
169+
$description: "The selected entity object"
170+
$defaultValue: '12' # can be null
171+
$entity: 'App/Entity/MyEntity'
172+
$choiceLabel: 'label' # Name of the label field.
173+
tags:
174+
- "comfy.config"
175+
```

0 commit comments

Comments
 (0)