From 5990258f05957a07a4f7e89fd020312531bca460 Mon Sep 17 00:00:00 2001 From: Ross Keatinge Date: Sun, 7 Sep 2014 10:09:26 -0400 Subject: [PATCH] Added more comments. Changed service defintion from yml to xml --- .gitignore | 5 +++- .../TetranzSelect2EntityExtension.php | 5 ++-- .../EntitiesToPropertyTransformer.php | 24 ++++++++++++++----- .../EntityToPropertyTransformer.php | 24 ++++++++++++++----- Form/Type/Select2EntityType.php | 14 ++++++----- LICENSE | 2 +- Resources/config/services.xml | 18 ++++++++++++++ Resources/config/services.yml | 8 ------- Resources/views/Form/fields.html.twig | 2 +- Tests/Controller/DefaultControllerTest.php | 17 ------------- 10 files changed, 71 insertions(+), 48 deletions(-) create mode 100644 Resources/config/services.xml delete mode 100644 Resources/config/services.yml delete mode 100644 Tests/Controller/DefaultControllerTest.php diff --git a/.gitignore b/.gitignore index f4a95cb..bbbe0ef 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,7 @@ app/config/parameters.ini app/config/parameters.yml # Composer -composer.phar \ No newline at end of file +composer.phar + +#PhpStorm +.idea diff --git a/DependencyInjection/TetranzSelect2EntityExtension.php b/DependencyInjection/TetranzSelect2EntityExtension.php index 00c6dee..b59f7f4 100644 --- a/DependencyInjection/TetranzSelect2EntityExtension.php +++ b/DependencyInjection/TetranzSelect2EntityExtension.php @@ -22,13 +22,14 @@ public function load(array $configs, ContainerBuilder $container) $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); + // set parameters with these settings so they'll be available in the service definition xml $varNames = ['minimum_input_length', 'page_limit', 'data_type']; foreach($varNames as $varName) { $container->setParameter("tetranz_select2_entity.$varName", $config[$varName]); } - $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); - $loader->load('services.yml'); + $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.xml'); } } diff --git a/Form/DataTransformer/EntitiesToPropertyTransformer.php b/Form/DataTransformer/EntitiesToPropertyTransformer.php index 7c0cf04..21bd092 100644 --- a/Form/DataTransformer/EntitiesToPropertyTransformer.php +++ b/Form/DataTransformer/EntitiesToPropertyTransformer.php @@ -1,10 +1,4 @@ textProperty = $textProperty; } + /** + * Transform initial entities as json with id and text + * + * @param mixed $entities + * @return string + */ public function transform($entities) { if (count($entities) == 0) { @@ -49,6 +55,12 @@ public function transform($entities) return htmlspecialchars(json_encode($data)); } + /** + * Transform csv list of ids to a collection of entities + * + * @param string $values as a CSV list + * @return array|ArrayCollection|mixed + */ public function reverseTransform($values) { // remove the 'magic' non-blank value added in fields.html.twig diff --git a/Form/DataTransformer/EntityToPropertyTransformer.php b/Form/DataTransformer/EntityToPropertyTransformer.php index 71dd0d6..2ea0318 100644 --- a/Form/DataTransformer/EntityToPropertyTransformer.php +++ b/Form/DataTransformer/EntityToPropertyTransformer.php @@ -1,16 +1,16 @@ textProperty = $textProperty; } + /** + * Transform entity to json with id and text + * + * @param mixed $entity + * @return string + */ public function transform($entity) { if (null === $entity || '' === $entity) { @@ -44,6 +50,12 @@ public function transform($entity) return htmlspecialchars(json_encode($data)); } + /** + * Transform to single id value to an entity + * + * @param string $value + * @return mixed|null|object + */ public function reverseTransform($value) { if (null === $value || '' === $value) { diff --git a/Form/Type/Select2EntityType.php b/Form/Type/Select2EntityType.php index a25c519..5ddbffc 100755 --- a/Form/Type/Select2EntityType.php +++ b/Form/Type/Select2EntityType.php @@ -1,10 +1,4 @@ em, $options['class'], $options['text_property']) : new EntityToPropertyTransformer($this->em, $options['class'], $options['text_property']); @@ -49,6 +50,7 @@ public function finishView(FormView $view, FormInterface $form, array $options) { parent::finishView($view, $form, $options); + // make variables available to the view $view->vars['remote_path'] = $options['remote_path'] ?: $this->router->generate($options['remote_route'], $options['remote_params']); diff --git a/LICENSE b/LICENSE index 8a024d8..1260d78 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014 tetranz +Copyright (c) 2014 Ross Keatinge Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/Resources/config/services.xml b/Resources/config/services.xml new file mode 100644 index 0000000..a8bf6e3 --- /dev/null +++ b/Resources/config/services.xml @@ -0,0 +1,18 @@ + + + + + + + + + + %tetranz_select2_entity.minimum_input_length% + %tetranz_select2_entity.page_limit% + %tetranz_select2_entity.data_type% + + + + diff --git a/Resources/config/services.yml b/Resources/config/services.yml deleted file mode 100644 index f8c327f..0000000 --- a/Resources/config/services.yml +++ /dev/null @@ -1,8 +0,0 @@ -parameters: - tetranz_select2entity.select2entity_type.class: Tetranz\Select2EntityBundle\Form\Type\Select2EntityType - -services: - tetranz_select2entity.select2entity_type: - class: %tetranz_select2entity.select2entity_type.class% - arguments: [@doctrine.orm.entity_manager, @router, %tetranz_select2_entity.minimum_input_length%, %tetranz_select2_entity.page_limit%, , %tetranz_select2_entity.data_type%] - tags: [{ name: form.type, alias: tetranz_select2entity }] diff --git a/Resources/views/Form/fields.html.twig b/Resources/views/Form/fields.html.twig index bc83244..39d6f8c 100755 --- a/Resources/views/Form/fields.html.twig +++ b/Resources/views/Form/fields.html.twig @@ -12,7 +12,7 @@ {# Add class for jQuery for find it #} {% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' select2entity')|trim }) %} - {# A bit of a kludge here. + {# A bit of a hack here. It seems to work more smoothly if we put the initial value into data-value rather than the value attribute. But ... value needs to be non-blank for initSelection in the js to fire. We need to remove this value in EntitiesToPropertyTransformer #} diff --git a/Tests/Controller/DefaultControllerTest.php b/Tests/Controller/DefaultControllerTest.php deleted file mode 100644 index 74b00bd..0000000 --- a/Tests/Controller/DefaultControllerTest.php +++ /dev/null @@ -1,17 +0,0 @@ -request('GET', '/hello/Fabien'); - - $this->assertTrue($crawler->filter('html:contains("Hello Fabien")')->count() > 0); - } -}