drugento/date-collection-type
Installation Run:
composer require --prefer-dist drugento/date-collection-type
Ensure Akeneo PIM Community Edition v1.7.x is installed.
Register the Bundle
Add to app/AppKernel.php:
new \Pim\Bundle\DateCollectionTypeBundle\PimDateCollectionTypeBundle(),
First Use Case Create a DateCollection attribute type in Akeneo:
validity_periods) and save.Verify UI The attribute will now appear in product forms with a date range picker (as shown in the README screenshot).
Attribute Definition
Use the DateCollection type for:
Example YAML (if extending via custom bundle):
pim_enrich:
attribute:
validity_periods:
type: pim_date_collection
label: "Validity Periods"
order: 100
Data Population
start/end dates:
{
"validity_periods": [
{"start": "2023-10-01", "end": "2023-12-31"},
{"start": "2024-01-15", "end": null} // Open-ended range
]
}
Querying Data Retrieve values in PHP:
$product = $productRepository->find($id);
$dateRanges = $product->getValidityPeriods(); // Returns array of DateRange objects
foreach ($dateRanges as $range) {
echo $range->getStart()->format('Y-m-d') . " to " . $range->getEnd()->format('Y-m-d');
}
Validation
start ≤ end (handled automatically by the UI/picker).use Pim\Bundle\DateCollectionTypeBundle\Validator\Constraints as DateCollectionAssert;
/**
* @DateCollectionAssert\DateCollection(
* maxRanges=5,
* message="Maximum 5 date ranges allowed."
* )
*/
Custom Templates: Override the Twig template for the date picker by copying:
vendor/drugento/date-collection-type/src/Resources/views/PimDateCollectionType/attribute.html.twig
to app/Resources/PimDateCollectionType/views/.
Export/Import: Use the pim_enrich:export and pim_enrich:import commands with the validity_periods field mapped to your CSV/JSON.
API Platform: If using API Platform, ensure the DateRange object is serialized correctly:
# config/packages/api_platform.yaml
api_platform:
formats:
jsonld:
mime_types: ['application/ld+json']
jsonld_context:
'@context': '/contexts/Product'
validity_periods:
'@type': 'array'
'@context': 'https://schema.org/Date'
Akeneo Version Lock
Date Format Mismatch
MM/DD/YYYY) may fail silently.Empty Ranges
start and end as null may cause errors.$validator = $this->get('validator');
$errors = $validator->validate($dateRanges, [
new \Pim\Bundle\DateCollectionTypeBundle\Validator\Constraints\DateCollection([
'allowEmptyRanges' => false,
]),
]);
UI Glitches
php bin/console cache:clear.use Symfony\Component\Debug\Debug;
Debug::dump($product->getValidityPeriods());
pim_catalog_value table:
SELECT * FROM pim_catalog_value
WHERE attribute_id = (SELECT id FROM pim_catalog_attribute WHERE code = 'validity_periods');
Custom Date Range Logic
Extend the DateRange class to add methods:
namespace App\Entity;
use Pim\Bundle\DateCollectionTypeBundle\Entity\DateRange as BaseDateRange;
class CustomDateRange extends BaseDateRange {
public function isOverlappingWith(BaseDateRange $other): bool {
return $this->getStart() <= $other->getEnd() && $this->getEnd() >= $other->getStart();
}
}
Register as a service:
services:
app.date_range.type:
class: App\Entity\CustomDateRange
tags:
- { name: pim_date_collection.range_type }
Custom Validation Create a custom constraint:
namespace App\Validator;
use Symfony\Component\Validator\Constraint;
class NoFutureDates extends Constraint {
public $message = 'Date ranges cannot be in the future.';
}
Then validate in your controller:
$validator = $this->get('validator');
$errors = $validator->validate($dateRanges, [
new NoFutureDates(),
]);
Localization Override translation keys (e.g., for the "Add Date Range" button):
# app/Resources/translations/messages.en.yml
pim_date_collection:
add_date_range: "Add Custom Period"
DateCollection attributes in chunks to avoid memory issues.CREATE INDEX idx_pim_catalog_value_validity_periods ON pim_catalog_value (attribute_id, locale, value);
How can I help you explore Laravel packages today?