2

言語が変更されたときに ajax コールバックをバインドする方法はありますか。言語が変更されたときに nodereference-dropdown を更新したい (その言語でのみ値を表示する)。

他のコールバックは機能していますが、次のコードは機能していません (form_alter)。

どうすればそれを達成できますか?

$form['language']['#ajax'] = array(
            'callback' => 'mymodule_something_language_callback',
            'wrapper' => 'my-module-replace',                
            '#weight' => 2
        );

ありがとう。

コメントから

$form['language']; の var_dump を次に示します。

array
  '#type' => string 'select' (length=6)
  '#title' => string 'Language' (length=8)
  '#default_value' => string 'und' (length=3)
  '#options' => 
    array
      'und' => string 'Language neutral' (length=16)
      'en' => string 'English' (length=7)
      'ar' => string 'Arabic' (length=6)
4

1 に答える 1

0

問題は、hook_form_alter() が呼び出された後に Locale モジュールがこのフォーム要素を変更することです (この投稿を参照)。

この問題を解決した方法は次のとおりです。

最初に、Drupal がフックを実装する順序を変更し、「form_alter」を最後に置きます。

<?php 

/**
 * Implementation of hook_module_implements_alter()
 */
function chronos_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'form_alter') {
    // Move mymodule_form_alter() to the end of the list. module_implements()
    // iterates through $implementations with a foreach loop which PHP iterates
    // in the order that the items were added, so to move an item to the end of
    // the array, we remove it and then add it.
    $group = $implementations['chronos'];
    unset($implementations['chronos']);
    $implementations['chronos'] = $group;
  }
}

次に、必要な form 要素と '#ajax' 要素を $form['language'] に追加します。

<?php 

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'page_node_form') {
        // alter the form
        $form['container'] = array(
        '#prefix' => '<div id="ajax-language">',
        '#suffix' => '</div>',
    );
        $form['language']['#ajax'] = array(
                'callback' => 'mymodule_save_language_callback',
                'wrapper' => 'ajax-language'
        );
        return $form;
    }
}

最後に、コールバックを追加します。

<?php

/**
 * Returns changed part of the form.
 *
 * @return renderable array
 *
 * @see ajax_example_form_node_form_alter()
 */
function chronos_save_language_callback($form, $form_state) {
  # set session variables or perform other actions here, if applicable
  return $form['container'];
}
于 2014-09-17T21:46:47.393 に答える