简体   繁体   English

Drupal钩子更改不起作用

[英]Drupal hook alter doens't work

I'm using google geocoder here: 我在这里使用google geocoder:

(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));

And trying too hook them in *.module 并尝试将它们钩在* .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;
  }
}

After debuging I found out that js script is working and I have json response. 调试后,我发现js脚本正在运行,并且我有json响应。 But module doesn't receive this value. 但是模块没有收到该值。 The interesting part that this build work on my domain, but doesn't work localy. 这个构建可以在我的域上运行但在本地不起作用的有趣部分。 Where can be the problem? 问题出在哪里?

尝试使用https://www.drupal.org/project/geocoder代替JS非常简单,而且效果很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM