作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为 JulLocationBundle
位置安装 bundle 。我遇到了一些问题,但我设法解决了;然后出现错误调用未定义的函数 getChild
。
一些研究表明,这是由该方法的弃用引起的(自 2.2 起)...
因此,这是需要更改的代码部分:
if( $locationForm->offsetExists( $locationType ) )
{
$topLevel = $locationType;
$topLevelForm = $locationForm->getChild( $topLevel );
break;
}
完整的 Controller 代码:
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() 的弃用问题
方法.?!
编辑:对于任何需要该 bundle 的人。
我将在几天内提供拉取请求来修复 2.2 的 JulLocationBundle
最佳答案
您可以使用 get
方法 $form->get('...')
或仅使用 $form['...']
.
了解更多关于deprecations的信息.
关于symfony 2.2 getChild 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17017426/
我是一名优秀的程序员,十分优秀!