dev-develop
dev-develop
MIT
The Requires
The Development Requires
1.0.0-alpha1
1.0.0.0-alpha1
MIT
The Requires
The Development Requires
This library allows you to add extra values for instances of predefined entity types. These extra values and their characteristics are dynamicly defined in other bundles with the usage of data providers. Based on these providers your form will automatically build and do the necessary validations. After saving, these values (strings, integers, array collections, ...) are easily accessible with the services we provide in this bundle so they can be used throughout the project., (*1)
This bundle is compatible with all Symfony ^5.0 releases., (*2)
You can use composer to install the bundle in an existing symfony project., (*3)
composer require digipolisgent/setting-bundle
For more information on how to install symfony bundles, please see the Symfony documentation about bundles, (*4)
There are no routes to register or other mandatory configuration options., (*5)
If you are not familiar with symfony we suggest reading the Symfony 5 documentation., (*6)
Entity types are the the entities we want to assign extra values to. We define these by using the SettingImplementationTrait.
This requires the getSettingImplementationName
method to be implemented too. We use this name to access the entity types., (*7)
<?php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; use DigipolisGent\SettingBundle\Entity\Traits\SettingImplementationTrait; /** * @ORM\Entity() */ class Foo { /** * @ORM\Id * @ORM\Column(type="guid") * @ORM\GeneratedValue(strategy="UUID") */ protected $id; /** * Get id * * @return integer */ public function getId() { return $this->id; } use SettingImplementationTrait; /** * @return string */ public static function getSettingImplementationName() { return 'foo'; } }
The trait will, (*8)
DynamicSettingImplementationRelationSubscriber
SettingEntityType
entity is added to the databaseDataValueService
FormService
adds and processes the extra configuration if the set data is an entity using this traitThese are the services that will define how an extra configured field behaves in a form and how the data is presented when we want to use it throughout the application., (*9)
A service needs to extend from the AbstractFieldType
. As an example we use one of the predefined field types., (*10)
<?php namespace DigipolisGent\SettingBundle\FieldType; use Symfony\Component\Form\Extension\Core\Type\TextType; /** * Class StringFieldType * @package DigipolisGent\SettingBundle\FieldType */ class StringFieldType extends AbstractFieldType { /** * @return string */ public static function getName(): string { return 'string'; } /** * @return string */ public function getFormType(): string { return TextType::class; } /** * @param $value * @return array */ public function getOptions($value): array { $options = []; $options['attr']['value'] = $value ? $value : ''; return $options; } /** * @param $value * @return mixed */ public function decodeValue($value) { return $value; } /** * @param $value * @return string */ public function encodeValue($value): string { return $value; } }
The important thing here is the name we give to the service. We will use this name later to add extra config to the entities that are an instance of the entity types we used before., (*11)
Once the field type is in its place we define the service and tag it as a field_type
, (*12)
services: _defaults: autowire: true autoconfigure: true public: true DigipolisGent\SettingBundle\FieldType\StringFieldType: tags: - { name: field_type}
Data types are the representation of the extra settings you want to add for every entity type. You can define these by adding a DataTypeProvider
.
These datatype providers must be registered as a tagged service using the data_type_provider
tag and implement the DataTypeProviderInterface
., (*13)
<?php namespace AppBundle\Provider; use DigipolisGent\SettingBundle\Provider\DataTypeProviderInterface; class DataTypeProvider implements DataTypeProviderInterface { /** * @return array */ public function getDataTypes() { return [ [ 'key' => 'bar', 'label' => 'My foo label', 'required' => true, 'field_type' => 'foo', 'entity_types' => ['foo'], ], ]; } }
All keys are checked, (*14)
For the changes to take effect the database needs to be updated. The following command will fill the database., (*15)
bin/console doctrine:fixtures:load --append
You can add these extra settings to your form by adding an event subscriber to your form builder. This subscriber will make sure the form is built and processed as defined in the field types. If the data set to the form is not an entity type nothing will happen. This way you can also use the subscriber in generic forms., (*16)
<?php namespace AppBundle\Form\Type; use DigipolisGent\SettingBundle\EventListener\SettingFormListener; use DigipolisGent\SettingBundle\Service\FormService; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\AbstractType; class YourFormType extends AbstractType { public $formService; /** * @param FormService $formService */ public function setFormService(FormService $formService) { $this->formService = $formService; } /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { parent::buildForm($builder, $options); $builder->addEventSubscriber(new SettingFormListener($this->formService)); } }
To access these values in other scripts we can use the DataValueService
., (*17)
For example. If you want the the value with key bar
from an instance of the foo
class that you previously saved using the form you do the following., (*18)
$value = $this->dataValueService->getValue($foo, 'bar');
If after some data manipulation you want to update this value and make it visibile in the form you can also store the value., (*19)
$this->dataValueService->storeValue($foo, 'bar', 'manipulated string');
You can also use the field type to store and manipulate entities since you can inject other services here. This is an example where we make a checkbox list of bar
entities., (*20)
<?php namespace AppBundle\FieldType; use AppBundle\Entity\Bar; use AppBundle\Form\Type\BarFormType; use DigipolisGent\SettingBundle\Entity\SettingDataValue; use DigipolisGent\SettingBundle\FieldType\AbstractFieldType; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Form\Extension\Core\Type\CollectionType; class BarCheckboxFieldType extends AbstractFieldType { private $entityManager; public function __construct(EntityManagerInterface $entityManager) { $this->entityManager = $entityManager; } public function getFormType(): string { return CollectionType::class; } /** * @param $value * @return array */ public function getOptions($value): array { $options = []; $options['entry_type'] = BarFormType::class; $options['allow_add'] = true; $options['allow_delete'] = true; $options['by_reference'] = false; $options['prototype'] = true; $options['prototype_data'] = new Bar(); $ids = json_decode($value, true); $barRepository = $this->entityManager->getRepository(Bar::class); $data = []; if (!is_null($ids)) { foreach ($ids as $id) { $data[] = $barRepository->find($id); } } $options['data'] = $data; return $options; } /** * @return string */ public static function getName(): string { return 'bar_checkbox'; } /** * @param $value * @return string */ public function encodeValue($value): string { $barIds = []; foreach ($value as $bar) { $this->entityManager->persist($bar); $capistranoSymlinkIds[] = $bar->getId(); } return json_encode($barIds); } public function decodeValue($value) { $barRepository = $this->entityManager->getRepository(Bar::class); $ids = []; if ($value == '' || is_null($ids)) { return []; } $bars = []; $ids = json_decode($value, true); foreach ($ids as $id) { $bars[] = $barRepository->find($id); } return $bars; } }
Using bar_checkbox
as a field type in your data type provider will :, (*21)
getValue
function in the DataValueService
will give you a list of Bar
entitiesstoreValue
function in the DataValueService
gives you the possibility to save a list of Bar
entities
MIT
MIT