場所のバンドルをインストールしようとしていますJulLocationBundle
。そして、私は解決することができたいくつかの問題に出くわしました。その後、エラーが発生しましたcall undefined function getChild
。
一部の調査では、これはメソッドの非推奨が原因であることが明らかになりました (2.2 以降)...
したがって、変更する必要があるコードの部分は次のとおりです。
if( $locationForm->offsetExists( $locationType ) )
{
$topLevel = $locationType;
$topLevelForm = $locationForm->getChild( $topLevel );
break;
}
完全なコントローラーコード:
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
));
}
}
コードは非常に自明ですが、最上位エンティティの子を取得する必要があります (セットアップに従う場合は通常場所ですが、問題はformview::getchild()
メソッドの非推奨を回避する方法にあります.?!
編集:バンドルが必要な人向け。
JulLocationBundle
数日以内に 2.2 用に修正するためのプル リクエストを提供します