Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Implements hook_menu()
- */
- function example_hs_menu() {
- $items['example-hs'] = array(
- 'title' => 'Example Hierarchical Select',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('example_hs_example_form'),
- 'access arguments' => array('access content'),
- );
- return $items;
- }
- /**
- * Form builder
- */
- function example_hs_example_form($form, $form_state) {
- // Враппер для селектов
- $form['terms'] = array(
- '#tree' => TRUE,
- '#prefix' => '<div id="terms">',
- '#suffix' => '</div>',
- );
- // Если сработает AJAX, то в $form_state['values']['terms'] будут выбранные значения селектов
- $terms_values = isset($form_state['values']['terms']) ? $form_state['values']['terms'] : array();
- // Селектов будет генерится на 1 больше чем есть в $form_state['values']['terms']
- $terms_values += array('term' . (count($terms_values) + 1) => 'none');
- // В первом селекте будут выводится термины без родителя
- $parent_tid = 0;
- foreach ($terms_values as $input_name => $tid) {
- // Завершаем цикл если в предыдущем селекте ничего не выбрано
- // или у термина нет дочерних терминов
- if ($parent_tid === 'none' || !($terms = taxonomy_get_tree(6, $parent_tid, 1))) {
- break;
- }
- // Формируем данные для селекта
- $options = array('none' => t('- Any -'));
- foreach ($terms as $term) {
- $options[$term->tid] = $term->name;
- }
- // Создаём сам селект
- $form['terms'][$input_name] = array(
- '#title' => $parent_tid == 'none' ? t('Region') : t('Town'),
- '#type' => 'select',
- '#options' => $options,
- '#default_value' => $tid,
- '#ajax' => array(
- 'callback' => 'example_hs_example_form_ajax_callback',
- 'wrapper' => 'terms',
- ),
- );
- $form['terms']['term2'] = array(
- '#title' => t('Town'),
- '#type' => 'select',
- '#options' => $options,
- '#default_value' => $tid,
- '#attributes' => $parent_tid != 'none' ? '' : array('disabled' => array('disabled')),
- );
- $parent_tid = isset($options[$tid]) ? $tid : 'none';
- }
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => 'Submit',
- );
- return $form;
- }
- /**
- * AJAX callback
- */
- function example_hs_example_form_ajax_callback($form, $form_state) {
- return $form['terms'];
- }
- /**
- * Submit callback
- */
- function example_hs_example_form_submit($form, $form_state) {
- dsm($form_state);
- }
- /**
- * Реализация hook_block_info()
- * Информация о блоке и дефолтные настройки
- */
- function example_hs_block_info() {
- $blocks['hello-message'] = array('info' => t('Message'));
- return $blocks;
- }
- /**
- * Реализация hook_block_view()
- * Генерация заголовка блока и его контента
- */
- function example_hs_block_view($delta = '') {
- $block = array();
- if ($delta == 'hello-message') {
- $block['subject'] = t('Message');
- $block['content'] = drupal_render(drupal_get_form('example_hs_example_form'));
- }
- return $block;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement