Skip to content
This repository was archived by the owner on Feb 13, 2023. It is now read-only.

Commit 0ce464b

Browse files
authored
Extended new attribute cookbook (#44)
1 parent 9acafb2 commit 0ce464b

File tree

1 file changed

+59
-26
lines changed

1 file changed

+59
-26
lines changed

docs/backend/cookbook/new_attribute.md

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ This file should live in `Your-module\Application\Form\Attribute\` namespace ext
1212
In Ergonode system we have several Abstract Attribute classes (you can find them in `Ergonode\Attribute\Domain\Entity\Attribute` namespace), it depends on what kind of attribute you want to create, you can either use one of already created or create a custom one.
1313

1414
Keep in mind that Abstract Attribute class should extend `\Ergonode\Attribute\Domain\Entity\AbstractAttribute`.
15+
1516
```php
16-
namespace Ergonode\Attribute\Domain\Entity\Attribute;
17+
namespace YourNameSpace\Domain\Entity\Attribute;
1718

1819
/**
1920
*/
@@ -22,7 +23,7 @@ class YourAttribute extends AbstractCollectionAttribute implements AttributeInte
2223
public const TYPE = 'YOUR_ATTRIBUTE';
2324

2425
/**
25-
* @JMS\virtualProperty();
26+
* @JMS\VirtualProperty();
2627
* @JMS\SerializedName("type")
2728
*
2829
* @return string
@@ -42,7 +43,7 @@ Create a command for creation `CreateYourAttributeCommand`.
4243
This file should live in `Your-module\Domain\Command\Attribute\Create` namespace and extends `Ergonode\Attribute\Domain\Command\Attribute\AbstractCreateAttributeCommand`.
4344

4445
```php
45-
namespace Ergonode\Attribute\Domain\Command\Attribute\Create;
46+
namespace YourNameSpace\Domain\Command\Attribute\Create;
4647

4748
/**
4849
*/
@@ -55,9 +56,10 @@ Each command needs to have handler, class that handles the logic represented by
5556

5657
Create a handler `CreateYourAttributeCommandHandler`.
5758
This file should live in `Your-module\Infrastructure\Handler\Attribute\Create` namespace.
59+
Remember to tag the handler with `messenger.message_handler`
5860

5961
```php
60-
namespace Ergonode\Attribute\Infrastructure\Handler\Attribute\Create;
62+
namespace YourNameSpace\Infrastructure\Handler\Attribute\Create;
6163

6264
/**
6365
*/
@@ -93,7 +95,7 @@ class CreateYourAttributeCommandHandler
9395
);
9496

9597
foreach ($command->getGroups() as $group) {
96-
$attribute->addGroup(new AttributeGroupId($group));
98+
$attribute->addGroup($group);
9799
}
98100

99101
$this->attributeRepository->save($attribute);
@@ -104,7 +106,7 @@ class CreateYourAttributeCommandHandler
104106
Create a command for update `UpdateYourAttributeCommand`.This file should live in `Your-module\Domain\Command\Attribute\Update` namespace and extends `Ergonode\Attribute\Domain\Command\Attribute\AbstractUpdateAttributeCommand`.
105107

106108
```php
107-
namespace Ergonode\Attribute\Domain\Command\Attribute\Update;
109+
namespace YourNameSpace\Domain\Command\Attribute\Update;
108110

109111
/**
110112
*/
@@ -114,9 +116,10 @@ class UpdateYourAttributeCommand extends AbstractUpdateAttributeCommand
114116
```
115117

116118
Create a handler `UpdateYourAttributeCommandHandler`.This file should live in `Your-module\Infrastructure\Handler\Attribute\Upadate` namespace.
119+
Remember to tag the handler with `messenger.message_handler`
117120

118121
```php
119-
namespace Ergonode\Attribute\Infrastructure\Handler\Attribute\Update;
122+
namespace YourNameSpace\Infrastructure\Handler\Attribute\Update;
120123

121124
/**
122125
*/
@@ -154,11 +157,11 @@ class UpdateYourAttributeCommandHandler extends AbstractUpdateAttributeCommandHa
154157

155158
## Command factories
156159

157-
Create a handler for creation command `CreateYourAttributeCommandFactory`.This file should live in `Your-module\Infrastructure\Factory\Command\Create` namespace and implements `Ergonode\Attribute\Infrastructure\Factory\Command\CreateAttributeCommandFactoryInterface`
158-
160+
Create a handler for creation command `CreateYourAttributeCommandFactory`.This file should live in `Your-module\Infrastructure\Factory\Command\Create` namespace and implements `Ergonode\Attribute\Infrastructure\Factory\Command\CreateAttributeCommandFactoryInterface`
161+
If autoconfiguration is not used remember to tag your service with `component.attribute.create_attribute_command_factory_interface`
159162

160163
```php
161-
namespace Ergonode\Attribute\Infrastructure\Factory\Command\Create;
164+
namespace YourNameSpace\Infrastructure\Factory\Command\Create;
162165

163166
/**
164167
*/
@@ -199,9 +202,10 @@ class CreateYourAttributeCommandFactory implements CreateAttributeCommandFactory
199202
```
200203

201204
Create a handler for updating command `UpdateYourAttributeCommandFactory`.This file should live in `Your-module\Infrastructure\Factory\Command\Update` namespace and implements `Ergonode\Attribute\Infrastructure\Factory\Command\UpdateAttributeCommandFactoryInterface`
205+
If autoconfiguration is not used remember to tag your service with `component.attribute.update_attribute_command_factory_interface`
202206

203207
```php
204-
namespace Ergonode\Attribute\Infrastructure\Factory\Command\Update;
208+
namespace YourNameSpace\Infrastructure\Factory\Command\Update;
205209

206210
/**
207211
*/
@@ -235,7 +239,7 @@ class UpdateYourAttributeCommandFactory implements UpdateAttributeCommandFactory
235239
new TranslatableString($data->hint),
236240
new TranslatableString($data->placeholder),
237241
new AttributeScope($data->scope),
238-
$data->groups,
242+
array_map(fn($group) => new AttributeGroupId($group), $data->groups),
239243
);
240244
}
241245
}
@@ -244,12 +248,11 @@ class UpdateYourAttributeCommandFactory implements UpdateAttributeCommandFactory
244248
## Value Constrain Strategy
245249

246250
To be able to validate values which are used in our new attribute `YourAttributeValueConstraintStrategy` needs to be created. This file should live in `Your-module\Infrastructure\Provider\Strategy` namespace and implements `Ergonode\Attribute\Infrastructure\Provider\AttributeValueConstraintStrategyInterface`
251+
If autoconfiguration is not used remember to tag your service with `component.attribute.attribute_validation_interface`
247252

248253
```php
249-
namespace Ergonode\Attribute\Infrastructure\Provider\Strategy;
254+
namespace YourNameSpace\Infrastructure\Provider\Strategy;
250255

251-
/**
252-
*/
253256
class YourAttributeValueConstraintStrategy implements AttributeValueConstraintStrategyInterface
254257
{
255258
/**
@@ -283,9 +286,10 @@ Create form class `YouAttributeForm`
283286
This file should live in `Your-module\Application\Form\Attribute` namespace and extends:
284287
* `Symfony\Component\Form\AbstractType`
285288
* `Ergonode\Attribute\Application\Form\Attribute\AttributeFormInterface`
289+
If autoconfiguration is not used remember to tag your service with `attribute.form.attribute_form_interface`
286290

287291
```php
288-
namespace Ergonode\Attribute\Application\Form\Attribute;
292+
namespace YourNameSpace\Application\Form\Attribute;
289293

290294
class YourAttributeForm extends AbstractType implements AttributeFormInterface
291295
{
@@ -358,12 +362,36 @@ class YourAttributeForm extends AbstractType implements AttributeFormInterface
358362

359363
If you want be able to use your attribute on a grid you need to create following classes.
360364

365+
366+
Create column class `YourAttributeColumn`
367+
368+
This file should live in `Your-module\Grid\Column` namespace and extends `\Ergonode\Grid\Column\AbstractColumn`
369+
370+
```php
371+
namespace YourNameSpace\Grid\Column;
372+
373+
/**
374+
*/
375+
class YourColumn extends AbstractColumn
376+
{
377+
public const TYPE = 'YOUR_ATTRIBUTE';
378+
379+
/**
380+
* {@inheritDoc}
381+
*/
382+
public function getType(): string
383+
{
384+
return self::TYPE;
385+
}
386+
}
387+
```
388+
361389
Create strategy class `YourAttributeColumnStrategy`
362390

363391
This file should live in `Your-module\Infrastructure\Grid\Column\Provider\Strategy` namespace and implements `Ergonode\Product\Infrastructure\Grid\Column\Provider\Strategy\AttributeColumnStrategyInterface`
364392

365393
```php
366-
namespace Ergonode\Product\Infrastructure\Grid\Column\Provider\Strategy;
394+
namespace YourNameSpace\Infrastructure\Grid\Column\Provider\Strategy;
367395

368396
/**
369397
*/
@@ -395,7 +423,7 @@ Create DataSet Builder class `YourAttributeDataSetQueryBuilder`
395423
This file should live in `Your-module\Infrastructure\Grid\Builder\Query` namespace and extends `Ergonode\Product\Infrastructure\Grid\Builder\Query\AbstractAttributeDataSetBuilder`
396424

397425
```php
398-
namespace Ergonode\Product\Infrastructure\Grid\Builder\Query;
426+
namespace YourNameSpace\Infrastructure\Grid\Builder\Query;
399427

400428
/**
401429
*/
@@ -411,25 +439,30 @@ class YourAttributeDataSetQueryBuilder extends AbstractAttributeDataSetBuilder
411439
}
412440
```
413441

414-
Create column class `YourAttributeColumn`
415-
416-
This file should live in `Your-module\Grid\Column` namespace and extends `\Ergonode\Grid\Column\AbstractColumn`
442+
Create renderer class `YourAttributeColumnRenderer`
417443

418444
```php
419-
namespace Ergonode\Grid\Column;
445+
namespace YourNameSpace\Infrastructure\Grid\Column\Renderer;
420446

421447
/**
422448
*/
423-
class YourColumn extends AbstractColumn
449+
class YourAttributeColumnRenderer extends \Ergonode\Grid\Column\Renderer\ColumnRendererInterface
424450
{
425-
public const TYPE = 'YOUR_ATTRIBUTE';
451+
/**
452+
* {@inheritDoc}
453+
*/
454+
public function supports(ColumnInterface $column): bool
455+
{
456+
return $column instanceof YourAttributeColumn;
457+
}
426458

427459
/**
428460
* {@inheritDoc}
429461
*/
430-
public function getType(): string
462+
463+
public function render(ColumnInterface $column, string $id, array $row)
431464
{
432-
return self::TYPE;
465+
// TODO return your column value
433466
}
434467
}
435468
```

0 commit comments

Comments
 (0)