Skip to content

Sf4 support #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
16 changes: 2 additions & 14 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,22 @@

namespace Oh\GoogleMapFormTypeBundle\DependencyInjection;


use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();

$rootNode = $treeBuilder->root('oh_google_map_form_type');

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
$rootNode
->children()
->scalarNode('api_key')
->scalarNode('api_key')->isRequired()->cannotBeEmpty()
->end()
->end();
->end();

return $treeBuilder;
}
Expand Down
26 changes: 6 additions & 20 deletions DependencyInjection/OhGoogleMapFormTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,19 @@

namespace Oh\GoogleMapFormTypeBundle\DependencyInjection;

use Oh\GoogleMapFormTypeBundle\Form\Type\GoogleMapType;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class OhGoogleMapFormTypeExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);


$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');

$container
->getDefinition('form.type.oh_google_maps')
->addArgument($config['api_key']);
$config = $this->processConfiguration(new Configuration(), $configs);

$container->register('form.type.oh_google_maps', GoogleMapType::class)
->addArgument($config['api_key'])
->addTag('form.type')
;
}
}
15 changes: 2 additions & 13 deletions Form/Type/GoogleMapType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
Expand All @@ -13,10 +12,10 @@

class GoogleMapType extends AbstractType
{

private $api_key;

public function __construct($api_key){
public function __construct($api_key)
{
$this->api_key = $api_key;
}

Expand Down Expand Up @@ -92,16 +91,6 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$view->vars['include_gmaps_js'] = $options['include_gmaps_js'];
}

public function getParent()
{
return FormType::class;
}

public function getName()
{
return 'oh_google_maps';
}

public function getBlockPrefix()
{
return 'oh_google_maps';
Expand Down
32 changes: 21 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,12 @@ public function registerBundles()
// ...
```

You might need to change a couple of options if you are trying to use Symfony 2.0

Add OhGoogleMapFormTypeBundle to assetic
```yaml
# app/config/config.yml
# Assetic Configuration
assetic:
bundles: [ 'OhGoogleMapFormTypeBundle' ]

...
Add a file
`vendor/oh/google-map-form-type-bundle/Oh/GoogleMapFormTypeBundle/Resources/public/js/jquery.ohgooglemaps.js`
to your webpack encore config.

```
oh_google_map_form_type:
api_key: "%google_maps_api_key%"
```
Expand All @@ -41,10 +36,19 @@ Usage

This bundle contains a new FormType called GoogleMapType which can be used in your forms like so:

$builder->add('latlng', 'oh_google_maps');
```php
<?php

use Oh\GoogleMapFormTypeBundle\Form\Type\GoogleMapType;

/** @var \Symfony\Component\Form\FormBuilderInterface $builder */
$builder->add('latlng', GoogleMapType::class);
```

On your model you will have to process the latitude and longitude array

```php
<?php
// Your model eg, src/My/Location/Entity/MyLocation.php
use Symfony\Component\Validator\Constraints as Assert;
use Oh\GoogleMapFormTypeBundle\Validator\Constraints as OhAssert;
Expand All @@ -70,18 +74,22 @@ On your model you will have to process the latitude and longitude array
}

}
```

Include the twig template for the layout. It's generally a good idea to overwrite the form template in your own twig template

```yaml
# your config.yml
twig:
form:
resources:
# This uses the default - you can put your own one here
- 'OhGoogleMapFormTypeBundle:Form:fields.html.twig'
```

If you are intending to override some of the elements in the template then you can do so by extending the default `google_maps.html.twig`. This example adds a callback to the javascript when a new map position is selected.

```twig
{# your template which is inluded in the config.yml (above)
eg src/My/Location/Resources/views/Form/fields.html.twig #}
{% extends "OhGoogleMapFormTypeBundle:Form:google_maps.html.twig" %}
Expand All @@ -93,13 +101,14 @@ If you are intending to override some of the elements in the template then you c
}
</script>
{% endblock %}

```

Options
-------

There are a number of options, mostly self-explanatory

```
array(
'type' => 'text', // the types to render the lat and lng fields as
'options' => array(), // the options for both the fields
Expand All @@ -116,6 +125,7 @@ There are a number of options, mostly self-explanatory
'include_jquery' => false, // jquery needs to be included above the field (ie not at the bottom of the page)
'include_gmaps_js'=>true // is this the best place to include the google maps javascript?
)
```

Screenshots
-------
Expand Down
Empty file removed Resources/config/config.yml
Empty file.
17 changes: 0 additions & 17 deletions Resources/config/services.xml

This file was deleted.

10 changes: 4 additions & 6 deletions Resources/views/Form/google_maps.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@
{% endblock %}
{% block oh_google_maps_javascripts %}
{% if include_jquery %}
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script
src="https://code.jquery.com/jquery-1.7.2.min.js"
integrity="sha256-R7aNzoy2gFrVs+pNJ6+SokH04ppcEqJ0yFLkNGoFALQ="
crossorigin="anonymous"></script>
{% endif %}
{% if include_gmaps_js %}
<script type="text/javascript" src="//maps.google.com/maps/api/js?key={{ api_key }}"></script>
{% endif %}
{% javascripts
'@OhGoogleMapFormTypeBundle/Resources/public/js/jquery.ohgooglemaps.js'
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
{% block oh_google_maps_javascript %}
{% block oh_google_maps_callback %}
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
],
"require": {
"php": ">=5.3.2",
"symfony/framework-bundle": "^2.1 || ^3.0"
"symfony/framework-bundle": "^3.4 || ^4.1",
"symfony/form": "^3.4 || ^4.1",
"symfony/validator": "^3.4 || ^4.1"
},
"autoload": {
"psr-0": {
Expand Down