Drupal hook改变不起作用

时间:2017-10-11 07:10:57

标签: php drupal drupal-7

我在这里使用谷歌地理编码器:

(function ($) {
Drupal.behaviors.ems_offices = {
    attach: function (context, settings) {
        geocoder = new google.maps.Geocoder();
        $("#edit-submit").click(function(e){
            var googleMapAdress       = $("#edit-field-offices-google-maps-info-und-0-value").val();
            var googleMapsHiddenInput = $("input[name='google_maps_geolocation']");
            e.preventDefault();

            geocoder.geocode( { 'address': googleMapAdress}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (googleMapAdress === '') {
                        googleMapsHiddenInput.val('');
                    } else {
                        googleMapsHiddenInput.val(JSON.stringify(results));
                    }
                }
            });

            $("#offices-node-form").submit();
        });
    }
  };
}(jQuery));

尝试将它们挂钩在* .module

function ems_offices_form_offices_node_form_alter(&$form, &$form_state, $form_id) {
if ('offices_node_form' == $form_id) {
    $form['#attached']['js'][] = drupal_get_path("module", "ems_offices")."/assets/js/change_address_to_google_map_object.js" ;
    $form['#submit'][] = 'ems_offices_submit';
    $form['google_maps_geolocation'] = array(
        '#type'          => 'hidden',
        '#default_value' => '',
    );
}

}

function ems_offices_submit(&$form, &$form_state) {
  if ($_POST['google_maps_geolocation'] !== '') {
    $googleMapsInformation                                                     = $_POST['google_maps_geolocation'];
    $form_state['values']['field_offices_google_maps_info']['und'][0]['value'] = $googleMapsInformation;
  }
}

在调试后我发现js脚本正在工作,我有json响应。但是模块没有收到这个值。这个构建在我的域上工作的有趣部分,但不适用于localy。 哪里可以问题?

1 个答案:

答案 0 :(得分:-1)

尝试使用https://www.drupal.org/project/geocoder代替JS 非常容易和工作好!