danielantelo / AddressableBundle

Symfony2 bundle which facilitates making entities addressable and geo location aware. It includes a map form type to set address by searching in a google map.
6 stars 11 forks source link

Symphony 3 upgrade #9

Closed MargauxG closed 6 years ago

MargauxG commented 8 years ago

Hello,

It seems this bundle is not really compatible with Symphony 3. I got form problems (type text doesn't exist, it should be TextType::class), after correcting the type problem, I got this error: "Impossible to access an attribute ("latitude") on a null variable in AddressableBundle:Form:fields.html.twig at line 45". If I completely redo the AddressMapType to have a match between the fields, I only have the address field (i.e

street number, street name, city, zip code and country, no map and search area).

Is there another way to include the rest of the bundle (search area and map)?

danielantelo commented 8 years ago

Hi,

I have verified that master works on the latest standard symfony 3.1 installation (using symfony installer):

# see https://symfony.com/doc/current/setup.html
symfony new symfony_sandbox

Require the bundle in composer:

"require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.1.*",
        "doctrine/orm": "^2.5",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/doctrine-cache-bundle": "^1.2",
        "symfony/swiftmailer-bundle": "^2.3",
        "symfony/monolog-bundle": "^2.8",
        "symfony/polyfill-apcu": "^1.0",
        "sensio/distribution-bundle": "^5.0",
        "sensio/framework-extra-bundle": "^3.0.2",
        "incenteev/composer-parameter-handler": "^2.0",
        "daa/addressable-bundle": "0.*"
    },

Then register the bundle in AppKerne.php:

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new AppBundle\AppBundle(),
            new Addressable\Bundle\AddressableBundle(),
        ];

        return $bundles;
    }

Then modify the default controller symfony_sandbox/src/AppBundle/Controller/DefaultController.php to create the form:

<?php

namespace AppBundle\Controller;

use Addressable\Bundle\Form\Type\AddressMapType;
use AppBundle\Model\AddressableEntity;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        $entity = new AddressableEntity();
        $form = $this->createForm(AddressMapType::class, $entity, array(
            'google_api_key' => 'yourKeyHere'
        ));

        // replace this example code with whatever you need
        return $this->render('default/index.html.twig', [
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
            'form' => $form->createView(),
        ]);
    }
}

and add the model symfony_sandbox/src/AppBundle/Model/AddressableEntity.php

<?php

namespace AppBundle\Model;

use Addressable\Bundle\Model\AddressableInterface;
use Addressable\Bundle\Model\Traits\ORM\AddressableTrait;

class AddressableEntity implements AddressableInterface
{
    use AddressableTrait;
}

and update the view symfony_sandbox/app/Resources/views/default/index.html.twig to render the form:

{% extends 'base.html.twig' %}

{% block body %}
    <div id="wrapper">
        <div id="container">
            <div id="welcome">
                <h1><span>Welcome to</span> Symfony {{ constant('Symfony\\Component\\HttpKernel\\Kernel::VERSION') }}</h1>
            </div>

            {{ form_start(form) }}
            {{ form_widget(form) }}
            {{ form_end(form) }}

        </div>
    </div>
{% endblock %}

This produces:

screen shot 2016-09-20 at 21 38 51

The error "Impossible to access an attribute ("latitude") on a null variable in AddressableBundle:Form:fields.html.twig at line 45". is due to the fact that you are not passing a model/entity object to the form builder. You can do as above shown:

        $entity = new AddressableEntity();
        $form = $this->createForm(AddressMapType::class, $entity);

Let me know if you get it working, and I will close this issue, thanks.

danielantelo commented 8 years ago

Anyway, new release 0.4.3 will allow for null value in the view. 👍 Hope that helps.