Question

I'm trying to install a bundle for locations JulLocationBundle. And I came across some problems, which I managed to solve; then an error came up call undefined function getChild.

Some research revealed that this is caused by deprecation of the method (since 2.2 )...

So, here is the part of the code that needs to be changed:

            if( $locationForm->offsetExists( $locationType ) )
        {
            $topLevel = $locationType;
            $topLevelForm = $locationForm->getChild( $topLevel );

            break;
        }

full controller code :

namespace Jul\LocationBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class GooglemapsController extends Controller
{
    public function placesAutocompleteAction
    (
            $locationForm,
            $zoomDefault = null,
            $zoomResolved = 17,
            $latitude = null,
            $longitude = null,
            $mapDiv = 'map_canvas',
            $mapOptions = array(),
            $acFields = null,
            $addressFallback = false,
            $maxImageWidth = 200,
            $maxImageHeight = 200
    )
    {
        /*
         * Find top level entity
         */
        $locationTypes = array( 'location', 'city', 'state', 'country' );

        foreach( $locationTypes as $locationType )
        {
            if( $locationForm->offsetExists( $locationType ) )
            {
                $topLevel = $locationType;
                $topLevelForm = $locationForm->getChild( $topLevel );

                break;
            }

            if( $locationForm->getName() == 'Jul' . ucfirst( $locationType ) . 'Field' )
            {
                $topLevel = $locationType;
                $topLevelForm = $locationForm;

                break;
            }
        }

        /*
         * Top level not found
         */
        if( ! isset( $topLevel ) ) throw new \Exception( 'There is no location field in the form sent to the controller JulLocationBundle:Googlemaps:placesAutocomplete' );

        /*
         * Default map center and zoom
         */
        if( $topLevelForm->offsetExists( 'latitude' ) && ( $latForm = $topLevelForm->getChild( 'latitude' )->get( 'value' ) ) <> 0 )
        {
            /*
             * If the form has been sent with a location
             */
            $latitude = $latForm;
            $longitude = $topLevelForm->getChild( 'longitude' )->get( 'value' );

            $zoomDefault = $zoomResolved;
        }
        else
        {
            if( ! $latitude ) $latitude = 40.4230;
            if( ! $longitude ) $longitude = -98.7372;
            if( ! $zoomDefault ) $zoomDefault = 3;
        }

        /*
         * Default map options array
         */
        $mapOptions = array_merge( array(
            'zoom' => $zoomDefault
            ), $mapOptions );

        /*
         * Default autocomplete input field
         */
        if( ! isset( $acFields[ 0 ][ 'acInput' ] ) )
        {
            $acFields[ 0 ][ 'acInput' ] = ( $topLevelForm->offsetExists( 'long_name' ) ) ? $topLevelForm->getChild( 'long_name' )->get( 'id' ) : $topLevelForm->getChild( 'name' )->get( 'id' );
        }

        /*
         * Default autocomplete Types
         */
        if( ! isset( $acFields[ 0 ][ 'acOptions' ]['types'] ) )
        {
            switch( $topLevel )
            {
                case 'location': $acFields[ 0 ][ 'acOptions' ][ 'types' ] = array( 'establishment' ); break;
                case 'city': $acFields[ 0 ][ 'acOptions' ][ 'types' ] = array( '(cities)' ); break;
                default: $acFields[ 0 ][ 'acOptions' ][ 'types' ] = array( '(regions)' );
            }
        }

        /*
         * Address autocomplete fallback
         */
        if( $addressFallback && $topLevel == 'location' && ! isset( $acFields[ 1 ][ 'acInput' ] ) && $topLevelForm->offsetExists( 'long_address' ) )
        {
            $acFields[ 1 ][ 'acInput' ] = ( $topLevelForm->offsetExists( 'long_name' ) ) ? $topLevelForm->getChild( 'long_address' )->get( 'id' ) : $topLevelForm->getChild( 'address' )->get( 'id' );
            $acFields[ 1 ][ 'acOptions' ][ 'types' ] = array( 'geocode' );
        }

        /*
         * Build javascript field IDs array using JulLocationBundle config
         */

        $jsFieldIds = array();
        $tmpLevel = $locationForm;

        foreach( $this->container->parameters[ 'jul_location.options' ] as $level => $options )
        {
            $fields = $options['fields'];

            $tmpArray = array();

            if( $tmpLevel->offsetExists( $level ) )
            {
                $tmpLevel = $tmpLevel->getChild( $level );

                foreach( $fields as $field => $fieldArray )
                {
                    /*
                     * Check if field is active in config && exists in the form
                     */
                    if( $fieldArray[ 'enabled' ] && $tmpLevel->offsetExists( $field ) ) $tmpArray[ $field ] = $tmpLevel->getChild( $field )->get( 'id' );
                }
            }

            $jsFieldIds[ $level ] = $tmpArray;
        }

        return $this->render( 'JulLocationBundle:Googlemaps:placesAutocomplete.html.twig', array(
                'mapDiv' => $mapDiv,
                'mapOptions' => json_encode( $mapOptions ),
                'acFields' => json_encode( $acFields ),
                'topLevel' => $topLevel,
                'zoomResolved' => $zoomResolved,
                'latitude' => $latitude,
                'longitude' => $longitude,
                'jsFieldIds' => json_encode( $jsFieldIds ),
                'maxImageWidth' => $maxImageWidth,
                'maxImageHeight' => $maxImageHeight
                ));
    }
}

the code is pretty self explanatory we need to get the child of the top level entity (usually location if you follow the setup , but the question lies on how to work around the deprecation of formview::getchild() method.?!

EDIT : for any one who need the bundle .

i will provide a pull request to fix JulLocationBundle for 2.2 in a few days

Was it helpful?

Solution

You can use get method $form->get('...') or just $form['...'].

Read more about deprecations.

OTHER TIPS

Instead of :

$topLevelForm = $locationForm->getChild($topLevel);

You should use :

$topLevelForm = $locationForm->children[$topLevel];

As it is described in the code (branch 2.2).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top