简体   繁体   English

如何将值从 javascript 传递到 drupal 8 表单?

[英]How to pass a value from javascript to drupal 8 form?

I need to pass a value from Javascript to my drupal 8 form.我需要将值从 Javascript 传递到我的 drupal 8 表单。

I've added a hidden field to the form.我在表单中添加了一个隐藏字段。 Javascript is calculating a value and writes it into the field. Javascript 正在计算一个值并将其写入该字段。 But how can I get to the value within the function submitForm()?但是如何获取函数 submitForm() 中的值?

Is using a hidden field even the right approach?使用隐藏字段甚至是正确的方法吗? If so, what do I have to do, to make this work?如果是这样,我必须做什么才能使这项工作?

I've removed most of the code for readability.为了便于阅读,我删除了大部分代码。

FooForm.php: FooForm.php:

class FooForm extends FormBase
{
    public function getFormId()
    {
        return 'fooID';
    }
    public function buildForm(array $form, FormStateInterface $form_state)
    {

    //...here are lot's of elements not relevant right now

    $form['myhiddenfield'] = ['#type' => 'hidden'];//adding hidden field.
    $form['#attached']['library'][] = 'foo/foocalculator';

    return $form;
    }

    public function submitForm(array &$form, FormStateInterface $form_state)
    {
    dpm($form_state->getValues()['myhiddenfield']);//not getting the calculated value.
    }

foocalculator.js: foocalculator.js:

(function ($, Drupal) {
    passToDrupal = $('#myhiddenfield');
    $('#edit-submit--3').click(function (event) {
        calcRoute(address, $editparcel.fieldValue().toString())
    });
})(jQuery, Drupal);

function calcRoute(start, destination) {
    var request = {
        origin: start,
        destination: destination,
    };
    directionsService.route(request, function (result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(result);
        mydistance = result.routes[0].legs[0].distance.value;
        passToDrupal.val(mydistance);//adds value to hidden field.
    }    
    });
}

The hidden fields are a bit special because the changes of the field value are not directly visible among the submitted values (which can be retrieved with $form_state->getValues()).隐藏字段有点特殊,因为字段值的变化在提交的值中并不直接可见(可以通过 $form_state->getValues() 检索)。 Instead, they are visible in the user input values of the form state, so try this:相反,它们在表单状态的用户输入值中可见,所以试试这个:

$form_state->getUserInput()['myhiddenfield']

Be careful with using the getUserInput() for hidden fields, because unless you have a good reason why you do it (like you have in this case) a change in a hidden field is usually done by "unfriendly" entities, which are trying to break your site.对隐藏字段使用 getUserInput() 时要小心,因为除非你有充分的理由这样做(就像你在这种情况下那样)隐藏字段的更改通常是由“不友好”的实体完成的,它们试图破坏您的网站。

You should set a default value to your hidden field:您应该为隐藏字段设置默认值:

$form['bid'] = array('#type' => 'hidden', '#default_value' => '');

Taken from: https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7.x#hidden摘自: https : //api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7.x#hidden

Note that if you plan to use JavaScript to change your hidden element's value, you will need to use the #default_value property rather than #value.请注意,如果您计划使用 JavaScript 来更改隐藏元素的值,则需要使用 #default_value 属性而不是 #value。

I tested in Drupal 8, and it worked fine.我在 Drupal 8 中进行了测试,效果很好。

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

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