드루팔 (Drupal 7)의 동적 선택 목록에 대한 모범 사례는 무엇입니까?드루팔 (Drupal 7 : 드루팔 (Drupal 7의 동적 선택 목록에 대한 베스트 프랙티스)
a)는 UI를 통해 더미 옵션 필드 만들고 hook_form_FORM_ID_alter
또는
b) 상기 hook_form
으로 정의 모듈을 통해 처음부터 동적 선택 목록을 만드는 옵션을 무시 또는
c) 다른 접근?
감사
드루팔 (Drupal 7)의 동적 선택 목록에 대한 모범 사례는 무엇입니까?드루팔 (Drupal 7 : 드루팔 (Drupal 7의 동적 선택 목록에 대한 베스트 프랙티스)
a)는 UI를 통해 더미 옵션 필드 만들고 hook_form_FORM_ID_alter
또는
b) 상기 hook_form
으로 정의 모듈을 통해 처음부터 동적 선택 목록을 만드는 옵션을 무시 또는
c) 다른 접근?
감사
당신이 사용할 수있는 양식 API의 #ajax 그렇게 (이 Drupal6에서 #ahah
를 호출하는 데 사용). 파일
소스 mymodule.module :
여기 첫번째 드롭 두번째 드롭의 옵션 목록을 수정 개의 드롭 다운을 나타낸다 ( Examples for Developers 기준) 드루팔 7 예는<?php
/**
* Implementation of hook_menu().
* Registers a form-based page that you can access at "http://localhost/mypage"
*/
function mymodule_menu(){
return array(
'mypage' => array(
'title' => 'A page to test ajax',
'page callback' => 'drupal_get_form',
'page arguments' => array('mymodule_page'),
'access arguments' => array('access content'),
)
);
}
/**
* A form with a dropdown whose options are dependent on a
* choice made in a previous dropdown.
*
* On changing the first dropdown, the options in the second are updated.
*/
function mymodule_page($form, &$form_state) {
// Get the list of options to populate the first dropdown.
$options_first = mymodule_first_dropdown_options();
// If we have a value for the first dropdown from $form_state['values'] we use
// this both as the default value for the first dropdown and also as a
// parameter to pass to the function that retrieves the options for the
// second dropdown.
$value_dropdown_first = isset($form_state['values']['dropdown_first']) ? $form_state['values']['dropdown_first'] : key($options_first);
$form['dropdown_first'] = array(
'#type' => 'select',
'#title' => 'First Dropdown',
'#options' => $options_first,
'#default_value' => $value_dropdown_first,
// Bind an ajax callback to the change event (which is the default for the
// select form type) of the first dropdown. It will replace the second
// dropdown when rebuilt
'#ajax' => array(
// When 'event' occurs, Drupal will perform an ajax request in the
// background. Usually the default value is sufficient (eg. change for
// select elements), but valid values include any jQuery event,
// most notably 'mousedown', 'blur', and 'submit'.
'event' => 'change',
'callback' => 'mymodule_ajax_callback',
'wrapper' => 'dropdown_second_replace',
),
);
$form['dropdown_second'] = array(
'#type' => 'select',
'#title' => 'Second Dropdown',
// The entire enclosing div created here gets replaced when dropdown_first
// is changed.
'#prefix' => '<div id="dropdown_second_replace">',
'#suffix' => '</div>',
// when the form is rebuilt during ajax processing, the $value_dropdown_first variable
// will now have the new value and so the options will change
'#options' => mymodule_second_dropdown_options($value_dropdown_first),
'#default_value' => isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : '',
);
return $form;
}
/**
* Selects just the second dropdown to be returned for re-rendering
*
* Since the controlling logic for populating the form is in the form builder
* function, all we do here is select the element and return it to be updated.
*
* @return renderable array (the second dropdown)
*/
function mymodule_ajax_callback($form, $form_state) {
return $form['dropdown_second'];
}
/**
* Helper function to populate the first dropdown. This would normally be
* pulling data from the database.
*
* @return array of options
*/
function mymodule_first_dropdown_options() {
return array(
'colors' => 'Names of colors',
'cities' => 'Names of cities',
'animals' => 'Names of animals',
);
}
/**
* Helper function to populate the second dropdown. This would normally be
* pulling data from the database.
*
* @param key. This will determine which set of options is returned.
*
* @return array of options
*/
function mymodule_second_dropdown_options($key = '') {
$options = array(
'colors' => array(
'red' => 'Red',
'green' => 'Green',
'blue' => 'Blue'
),
'cities' => array(
'paris' => 'Paris, France',
'tokyo' => 'Tokyo, Japan',
'newyork' => 'New York, US'
),
'animals' => array(
'dog' => 'Dog',
'cat' => 'Cat',
'bird' => 'Bird'
),
);
if (isset($options[$key])) {
return $options[$key];
}
else {
return array();
}
}
위의 코드를 사용했습니다. 하지만 내가 선택한 값을 얻을 수 없습니다. dropdown_one을 변경하면 dropdown_second가 표시되지 않습니다. 나는 당신에게 한 가지 변화를 주었다. hook_page() 대신 hook_form_form_id_alter 훅을 사용했습니다. 나는 질문을 제기했다. 이 문제를 확인하고 도울 수 있습니까? http://stackoverflow.com/questions/23496574/drupal-dynamic-select-list-not-reterving-values-in-subcategory-field – Ananth
wildpeaks에서 답변 콜백은 다음과 같아야합니다
내가 예를 들어 내 대답을 업데이트function mymodule_ajax_callback($form, $form_state) {
return render($form['dropdown_second']);
}
Drupal 7 – wildpeaks