Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Drupal 8表单中获取触发按钮id_Php_Drupal_Drupal 7_Drupal 6_Drupal 8 - Fatal编程技术网

Php 如何在Drupal 8表单中获取触发按钮id

Php 如何在Drupal 8表单中获取触发按钮id,php,drupal,drupal-7,drupal-6,drupal-8,Php,Drupal,Drupal 7,Drupal 6,Drupal 8,嗨,我想知道一种在Drupal8表单中获取点击按钮的索引id的方法。 我有一张有一些字段的表格。它有添加、删除、添加更多按钮 我想在单击“删除”按钮时删除特定字段的值。 为了做到这一点,我需要知道点击按钮的索引。我能够在Drupal6和Drupal7中实现这一点,但在Drupal8中无法实现 public function field_add(array &$form, FormStateInterface $form_state) { $element_key = $form_

嗨,我想知道一种在Drupal8表单中获取点击按钮的索引id的方法。 我有一张有一些字段的表格。它有添加、删除、添加更多按钮

我想在单击“删除”按钮时删除特定字段的值。 为了做到这一点,我需要知道点击按钮的索引。我能够在Drupal6和Drupal7中实现这一点,但在Drupal8中无法实现

public function field_add(array &$form, FormStateInterface $form_state) {
    $element_key = $form_state->getTriggeringElement()['#parents'][1];
}
Drupal 6:

function field_add($form, &$form_state) {

  $element_key = $form_state['clicked_button']['#parents'][1];
}
function field_add($form, &$form_state) {

  $element_key = $form_state['triggering_element']['#parents'][1];
}
Drupal 7:

function field_add($form, &$form_state) {

  $element_key = $form_state['clicked_button']['#parents'][1];
}
function field_add($form, &$form_state) {

  $element_key = $form_state['triggering_element']['#parents'][1];
}
如何在Drupal 8中实现同样的功能?

我能够理解。 下面是在Drupal8中实现它的一种方法

public function field_add(array &$form, FormStateInterface $form_state) {
    $element_key = $form_state->getTriggeringElement()['#parents'][1];
}

在Drupal8中,这在我的案例中起了作用

$clickedElement = $form_state->getTriggeringElement()['#array_parents'][1];

在Drupal 8中,我使用属性“#name”命名了我的按钮,以便能够使用以下代码:

$clickedElement = $form_state->getTriggeringElement()['#name'];

通过这种方式,您可以防止数组索引不同可能出现的问题。

奇怪,对我来说是可行的:$form_state->getTriggeringElement()['#parents'][0];这比在“数组”父母身上回复要容易。