我有三种内容类型:
“ Fiche Plan Action”具有“ Promoteur”的参考字段(field_nom_du_promoteur_en_questi)。
“ Fiche Action”具有“ Fiche Plan d'action”(field_plan_d_action)的参考字段。
内容类型的创建如下:发起人,Fiche Plan Action和最终Fiche Action。
我想将Ajax回调附加到已归档的(field_plan_d_action),所以当我更改此自动完成字段时,类型文本字段由指向“ Promoteur”内容的链接自动填充。
我试图在“ Fiche Action”中获取对象,然后从该对象中获取“ Promoteur”的对象,然后通过指向该“ Promoteur”的链接来更新字段。
注意:我试图将Ajax附加到自动完成功能(参考)和选择列表中。当我设置一个静态ID来获取“ Promoteur”时,它可以工作,但是我想从当前表单中动态获取“ Fiche Plan Action”和“ Promoteur”的对象(使用form_alter())。
我在使用选择列表类型的Drupal文档中仅找到一个示例。对于其他归档类型,如Date(日期选择器),对我来说还不清楚。
function nom_promoteur_form_alter(&$form, FormStateInterface $form_state, $form_id){
if($form_id == 'node_fiche_d_action_form' || $form_id == 'node_fiche_d_action_edit_form'){
$form['field_plan_d_action']['widget'][0]['target_id']['#ajax'] = [
'callback' => 'myAjaxCallback',
'disable-refocus' => FALSE,
'event' => 'change',
'wrapper'=>'edit-field-nom-du-promoteur-0-value',
];
$form['field_example_select']['widget']['#ajax'] = [
'callback' => 'myAjaxCallback',
'disable-refocus' => FALSE,
'event' => 'change',
'wrapper'=>'edit-field-nom-du-promoteur-0-value',
];
}
}
function myAjaxCallback(array &$form, FormStateInterface $form_state) {
//$values = $form_state->cleanValues()->getValues();
/* fiche plan d'action (Object)*/
$nodeFPA = $form['field_plan_d_action']['widget'][0]['target_id']['#default_value'];
//$nidFPA = (int) $nodeFPA->nid->value;
/* Get Promoteur Dynamically (Object)*/
//$promoteur = $nodeFPA->field_nom_du_promoteur_en_questi->entity;
/* Promoteur with Static ID (Object)*/
$promoteur = \Drupal\node\Entity\Node::load(2);
$nid = $promoteur->id();
$nLabel = $promoteur->label();
/* Url Alias */
$url_alias = \Drupal::service('path_alias.manager')->getAliasByPath('/node/'. $nid);
/* Update field */
$form['field_nom_du_promoteur']['widget'][0]['value']['#value'] = "<a href='$url_alias'>$nLabel </a>";
/* Just For Test */
//$form['field_nom_du_promoteur']['widget'][0]['value']['#value'] = " Test ";
return $form['field_nom_du_promoteur'];
}