aschaeffer/sonata-republicandate-field-bundle
Installation
composer require aschaeffer/sonata-republicandate-field-bundle
For non-Flex projects, enable the bundle in AppKernel.php:
new Aschaeffer\SonataRepublicandateFieldBundle\AschaefferSonataRepublicandateFieldBundle(),
First Use Case
Add the annotation to a string property in your entity, linking it to a DateTime field:
use Doctrine\ORM\Mapping as ORM;
use Aschaeffer\SonataRepublicandateFieldBundle\Annotation\RepublicandateField;
class User {
/**
* @ORM\Column(type="date", nullable=true)
*/
protected ?\DateTimeInterface $birthday;
/**
* @ORM\Column(type="string", nullable=true)
* @RepublicandateField(gregorianDate="birthday")
*/
protected ?string $birthdayRepublican;
}
Sonata Admin Integration
Ensure your Sonata Admin class extends Sonata\AdminBundle\Admin\Admin and the entity is registered in the admin configuration.
Bidirectional Sync
DateTime) and Republican (string) dates.Form Handling
DataGrid Integration
toString() method in your entity or use a custom field descriptor.@Assert\Regex for valid Republican date formats like 123ème de la République).RepublicandateConverter service to support non-standard Republican date formats.{{ entity.birthdayRepublican }}).Missing ext-calendar Extension
ext-calendar extension for date conversions. Verify it’s enabled:
php -m | grep calendar
sudo apt-get install php-calendar on Ubuntu).Non-Existent Republican Dates
public function setBirthday(?\DateTimeInterface $birthday): self {
if ($birthday && !$this->isRepublicanDateValid($birthday)) {
throw new \InvalidArgumentException('Invalid Republican date');
}
$this->birthday = $birthday;
return $this;
}
Sonata Admin Caching
php bin/console sonata:cache:clear
RepublicandateConverter service to debug issues:
$converter->convertToRepublican($gregorianDate); // Add logging here
string field contains valid Republican dates (e.g., 123ème de la République).Custom Converter Override the default converter by configuring a custom service:
# config/services.yaml
services:
App\Service\CustomRepublicandateConverter:
arguments:
- '@service_container'
tags: ['sonata_republicandate.converter']
Field Descriptor Override Customize how the Republican date appears in Sonata’s DataGrid:
protected function configureListFields(ListMapper $listMapper) {
$listMapper
->add('birthdayRepublican', 'string', [
'template' => 'custom_republican_date.html.twig',
]);
}
Validation Add custom validation logic to the Republican date field:
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Column(type="string", nullable=true)
* @RepublicandateField(gregorianDate="birthday")
* @Assert\Regex(
* pattern="/^\d{1,3}(?:ème|er|ère|e|ème de la République)$/",
* message="Invalid Republican date format"
* )
*/
protected ?string $birthdayRepublican;
How can I help you explore Laravel packages today?