Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added: codebase for default refData values #9

Open
wants to merge 9 commits into
base: ak7.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions CHANGELOG-AK7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
## Added default Label & Image properties

He now have default properties that reflect Reference Entities...

You have to enable them to use them...

Here is an example with Color

```yaml
## grid example color
# */ReferenceDataBundle/Resources/config/datagrid/color.yml

datagrid:
color:
options:
entityHint: color
manageFilters: false
source:
type: pim_datasource_default
entity: Induxx\Bundle\ReferenceDataBundle\Entity\ReferenceIcon
repository_method: createDatagridQueryBuilder
columns:
code:
label: pim_custom_entity.form.tab.code.title
label:
label: pim_custom_entity.form.tab.label.title
filters:
columns:
code:
type: string
label: pim_custom_entity.form.tab.code.title
data_name: rd.code
label:
type: search
label: pim_custom_entity.form.tab.label.title
data_name: rd.label
sorters:
columns:
code:
data_name: rd.code
label:
data_name: rd.label
default:
code: '%oro_datagrid.extension.orm_sorter.class%::DIRECTION_ASC'
properties:
id: ~
edit_link:
type: url
route: pim_customentity_rest_get
params:
- id
- customEntityName
delete_link:
type: url
route: pim_customentity_rest_delete
params:
- id
- customEntityName
actions:
edit:
type: navigate
label: grid.action.label.edit
icon: edit
link: edit_link
rowAction: true
delete:
type: delete
label: grid.action.label.delete
icon: trash
link: delete_link
```

```yaml
## add label and image
# */ReferenceDataBundle/Resources/config/form_extensions/color/edit.yml
...
<project>-color-edit-form-properties-label:
module: pim/form/common/fields/text
parent: <project>-color-edit-form-properties-common
targetZone: content
position: 90
config:
fieldName: label
label: pim_custom_entity.form.tab.label.title

<project>-color-edit-form-properties-image:
module: referencedata/media-field
parent: <project>-color-edit-form-properties-common
targetZone: content
position: 130
config:
fieldName: image
label: pim_custom_entity.form.tab.image.title
required: false
readOnly: false
```

```yaml
# */ReferenceDataBundle/Resources/config/doctrine/Color.orm.yml
Induxx\Bundle\ReferenceDataBundle\Entity\Color:
type: entity
table: refdata_reference_color
changeTrackingPolicy: DEFERRED_EXPLICIT
repositoryClass: Pim\Bundle\CustomEntityBundle\Entity\Repository\CustomEntityRepository
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
code:
type: string
length: 255
nullable: false
unique: true
label:
type: string
length: 255
nullable: true
oneToOne:
image:
targetEntity: Akeneo\Tool\Component\FileStorage\Model\FileInfoInterface
joinColumn:
name: Image
referencedColumnName: id
onDelete: 'SET NULL'
cascade:
- persist
```
After this you will have to generate a custom DB Integration which is a Case by Case Scenario.
Some might already have and Image or a Label ...
67 changes: 67 additions & 0 deletions Entity/DefaultLocalizableLabelsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Pim\Bundle\CustomEntityBundle\Entity;

use Webmozart\Assert\Assert;

trait DefaultLocalizableLabelsTrait
{
private ?string $label = null;
private ?array $labels = null;

public function setLabels(array $labels): void
{
foreach ($labels as $locale => $content) {
$this->setLabel($locale, $content);
}
}

public function setLabel(string $locale, string $content = null): void
{
$this->pop();
Assert::stringNotEmpty($locale);

// Validate the locale code format
if (!preg_match('/^[a-z]{2}_[A-Z]{2}$/', $locale)) {
throw new InvalidArgumentException('Invalid locale code format. Expected format is xx_YY.');
}

if (empty($content)) {
unset($labels[$locale]);
return;
}

$this->labels[$locale] = $content;
}

public function getLabel(string $locale):? string
{
return $this->labels[$locale] ?? null;
}

public function getLabels(): array
{
return $this->labels;
}

private function unpop(): void
{
if ($this->labels === null) {
$this->labels = [];

if (is_string($this->label)) {
$this->labels = json_decode($this->label, true);
}
}
}

private function pop(): void
{
$this->label = json_encode($this->labels);
}

public function __destruct()
{
$this->pop();
}
}
34 changes: 34 additions & 0 deletions Entity/DefaultValuesTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Pim\Bundle\CustomEntityBundle\Entity;

use Akeneo\Channel\Infrastructure\Component\Model\Locale;
use Akeneo\Tool\Component\FileStorage\Model\FileInfoInterface;
use Induxx\Bundle\ReferenceDataBundle\Entity\ReferenceColorParent;
use Webmozart\Assert\Assert;

trait DefaultValuesTrait
{
private ?FileInfoInterface $image;
private ?string $label = null;

public function getLabel(): ?string
{
return $this->label;
}

public function setLabel(?string $label): void
{
$this->label = $label;
}

public function getImage(): ?FileInfoInterface
{
return $this->image;
}

public function setImage(?FileInfoInterface $image): void
{
$this->image = $image;
}
}
51 changes: 51 additions & 0 deletions Normalizer/Standard/DefaultValuesStandardNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Pim\Bundle\CustomEntityBundle\Normalizer\Standard;

use Akeneo\Tool\Component\FileStorage\Model\FileInfoInterface;
use Pim\Bundle\CustomEntityBundle\Entity\AbstractCustomEntity;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class DefaultValuesStandardNormalizer implements NormalizerInterface
{
/** @var array $supportedFormats */
protected $supportedFormats = ['standard'];

/**
* @param AbstractCustomEntity $entity
* @param null $format
* @param array $context
*
* @return array
*/
public function normalize($entity, $format = null, array $context = []): array
{
$normalizedEntity = [
'id' => $entity->getId(),
'code' => $entity->getCode(),
'image' => $this->getFileData($object->getImage()),
'label' => $entity->getLabel(),
];

return $normalizedEntity;
}

/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null): bool
{
return $data instanceof AbstractCustomEntity && in_array($format, $this->supportedFormats);
}

protected function getFileData(?FileInfoInterface $fileInfo): array
{
if (!$fileInfo) {
return [];
}
return [
'originalFilename' => $fileInfo->getOriginalFilename(),
'filePath' => $fileInfo->getKey()
];
}
}
3 changes: 3 additions & 0 deletions Resources/config/requirejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ config:
custom_entity/remover/reference-data: pimcustomentity/js/remover/reference-data-remover
oro/datagrid/delete-action: pimcustomentity/js/datagrid/action/delete-action

referencedata/media-field: pimcustomentity/js/field/media-field
referencedata/template/field/media-field: pimcustomentity/templates/field/media-field.html

config:
pim/fetcher-registry:
fetchers:
Expand Down
6 changes: 6 additions & 0 deletions Resources/config/serializer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ services:
tags:
- { name: pim_serializer.normalizer, priority: 150 }

pim_custom_entity.normalizer.standard.default:
public: false
class: Pim\Bundle\CustomEntityBundle\Normalizer\Standard\DefaultValuesStandardNormalizer
tags:
- { name: pim_serializer.normalizer, priority: 160 }

pim_custom_entity.normalizer.flat.reference_data:
public: false
class: '%pim_custom_entity.normalizer.flat.reference_data.class%'
Expand Down
Loading