PHP code example of digipolisgent / setting-bundle

1. Go to this page and download the library: Download digipolisgent/setting-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

digipolisgent / setting-bundle example snippets





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';
    }
}




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;
    }
}




namespace AppBundle\Provider;

use DigipolisGent\SettingBundle\Provider\DataTypeProviderInterface;

class DataTypeProvider implements DataTypeProviderInterface
{

    /**
     * @return array
     */
    public function getDataTypes()
    {
        return [
            [
                'key' => 'bar',
                'label' => 'My foo label',
                '




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));
    }
}


$value = $this->dataValueService->getValue($foo, 'bar');

$this->dataValueService->storeValue($foo, 'bar', 'manipulated string');




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;
    }
}