akeneo-labs/custom-entity-bundle
## Getting Started
### Minimal Steps to First Use
1. **Install the Bundle**
```bash
composer require akeneo-labs/custom-entity-bundle:4.0.*
Enable in config/bundles.php:
Pim\Bundle\CustomEntityBundle\PimCustomEntityBundle::class => ['all' => true]
Add Routing
Append to config/routes/routes.yml:
pim_customentity:
prefix: /reference-data
resource: "@PimCustomEntityBundle/Resources/config/routing.yml"
Create a Basic Custom Entity
Extend AbstractCustomEntity and define Doctrine mapping:
// src/Acme/SupplierBundle/Entity/Supplier.php
namespace Acme\Bundle\SupplierBundle\Entity;
use Pim\Bundle\CustomEntityBundle\Entity\AbstractCustomEntity;
class Supplier extends AbstractCustomEntity {
protected $name;
// Getters/Setters...
}
# src/Acme/SupplierBundle/Resources/config/doctrine/Supplier.orm.yml
Acme\Bundle\SupplierBundle\Entity\Supplier:
type: entity
table: refdata_supplier
repositoryClass: Pim\Bundle\CustomEntityBundle\Entity\Repository\CustomEntityRepository
fields:
name: { type: string, length: 255, nullable: false }
Register the Entity
Declare in config/custom_entities.yml:
custom_entities:
supplier:
entity_class: Acme\Bundle\SupplierBundle\Entity\Supplier
Update Database
php bin/console doctrine:schema:update --force
Clear Cache & Rebuild Assets
php bin/console cache:clear --env=prod
yarn run webpack-dev
/reference-data/supplier (if menu is configured).AbstractCustomEntity for all custom entities (handles id and code fields automatically).CustomEntityRepository for all repositories to leverage built-in CRUD methods.NormalizerInterface for API responses (e.g., for REST exports).Menu Integration
Add a menu item in form_extensions/menu.yml:
extensions:
acme-menu-supplier:
module: pim/menu/item
parent: pim-menu-reference_data-navigation-block
config:
title: acme.menu.supplier
to: pim_customentity_index
routeParams: { customEntityName: supplier }
Datagrid Configuration
Define columns, filters, and sorting in datagrid/supplier.yml:
datagrid:
supplier:
options: { entityHint: supplier }
source:
type: pim_datasource_default
entity: Acme\Bundle\SupplierBundle\Entity\Supplier
repository_method: createDatagridQueryBuilder
columns:
code: { label: acme.supplier.code }
name: { label: acme.supplier.name }
filters:
columns:
name: { type: search, data_name: rd.name }
Form Extensions
index.yml).create.yml/edit.yml:
# create.yml
acme-supplier-create-form:
module: pim/form/standard
parent: pim-entity-create-form
targetZone: content
position: 100
config:
fields:
name: { type: text, options: { label: acme.supplier.name } }
Validation
Use Symfony’s validation YAML (e.g., validation.yml):
Acme\Bundle\SupplierBundle\Entity\Supplier:
properties:
name:
- NotBlank: ~
- Length: { max: 255 }
Reference Data as Attributes:
Create a "reference data simple select" attribute in Akeneo’s attribute configuration, targeting your custom entity (e.g., supplier).
# config/pim/catalog/attributes.yml
attributes:
supplier:
type: pim_catalog_reference_data_simple_select
reference_data_name: supplier
Mass Edits:
Use the csv_reference_data_quick_export job (created via CLI) to export/import custom entities in bulk:
php bin/console akeneo:batch:create-job "Supplier Export" "csv_reference_data_quick_export" "quick_export" "csv_reference_data_quick_export" '{"filePath": "/tmp/suppliers.csv"}'
NormalizerInterface for custom serialization:
use Pim\Bundle\CustomEntityBundle\Normalizer\NormalizerInterface;
class SupplierNormalizer implements NormalizerInterface {
public function normalize($entity, $format = null, array $context = []) {
return [
'code' => $entity->getCode(),
'name' => $entity->getName(),
];
}
}
services.yml:
services:
acme.supplier.normalizer:
class: Acme\Bundle\SupplierBundle\Normalizer\SupplierNormalizer
tags: [pim_custom_entity.normalizer]
custom_entity/field/custom-entity-select module to link entities:
# form_extensions/product_association.yml
acme-product-supplier-select:
module: custom_entity/field/custom-entity-select
parent: pim-entity-edit-form-properties-common
config:
fieldName: supplier
entityName: supplier
choiceNameField: name
choiceValueField: code
isCustomEntity: true
Cache Invalidation:
cache:clear --env=prod).yarn run webpack-dev) after routing/translation changes.Ctrl+F5) to see UI changes.Doctrine Schema Updates:
--dump-sql before running --force.refdata_{entity_name} (e.g., refdata_supplier).Menu Visibility:
customEntityName in routeParams matches the YAML key in custom_entities.yml.title is translated (check translations/messages.en.xlf).Form Extensions:
pim-entity-create-form for create forms, pim-entity-edit-form for edit forms.position values appear later in the form. Start with 100 for new fields.edit.yml form extension exists.Validation Errors:
validation.yml.NotBlank for required fields, but ensure the field exists in the entity.API Normalization:
pim_custom_entity.normalizer.normalize() method returns the expected structure.Mass Edit Jobs:
csv_reference_data_quick_export job must be created manually via CLI./tmp/suppliers.csv) must be writable by the web server.Check Logs:
php bin/console debug:config pim_custom_entity
Lists all registered custom entities and their configurations.
Datagrid Issues:
repository_method in datagrid.yml matches the repository’s method (e.g., createDatagridQueryBuilder).pim_datasource_default for standard queries.Form Rendering:
debug:router for routingHow can I help you explore Laravel packages today?